Load ruffle.js at page load

This commit is contained in:
JustinCB 2020-04-30 09:06:08 -04:00 committed by Mike Welsh
parent d1fff21560
commit 850fb81574
2 changed files with 31 additions and 27 deletions

View File

@ -1,4 +1,4 @@
(/**
/**
* Pierce the extension sandbox by copying our code into window space.
*
* The isolation extension content scripts get is neat, but it causes problems
@ -14,29 +14,32 @@
* unintentionally.
* 2. The ability to load extension resources such as .wasm files
*/
async function () {
let page_optout = document.getElementsByTagName("html")[0].dataset.ruffleOptout !== undefined;
let obfuscated_event_prefix = "rufEvent" + Math.floor(Math.random() * 100000000000);
if (!page_optout) {
let ext_path = "";
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 ruffle_src_resp = await fetch(ext_path + "dist/ruffle.js");
if (ruffle_src_resp.ok) {
let ruffle_src = "(function () { var runtime_path = \"" +
ext_path + "\";\nvar obfuscated_event_prefix = \"" +
obfuscated_event_prefix + "\";\n" +
await ruffle_src_resp.text() + "}())";
let scriptelem = document.createElement("script");
scriptelem.appendChild(document.createTextNode(ruffle_src));
document.head.appendChild(scriptelem);
} else {
console.error("Critical error loading Ruffle into page")
}
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();
}
}
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});
}

View File

@ -14,7 +14,8 @@
{
"matches": ["<all_urls>"],
"js": ["js/lv0.js"],
"all_frames": true
"all_frames": true,
"run_at": "document_start"
}
],
"web_accessible_resources": ["dist/*"]