jepawiki / lib /postParentMessage.ts
mishig's picture
mishig HF Staff
Upload folder using huggingface_hub
88b79dc verified
Raw History Blame Contribute Delete
844 Bytes
// Utility to post a message to the parent window with custom URLSearchParams.
// Used to sync this iframe's path with the HF Spaces parent URL bar so that
// shareable links to https://huggingface.co/spaces/mishig/jepawiki?path=...
// open the right wiki page.
//
// Pattern borrowed from lerobot-dataset-visualizer.
export function postParentMessageWithParams(
setParams: (params: URLSearchParams) => void,
) {
if (typeof window === "undefined") return;
if (window.parent === window) return; // not in an iframe
try {
const parentOrigin = "https://huggingface.co";
const searchParams = new URLSearchParams();
setParams(searchParams);
window.parent.postMessage(
{ queryString: searchParams.toString() },
parentOrigin,
);
} catch {
// Ignore cross-origin restrictions or non-iframe contexts.
}
}