This commit is contained in:
iqudoo
2026-05-26 02:37:01 +08:00
parent c08cdaee68
commit cdec1838ac
14 changed files with 1788 additions and 267 deletions

View File

@@ -13,8 +13,12 @@ import { createUUID } from "./utils/uuid";
import {
dispatchEmbedScanResult,
acknowledgeEmbedScanConsumed,
dispatchEmbedScanError,
} from "./services/provider/scan";
import { setEmbedScanResultForwarder } from "./services/embedScanBridge";
import {
setEmbedScanResultForwarder,
setEmbedScanErrorForwarder,
} from "./services/embedScanBridge";
const EMBED_SOURCE = "IScanEmbed";
const EMBED_V = 1;
@@ -25,6 +29,8 @@ const embedChildSources = new Set();
const EMBED_LISTENER_METHODS = new Set([
"onScanListener",
"offScanListener",
"onScanErrorListener",
"offScanErrorListener",
"clear",
]);
@@ -105,6 +111,26 @@ function hydrateEmbedParams(params, messageSource, targetOrigin) {
return params.map(walk);
}
function broadcastScanErrorToEmbedChildren(error) {
if (embedChildSources.size === 0 || error == null || error === "") {
return;
}
embedChildSources.forEach((source) => {
try {
source.postMessage(
{
source: EMBED_SOURCE,
v: EMBED_V,
kind: "forwardScanError",
error,
},
"*"
);
} catch (e) {
}
});
}
function broadcastScanResultToEmbedChildren(result) {
if (embedChildSources.size === 0 || result == null || result === "") {
return;
@@ -198,6 +224,12 @@ function embedChildOnMessage(ev) {
}
return;
}
if (data.kind === "forwardScanError") {
if (typeof data.error === "string") {
dispatchEmbedScanError(data.error);
}
return;
}
if (data.kind === "invokeResult") {
const pending = pendingInvokes[data.id];
if (!pending) {
@@ -307,6 +339,7 @@ export function installEmbedHost(lib) {
}
embedHostInstalled = true;
setEmbedScanResultForwarder(broadcastScanResultToEmbedChildren);
setEmbedScanErrorForwarder(broadcastScanErrorToEmbedChildren);
window.addEventListener("message", (ev) => {
const data = ev.data;
if (!isEmbedMessage(data)) {