Skip to content

Commit 6e6cda0

Browse files
committed
extension: When possible, replace script injection with content scripts
1 parent 48443e9 commit 6e6cda0

File tree

4 files changed

+41
-30
lines changed

4 files changed

+41
-30
lines changed

web/packages/extension/src/background.ts

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -175,24 +175,35 @@ async function enable() {
175175
return;
176176
}
177177
if (!(await contentScriptRegistered())) {
178+
const excludeMatches = [
179+
"https://sso.godaddy.com/*", // See https://github.com/ruffle-rs/ruffle/pull/7146
180+
"https://authentication.td.com/*", // See https://github.com/ruffle-rs/ruffle/issues/2158
181+
"https://*.twitch.tv/*", // See https://github.com/ruffle-rs/ruffle/pull/8150
182+
"https://www.tuxedocomputers.com/*", // See https://github.com/ruffle-rs/ruffle/issues/11906
183+
"https://*.taobao.com/*", // See https://github.com/ruffle-rs/ruffle/pull/12650
184+
"https://*.time4learning.com/*", // See https://github.com/ruffle-rs/ruffle/pull/16186
185+
"https://*.edgenuity.com/*", // See https://github.com/ruffle-rs/ruffle/pull/16186
186+
"https://www.chewy.com/*", // See https://github.com/ruffle-rs/ruffle/issues/18265
187+
"https://*.duosecurity.com/*", // See https://github.com/ruffle-rs/ruffle/pull/18299
188+
"https://*.tiktok.com/*", // See https://github.com/ruffle-rs/ruffle/pull/20250
189+
];
178190
await utils.scripting.registerContentScripts([
191+
{
192+
id: "ruffle",
193+
js: ["dist/ruffle.js"],
194+
persistAcrossSessions: true,
195+
matches: ["<all_urls>"],
196+
excludeMatches,
197+
runAt: "document_start",
198+
allFrames: true,
199+
world: "MAIN",
200+
},
179201
{
180202
id: "plugin-polyfill",
181203
js: ["dist/pluginPolyfill.js"],
182204
persistAcrossSessions: true,
183205
matches: ["<all_urls>"],
184-
excludeMatches: [
185-
"https://sso.godaddy.com/*", // See https://github.com/ruffle-rs/ruffle/pull/7146
186-
"https://authentication.td.com/*", // See https://github.com/ruffle-rs/ruffle/issues/2158
187-
"https://*.twitch.tv/*", // See https://github.com/ruffle-rs/ruffle/pull/8150
188-
"https://www.tuxedocomputers.com/*", // See https://github.com/ruffle-rs/ruffle/issues/11906
189-
"https://*.taobao.com/*", // See https://github.com/ruffle-rs/ruffle/pull/12650
190-
"https://*.time4learning.com/*", // See https://github.com/ruffle-rs/ruffle/pull/16186
191-
"https://*.edgenuity.com/*", // See https://github.com/ruffle-rs/ruffle/pull/16186
192-
"https://www.chewy.com/*", // See https://github.com/ruffle-rs/ruffle/issues/18265
193-
"https://*.duosecurity.com/*", // See https://github.com/ruffle-rs/ruffle/pull/18299
194-
"https://*.tiktok.com/*", // See https://github.com/ruffle-rs/ruffle/pull/20250
195-
],
206+
excludeMatches,
196207
runAt: "document_start",
197208
allFrames: true,
198209
world: "MAIN",
@@ -222,7 +233,7 @@ async function disable() {
222233
}
223234
if (await contentScriptRegistered()) {
224235
await utils.scripting.unregisterContentScripts({
225-
ids: ["plugin-polyfill", "4399"],
236+
ids: ["ruffle", "plugin-polyfill", "4399"],
226237
});
227238
}
228239
await disableSWFTakeover();

web/packages/extension/src/content.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
11
/**
2-
* Pierce the extension sandbox by copying our code into main world.
32
*
4-
* The isolation extension content scripts get is neat, but it causes problems
5-
* based on what browser you use:
6-
*
7-
* 1. On Chrome, you are explicitly banned from registering custom elements.
8-
* 2. On Firefox, you can register custom elements but they can't expose any
9-
* useful API surface, and can't even see their own methods.
10-
*
11-
* This code exists to pierce the extension sandbox, while maintaining:
3+
* This code provides a content script message listener that proxies messages
4+
* into/from the main world.
125
*
13-
* 1. The isolation of not interfering with the page's execution environment
14-
* unintentionally.
15-
* 2. The ability to load extension resources such as .wasm files.
6+
* On older Firefox, it also pierces the extension sandbox by copying our code into the main world
167
*
17-
* We also provide a content script message listener that proxies messages
18-
* into/from the main world.
198
*/
209

2110
import * as utils from "./utils";
@@ -169,10 +158,9 @@ function isXMLDocument(): boolean {
169158
?.filename !== "ruffle.js"
170159
) {
171160
injectScriptRaw("%PLUGIN_POLYFILL_SOURCE%");
161+
await injectScriptURL(utils.runtime.getURL("dist/ruffle.js"));
172162
}
173163

174-
await injectScriptURL(utils.runtime.getURL(`dist/ruffle.js?id=${ID}`));
175-
176164
window.addEventListener("message", (event) => {
177165
// We only accept messages from ourselves.
178166
if (event.source !== window || !event.data) {

web/packages/extension/src/ruffle.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import { Setup, setCurrentScriptURL } from "ruffle-core";
22
import { Message } from "./messages";
33

4+
/**
5+
*
6+
* This script runs in the MAIN ExecutionWorld for the following reasons:
7+
*
8+
* 1. On Chrome, you are explicitly banned from registering custom elements.
9+
* 2. On Firefox, you can register custom elements but they can't expose any
10+
* useful API surface, and can't even see their own methods.
11+
*
12+
*/
13+
414
// Current message ID to be included in openInNewTab
515
let currentMessageId: string | null = null;
616

web/packages/extension/webpack.config.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ export default function (/** @type {Record<string, any>} */ env, _argv) {
104104
},
105105
output: {
106106
path: url.fileURLToPath(new URL("assets/dist/", import.meta.url)),
107-
publicPath: "auto",
107+
// publicPath: "auto" throws for content scripts, which lack a script src
108+
// This is changed at runtime to the full URL of the extension's /dist/ folder
109+
publicPath: "/dist/",
108110
clean: true,
109111
assetModuleFilename: "assets/[name][ext][query]",
110112
},

0 commit comments

Comments
 (0)