web: Pass obfuscatedEventPrefix via query string

This eliminates the need to create a script dynamically, and fixes
publicPath() to correctly return the extension URL rather than the
host site URL.
This commit is contained in:
relrelb 2021-03-11 22:09:51 +02:00 committed by Mike Welsh
parent 56cece3336
commit 12dee2199e
3 changed files with 29 additions and 25 deletions

View File

@ -7,6 +7,25 @@ window.RufflePlayer = PublicAPI.negotiate(
);
__webpack_public_path__ = publicPath(window.RufflePlayer.config, "extension");
function getObfuscatedEventPrefix() {
if (
document.currentScript !== undefined &&
document.currentScript !== null &&
"src" in document.currentScript &&
document.currentScript.src !== ""
) {
// Default to the directory where this script resides.
try {
return new URL(document.currentScript.src).searchParams.get(
"obfuscatedEventPrefix"
);
} catch (e) {
return null;
}
}
}
const obfuscatedEventPrefix = getObfuscatedEventPrefix();
if (obfuscatedEventPrefix) {
document.addEventListener(obfuscatedEventPrefix + "_request", function (e) {
let body = JSON.parse(e.detail);

View File

@ -139,8 +139,6 @@ getSyncStorage(["ruffleEnable", "ignoreOptout"], function (data) {
}
});
const extPath = getExtensionUrl();
if (shouldLoadUntrustedWorld) {
// 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).
@ -151,22 +149,13 @@ getSyncStorage(["ruffleEnable", "ignoreOptout"], function (data) {
// Load Ruffle script asynchronously. By doing so, we can inject extra variables and isolate them from the global scope.
(async function () {
let ruffleSrcResp = await fetch(extPath + "dist/ruffle.js");
if (ruffleSrcResp.ok) {
let ruffleSource =
'(function () { var obfuscatedEventPrefix = "' +
obfuscatedEventPrefix +
'";\n' +
(await ruffleSrcResp.text()) +
"}())";
let ruffleScript = document.createElement("script");
ruffleScript.appendChild(document.createTextNode(ruffleSource));
(document.head || document.documentElement).appendChild(
ruffleScript
);
} else {
console.error("Critical error loading Ruffle into page");
}
const ruffleScript = document.createElement("script");
ruffleScript.src = getExtensionUrl(
`dist/ruffle.js?obfuscatedEventPrefix=${obfuscatedEventPrefix}`
);
(document.head || document.documentElement).appendChild(
ruffleScript
);
})();
}
});

View File

@ -213,15 +213,11 @@ function setMessageListener(listener) {
}
}
function getExtensionUrl() {
function getExtensionUrl(path) {
if (chrome && chrome.extension && chrome.extension.getURL) {
return chrome.extension
.getURL("dist/ruffle.js")
.replace("dist/ruffle.js", "");
return chrome.extension.getURL(path);
} else if (browser && browser.runtime && browser.runtime.getURL) {
return browser.runtime
.getURL("dist/ruffle.js")
.replace("dist/ruffle.js", "");
return browser.runtime.getURL(path);
} else {
console.error("Couldn't get extension URL");
}