/** * Pierce the extension sandbox by copying our code into main world. * * The isolation extension content scripts get is neat, but it causes problems * based on what browser you use: * * 1. On Chrome, you are explicitly banned from registering custom elements. * 2. On Firefox, you can register custom elements but they can't expose any * useful API surface, and can't even see their own methods. * * This code exists to pierce the extension sandbox, while maintaining: * * 1. The isolation of not interfering with the page's execution environment * unintentionally. * 2. The ability to load extension resources such as .wasm files. * * We also provide a content script message listener that proxies messages * into/from the main world. */ import * as utils from "./utils"; const pendingMessages: ({ resolve(value: unknown): void; reject(reason?: unknown): void; } | null)[] = []; const uniqueMessageSuffix = Math.floor(Math.random() * 100000000000); /** * Send a message to the main world, where Ruffle runs. * @param {*} data - JSON-serializable data to send to main world. * @returns {Promise<*>} JSON-serializable response from main world. */ function sendMessageToPage(data: unknown): Promise { const message = { type: `FROM_RUFFLE${uniqueMessageSuffix}`, index: pendingMessages.length, data, }; window.postMessage(message, "*"); return new Promise((resolve, reject) => { pendingMessages.push({ resolve, reject }); }); } /** * Inject a raw script to the main world. * @param {string} src - Script to inject. */ function injectScriptRaw(src: string) { const script = document.createElement("script"); script.textContent = src; (document.head || document.documentElement).append(script); } /** * Inject a script by URL to the main world. * @param {string} url - Script URL to inject. */ function injectScriptURL(url: string) { const script = document.createElement("script"); script.src = url; (document.head || document.documentElement).append(script); } /** * Check whether the current page (or one of its ancestors) is configured * to opt-out from Ruffle. * @returns {boolean} Whether the current page opts-out or not. */ function checkPageOptout(): boolean { if (document.documentElement.hasAttribute("data-ruffle-optout")) { return true; } try { if ( window.top && window.top.document && window.top.document.documentElement && window.top.document.documentElement.hasAttribute( "data-ruffle-optout" ) ) { // In case the opting-out page uses iframes. return true; } } catch (e) { console.warn(`Unable to check top-level optout: ${e.message}`); } return false; } /** * @returns {boolean} Whether the current page is an XML document or not. */ function isXMLDocument(): boolean { // Based on https://developer.mozilla.org/en-US/docs/Web/API/Document/xmlVersion return document.createElement("foo").tagName !== "FOO"; } (async () => { const options = await utils.getOptions(["ruffleEnable", "ignoreOptout"]); const pageOptout = checkPageOptout(); const shouldLoad = !isXMLDocument() && options.ruffleEnable && !window.RufflePlayer && (options.ignoreOptout || !pageOptout); utils.runtime.onMessage.addListener((message, sender, sendResponse) => { if (shouldLoad) { sendMessageToPage(message).then((response) => { sendResponse({ loaded: true, tabSettings: options, optout: pageOptout, data: response, }); }); return true; } else { sendResponse({ loaded: false, tabSettings: options, optout: pageOptout, }); return false; } }); if (shouldLoad) { // We must run the plugin polyfill before any flash detection scripts. // Unfortunately, this might still be too late for some websites when using Chrome (issue #969). // TODO: use plugin-polyfill.ts injectScriptRaw( '(function(){class RuffleMimeType{constructor(a,b,c){this.type=a,this.description=b,this.suffixes=c}}class RuffleMimeTypeArray{constructor(a){this.__mimetypes=[],this.__named_mimetypes={};for(let b of a)this.install(b)}install(a){let b=this.__mimetypes.length;this.__mimetypes.push(a),this.__named_mimetypes[a.type]=a,this[a.type]=a,this[b]=a}item(a){return this.__mimetypes[a]}namedItem(a){return this.__named_mimetypes[a]}get length(){return this.__mimetypes.length}}class RufflePlugin extends RuffleMimeTypeArray{constructor(a,b,c,d){super(d),this.name=a,this.description=b,this.filename=c}install(a){a.enabledPlugin||(a.enabledPlugin=this),super.install(a)}}class RufflePluginArray{constructor(a){this.__plugins=[],this.__named_plugins={};for(let b of a)this.install(b)}install(a){let b=this.__plugins.length;this.__plugins.push(a),this.__named_plugins[a.name]=a,this[a.name]=a,this[b]=a}item(a){return this.__plugins[a]}namedItem(a){return this.__named_plugins[a]}get length(){return this.__plugins.length}}const FLASH_PLUGIN=new RufflePlugin("Shockwave Flash","Shockwave Flash 32.0 r0","ruffle.js",[new RuffleMimeType("application/futuresplash","Shockwave Flash","spl"),new RuffleMimeType("application/x-shockwave-flash","Shockwave Flash","swf"),new RuffleMimeType("application/x-shockwave-flash2-preview","Shockwave Flash","swf"),new RuffleMimeType("application/vnd.adobe.flash-movie","Shockwave Flash","swf")]);function install_plugin(a){navigator.plugins.install||Object.defineProperty(navigator,"plugins",{value:new RufflePluginArray(navigator.plugins),writable:!1}),navigator.plugins.install(a),0 { // We only accept messages from ourselves. if (event.source !== window) { return; } const { type, index, data } = event.data; if (type === `TO_RUFFLE${uniqueMessageSuffix}`) { const request = pendingMessages[index]; if (request) { pendingMessages[index] = null; request.resolve(data); } else { // TODO: Handle page-initiated messages. console.warn("No pending request."); } } }); } })();