Skip to content

Truly independent web browser

1.5 KB
Raw Blame
<!DOCTYPE html>
<script>
    // Nests a copy of itself under a new URL, so no ancestor URL ever matches.
    const depth = Number(new URLSearchParams(location.search).get("depth") ?? 1);
    const nextURL = `nested-iframe-with-distinct-url.html?depth=${depth + 1}`;
    const iframe = document.createElement("iframe");
    if (depth < 30)
        iframe.src = nextURL;
    document.documentElement.appendChild(iframe);

    window.onmessage = () => {
        // A frame that did not load keeps its initial about:blank document.
        if (iframe.contentDocument?.URL !== "about:blank") {
            iframe.contentWindow.postMessage("probe", "*");
            return;
        }
        const blankIframe = document.createElement("iframe");
        blankIframe.onload = () => {
            // An opened document takes our URL, so this is a navigation to a fragment of it.
            blankIframe.contentDocument.open();
            blankIframe.contentDocument.close();
            blankIframe.contentWindow.location.hash = "fragment";
            const fragmentNavigated = blankIframe.contentWindow.location.hash === "#fragment";
            iframe.contentWindow.navigation.navigate(nextURL).committed.then(
                () => top.postMessage({ depth, fragmentNavigated, navigation: "committed" }, "*"),
                error => top.postMessage({ depth, fragmentNavigated, navigation: error.name }, "*"));
        };
        document.documentElement.appendChild(blankIframe);
    };
</script>