Skip to content

Truly independent web browser

1.9 KB
Raw Blame
<!DOCTYPE html>
<script src="../include.js"></script>
<script src="gamepad-helper.js"></script>
<script>
    asyncTest(async (done) => {
        const gamepad = internals.connectVirtualGamepad();
        await pumpGamepadEvents();
        listenForGamepadConnected();

        println(`Before pressing button in top level: ${getStringifiedGamepads()}`);
        gamepad.setButton(gamepad.buttons[0], true);
        await pumpGamepadEvents();
        println(`After pressing button in top level: ${getStringifiedGamepads()}`);
        gamepad.setButton(gamepad.buttons[0], false);
        await pumpGamepadEvents();

        const iframe = document.createElement("iframe");
        iframe.onload = async () => {
            const sendMessageAndWait = (message) => {
                return new Promise((resolve) => {
                    window.onmessage = ({ data }) => {
                        resolve(data);
                    };

                    iframe.contentWindow.postMessage(message, "*");
                });
            };

            println(`Gamepad should not be immediately exposed in the new navigable: ${await sendMessageAndWait("getGamepads")}`);

            // New navigables will automatically connect to available gamepads, otherwise this wouldn't do anything.
            gamepad.setButton(gamepad.buttons[0], true);
            await pumpGamepadEvents();

            println(`Gamepad is now exposed in the new navigable: ${await sendMessageAndWait("getGamepads")}`);

            gamepad.disconnect();
            done();
        };

        iframe.srcdoc = `
\u003cscript src="gamepad-helper.js"\u003e\u003c/script\u003e
\u003cscript\u003e
    window.onmessage = async ({ data }) => {
        switch (data) {
        case 'getGamepads':
            window.parent.postMessage(getStringifiedGamepads(), '*');
            break;
        default:
            break;
        }
    };
\u003c/script\u003e`;

        document.body.appendChild(iframe);
    });
</script>