2020-04-30 13:06:08 +00:00
|
|
|
/**
|
2019-08-24 23:56:46 +00:00
|
|
|
* Pierce the extension sandbox by copying our code into window space.
|
2019-08-23 02:39:00 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* The isolation extension content scripts get is neat, but it causes problems
|
|
|
|
* based on what browser you use:
|
2019-08-23 02:39:00 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* 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.
|
2019-08-23 02:39:00 +00:00
|
|
|
*
|
2019-08-24 23:56:46 +00:00
|
|
|
* 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
|
2019-08-23 02:39:00 +00:00
|
|
|
*/
|
2020-04-30 13:06:08 +00:00
|
|
|
function insert_ruffle(mutationsList,observer) {
|
|
|
|
let nodesAdded = mutationsList.some(mutation => mutation.addedNodes.length > 0);
|
|
|
|
let ext_path = "";
|
|
|
|
if (nodesAdded&&document.head) {
|
|
|
|
if (chrome && chrome.extension && chrome.extension.getURL) {
|
|
|
|
ext_path = chrome.extension.getURL("dist/ruffle.js").replace("dist/ruffle.js", "");
|
|
|
|
} else if (browser && browser.runtime && browser.runtime.getURL) {
|
|
|
|
ext_path = browser.runtime.getURL("dist/ruffle.js").replace("dist/ruffle.js", "");
|
|
|
|
}
|
|
|
|
let setup_scriptelem = document.createElement("script");
|
|
|
|
let setup_src = "var runtime_path = \"" +
|
|
|
|
ext_path + "\";\nvar obfuscated_event_prefix = \"" +
|
|
|
|
obfuscated_event_prefix + "\";";
|
|
|
|
let scriptelem = document.createElement("script");
|
|
|
|
setup_scriptelem.appendChild(document.createTextNode(setup_src));
|
|
|
|
document.head.appendChild(setup_scriptelem);
|
|
|
|
scriptelem.src=ext_path + "dist/ruffle.js";
|
|
|
|
document.head.appendChild(scriptelem);
|
|
|
|
observer.disconnect();
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 23:02:48 +00:00
|
|
|
|
2019-11-25 23:26:16 +00:00
|
|
|
|
2020-04-30 13:06:08 +00:00
|
|
|
let page_optout = document.getElementsByTagName("html")[0].dataset.ruffleOptout !== undefined;
|
|
|
|
let obfuscated_event_prefix = "rufEvent" + Math.floor(Math.random() * 100000000000);
|
|
|
|
if (!(page_optout||window.RufflePlayer)) {
|
|
|
|
const observer = new MutationObserver(insert_ruffle);
|
|
|
|
observer.observe(document, {childList: true, subtree: true});
|
|
|
|
}
|