修复模型库跨端口消息发送
This commit is contained in:
@@ -320,7 +320,7 @@ function requestSceneModelImport() {
|
|||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
type: requestSceneModelMessageType,
|
type: requestSceneModelMessageType,
|
||||||
requestId
|
requestId
|
||||||
}, resolveParentOrigin());
|
}, resolveParentPostMessageOrigin());
|
||||||
notify("已请求主程序导出当前选中模型");
|
notify("已请求主程序导出当前选中模型");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -360,7 +360,12 @@ function createSceneModelRequestId() {
|
|||||||
|
|
||||||
function isAllowedParentOrigin(origin: string) {
|
function isAllowedParentOrigin(origin: string) {
|
||||||
const expectedOrigin = resolveParentOrigin();
|
const expectedOrigin = resolveParentOrigin();
|
||||||
return expectedOrigin === "*" || origin === expectedOrigin;
|
return expectedOrigin === "*" || origin === expectedOrigin || isLocalDmtParentOrigin(origin);
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveParentPostMessageOrigin() {
|
||||||
|
const origin = resolveParentOrigin();
|
||||||
|
return origin === window.location.origin ? "*" : origin;
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveParentOrigin() {
|
function resolveParentOrigin() {
|
||||||
@@ -375,6 +380,16 @@ function resolveParentOrigin() {
|
|||||||
return "*";
|
return "*";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isLocalDmtParentOrigin(origin: string) {
|
||||||
|
try {
|
||||||
|
const url = new URL(origin);
|
||||||
|
const isLocalHost = url.hostname === "localhost" || url.hostname === "127.0.0.1";
|
||||||
|
return isLocalHost && url.port === "3000";
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function ensureGlbFileName(fileName: string) {
|
function ensureGlbFileName(fileName: string) {
|
||||||
return fileName.toLowerCase().endsWith(".glb") ? fileName : `${fileName}.glb`;
|
return fileName.toLowerCase().endsWith(".glb") ? fileName : `${fileName}.glb`;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -258,7 +258,7 @@ export function emitImportScenePayload(payload: PreviewImportScenePayload) {
|
|||||||
window.parent.postMessage({
|
window.parent.postMessage({
|
||||||
type: "DMT_MODEL_LIBRARY_IMPORT_SCENE",
|
type: "DMT_MODEL_LIBRARY_IMPORT_SCENE",
|
||||||
payload: buildParentImportScenePayload(payload)
|
payload: buildParentImportScenePayload(payload)
|
||||||
}, resolveParentOrigin());
|
}, resolveParentPostMessageOrigin());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
notifyError(error);
|
notifyError(error);
|
||||||
}
|
}
|
||||||
@@ -280,6 +280,11 @@ function buildParentImportScenePayload(payload: PreviewImportScenePayload): Pare
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveParentPostMessageOrigin() {
|
||||||
|
const origin = resolveParentOrigin();
|
||||||
|
return origin === window.location.origin ? "*" : origin;
|
||||||
|
}
|
||||||
|
|
||||||
function resolveParentOrigin() {
|
function resolveParentOrigin() {
|
||||||
try {
|
try {
|
||||||
const meta = import.meta as ImportMeta & { env?: Record<string, string | undefined> };
|
const meta = import.meta as ImportMeta & { env?: Record<string, string | undefined> };
|
||||||
|
|||||||
Reference in New Issue
Block a user