web: Inject plugin polyfill immediately, on chrome/edge

This commit is contained in:
Nathan Adams 2023-08-13 23:07:58 +02:00
parent 7533923f9b
commit 2f9387f1ac
3 changed files with 26 additions and 6 deletions

View File

@ -48,6 +48,7 @@
"storage",
"webRequest",
"declarativeNetRequest",
"scripting",
],
"web_accessible_resources": [{
"resources": ["*"],

View File

@ -63,6 +63,24 @@ async function enable() {
removeRuleIds: [RULE_SWF_URL],
addRules: rules,
});
await chrome.scripting.registerContentScripts([
{
id: "plugin-polyfill",
js: ["dist/pluginPolyfill.js"],
persistAcrossSessions: false,
matches: ["<all_urls>"],
excludeMatches: [
"https://sso.godaddy.com/*",
"https://authentication.td.com/*",
"https://*.twitch.tv/*",
"https://www.tuxedocomputers.com/*",
"https://*.taobao.com/*",
],
runAt: "document_start",
world: "MAIN",
},
]);
} else {
(chrome || browser).webRequest.onHeadersReceived.addListener(
onHeadersReceived,
@ -80,6 +98,9 @@ async function disable() {
await chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: [RULE_SWF_URL],
});
await chrome.scripting.unregisterContentScripts({
ids: ["plugin-polyfill"],
});
} else {
(chrome || browser).webRequest.onHeadersReceived.removeListener(
onHeadersReceived,

View File

@ -142,15 +142,13 @@ function isXMLDocument(): boolean {
}
// 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).
// Unfortunately, this might still be too late for some websites (issue #969).
// NOTE: The script code injected here is the compiled form of
// plugin-polyfill.ts. It is injected by tools/inject_plugin_polyfill.js
// which just search-and-replaces for this particular string.
if (chrome) {
await injectScriptURL(
utils.runtime.getURL(`dist/pluginPolyfill.js?id=${ID}`),
);
} else {
if (!chrome) {
// Chrome does this differently, by injecting it straight into the main world.
// This isn't as fast, oh well.
injectScriptRaw("%PLUGIN_POLYFILL_SOURCE%");
}