Initial commit

This commit is contained in:
TYt50
2026-01-17 16:34:39 +08:00
commit 73b8a6d23f
72 changed files with 45972 additions and 0 deletions

27
core/iframe-messaging.js Normal file
View File

@@ -0,0 +1,27 @@
export function getTrustedOrigin() {
return window.location.origin;
}
export function getIframeTargetOrigin(iframe) {
const sandbox = iframe?.getAttribute?.('sandbox') || '';
if (sandbox && !sandbox.includes('allow-same-origin')) return 'null';
return getTrustedOrigin();
}
export function postToIframe(iframe, payload, source, targetOrigin = null) {
if (!iframe?.contentWindow) return false;
const message = source ? { source, ...payload } : payload;
const origin = targetOrigin || getTrustedOrigin();
iframe.contentWindow.postMessage(message, origin);
return true;
}
export function isTrustedIframeEvent(event, iframe) {
return !!iframe && event.origin === getTrustedOrigin() && event.source === iframe.contentWindow;
}
export function isTrustedMessage(event, iframe, expectedSource) {
if (!isTrustedIframeEvent(event, iframe)) return false;
if (expectedSource && event?.data?.source !== expectedSource) return false;
return true;
}