Use pollyfill for invidious registration

Fix #798
This commit is contained in:
Ajay
2023-02-06 21:22:34 -05:00
parent b2e6e4f076
commit 92e156834b
5 changed files with 141 additions and 67 deletions

View File

@@ -84,7 +84,8 @@
"https://sponsor.ajay.app/*" "https://sponsor.ajay.app/*"
], ],
"optional_permissions": [ "optional_permissions": [
"*://*/*" "*://*/*",
"webNavigation"
], ],
"browser_action": { "browser_action": {
"default_title": "SponsorBlock", "default_title": "SponsorBlock",

95
package-lock.json generated
View File

@@ -28,6 +28,7 @@
"license": "LGPL-3.0-or-later", "license": "LGPL-3.0-or-later",
"dependencies": { "dependencies": {
"@ajayyy/maze-utils": "^1.0.3", "@ajayyy/maze-utils": "^1.0.3",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0" "react-dom": "^18.2.0"
}, },
@@ -4286,6 +4287,19 @@
"url": "https://github.com/yeoman/configstore?sponsor=1" "url": "https://github.com/yeoman/configstore?sponsor=1"
} }
}, },
"node_modules/content-scripts-register-polyfill": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/content-scripts-register-polyfill/-/content-scripts-register-polyfill-4.0.2.tgz",
"integrity": "sha512-8hDm+tu3BkxHZP7EUIIIo/495F6QNXF7cI9Lwr4PQaiohw2wWmi9k2SE4W4kNrAaLnFw6RZ2ev8EmrQb+sCoGQ==",
"dependencies": {
"webext-content-scripts": "^2.5.2",
"webext-patterns": "^1.3.0",
"webext-polyfill-kinda": "^1.0.0"
},
"funding": {
"url": "https://github.com/sponsors/fregante"
}
},
"node_modules/convert-source-map": { "node_modules/convert-source-map": {
"version": "1.8.0", "version": "1.8.0",
"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz",
@@ -12811,6 +12825,48 @@
"node": ">= 8" "node": ">= 8"
} }
}, },
"node_modules/webext-content-scripts": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/webext-content-scripts/-/webext-content-scripts-2.5.2.tgz",
"integrity": "sha512-N1Xq/E8dx0lVAOyPquuo+2Vj9Fx1GoqCFo79lWeJHbemaBJ53N3BHBmbJJYsQ8FOP1xiwN4bPRQY2dpSjHAD3Q==",
"dependencies": {
"webext-patterns": "^1.3.0",
"webext-polyfill-kinda": "^1.0.0"
},
"funding": {
"url": "https://github.com/sponsors/fregante"
}
},
"node_modules/webext-patterns": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/webext-patterns/-/webext-patterns-1.3.0.tgz",
"integrity": "sha512-X9HMnic9ZtvSFKi2cdh0l+sxyj7f9oLedaa2JfxjnyEqGBz8OJjaHQ40jmraX1DJLTHOpqr+rCz1r3MW2+doUg==",
"dependencies": {
"escape-string-regexp": "^5.0.0"
},
"funding": {
"url": "https://github.com/sponsors/fregante"
}
},
"node_modules/webext-patterns/node_modules/escape-string-regexp": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
"integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw==",
"engines": {
"node": ">=12"
},
"funding": {
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/webext-polyfill-kinda": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/webext-polyfill-kinda/-/webext-polyfill-kinda-1.0.0.tgz",
"integrity": "sha512-Py/d3w/bC0KntuO60ePSWHsdrebZ3uYBLeFUjyPkDV3yTEQib0MRFvPh57t8XjImu4ylBoEAsFjzh/r22UtxMw==",
"funding": {
"url": "https://github.com/sponsors/fregante"
}
},
"node_modules/webidl-conversions": { "node_modules/webidl-conversions": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",
@@ -16550,6 +16606,16 @@
"xdg-basedir": "^5.0.1" "xdg-basedir": "^5.0.1"
} }
}, },
"content-scripts-register-polyfill": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/content-scripts-register-polyfill/-/content-scripts-register-polyfill-4.0.2.tgz",
"integrity": "sha512-8hDm+tu3BkxHZP7EUIIIo/495F6QNXF7cI9Lwr4PQaiohw2wWmi9k2SE4W4kNrAaLnFw6RZ2ev8EmrQb+sCoGQ==",
"requires": {
"webext-content-scripts": "^2.5.2",
"webext-patterns": "^1.3.0",
"webext-polyfill-kinda": "^1.0.0"
}
},
"convert-source-map": { "convert-source-map": {
"version": "1.8.0", "version": "1.8.0",
"resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz", "resolved": "https://registry.npmjs.org/convert-source-map/-/convert-source-map-1.8.0.tgz",
@@ -22820,6 +22886,35 @@
"integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==",
"dev": true "dev": true
}, },
"webext-content-scripts": {
"version": "2.5.2",
"resolved": "https://registry.npmjs.org/webext-content-scripts/-/webext-content-scripts-2.5.2.tgz",
"integrity": "sha512-N1Xq/E8dx0lVAOyPquuo+2Vj9Fx1GoqCFo79lWeJHbemaBJ53N3BHBmbJJYsQ8FOP1xiwN4bPRQY2dpSjHAD3Q==",
"requires": {
"webext-patterns": "^1.3.0",
"webext-polyfill-kinda": "^1.0.0"
}
},
"webext-patterns": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/webext-patterns/-/webext-patterns-1.3.0.tgz",
"integrity": "sha512-X9HMnic9ZtvSFKi2cdh0l+sxyj7f9oLedaa2JfxjnyEqGBz8OJjaHQ40jmraX1DJLTHOpqr+rCz1r3MW2+doUg==",
"requires": {
"escape-string-regexp": "^5.0.0"
},
"dependencies": {
"escape-string-regexp": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-5.0.0.tgz",
"integrity": "sha512-/veY75JbMK4j1yjvuUxuVsiS/hr/4iHs9FTT6cgTexxdE0Ly/glccBAkloH/DofkjRbZU3bnoj38mOmhkZ0lHw=="
}
}
},
"webext-polyfill-kinda": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/webext-polyfill-kinda/-/webext-polyfill-kinda-1.0.0.tgz",
"integrity": "sha512-Py/d3w/bC0KntuO60ePSWHsdrebZ3uYBLeFUjyPkDV3yTEQib0MRFvPh57t8XjImu4ylBoEAsFjzh/r22UtxMw=="
},
"webidl-conversions": { "webidl-conversions": {
"version": "7.0.0", "version": "7.0.0",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-7.0.0.tgz",

View File

@@ -4,9 +4,10 @@
"description": "", "description": "",
"main": "background.js", "main": "background.js",
"dependencies": { "dependencies": {
"@ajayyy/maze-utils": "^1.0.3",
"content-scripts-register-polyfill": "^4.0.2",
"react": "^18.2.0", "react": "^18.2.0",
"react-dom": "^18.2.0", "react-dom": "^18.2.0"
"@ajayyy/maze-utils": "^1.0.3"
}, },
"devDependencies": { "devDependencies": {
"@types/chrome": "^0.0.199", "@types/chrome": "^0.0.199",

View File

@@ -2,6 +2,7 @@ import * as CompileConfig from "../config.json";
import Config from "./config"; import Config from "./config";
import { Registration } from "./types"; import { Registration } from "./types";
import registerContentScript from 'content-scripts-register-polyfill/ponyfill.js';
// Make the config public for debugging purposes // Make the config public for debugging purposes
@@ -20,11 +21,9 @@ const popupPort: Record<string, chrome.runtime.Port> = {};
const contentScriptRegistrations = {}; const contentScriptRegistrations = {};
// Register content script if needed // Register content script if needed
if (utils.isFirefox()) { utils.wait(() => Config.config !== null).then(function() {
utils.wait(() => Config.config !== null).then(function() {
if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts(); if (Config.config.supportInvidious) utils.setupExtraSiteContentScripts();
}); });
}
function onTabUpdatedListener(tabId: number) { function onTabUpdatedListener(tabId: number) {
chrome.tabs.sendMessage(tabId, { chrome.tabs.sendMessage(tabId, {
@@ -167,7 +166,7 @@ function registerFirefoxContentScript(options: Registration) {
const oldRegistration = contentScriptRegistrations[options.id]; const oldRegistration = contentScriptRegistrations[options.id];
if (oldRegistration) oldRegistration.unregister(); if (oldRegistration) oldRegistration.unregister();
browser.contentScripts.register({ registerContentScript({
allFrames: options.allFrames, allFrames: options.allFrames,
js: options.js, js: options.js,
css: options.css, css: options.css,
@@ -181,8 +180,10 @@ function registerFirefoxContentScript(options: Registration) {
* *
*/ */
function unregisterFirefoxContentScript(id: string) { function unregisterFirefoxContentScript(id: string) {
if (contentScriptRegistrations[id]) {
contentScriptRegistrations[id].unregister(); contentScriptRegistrations[id].unregister();
delete contentScriptRegistrations[id]; delete contentScriptRegistrations[id];
}
} }
async function submitVote(type: number, UUID: string, category: string) { async function submitVote(type: number, UUID: string, category: string) {

View File

@@ -120,8 +120,8 @@ export default class Utils {
*/ */
setupExtraSitePermissions(callback: (granted: boolean) => void): void { setupExtraSitePermissions(callback: (granted: boolean) => void): void {
// Request permission // Request permission
let permissions = ["declarativeContent"]; let permissions = ["declarativeContent", "webNavigation"];
if (this.isFirefox()) permissions = []; if (this.isFirefox() && !isSafari()) permissions = [];
chrome.permissions.request({ chrome.permissions.request({
origins: this.getPermissionRegex(), origins: this.getPermissionRegex(),
@@ -145,7 +145,6 @@ export default class Utils {
* For now, it is just SB.config.invidiousInstances. * For now, it is just SB.config.invidiousInstances.
*/ */
setupExtraSiteContentScripts(): void { setupExtraSiteContentScripts(): void {
if (this.isFirefox()) {
const firefoxJS = []; const firefoxJS = [];
for (const file of this.js) { for (const file of this.js) {
firefoxJS.push({file}); firefoxJS.push({file});
@@ -169,36 +168,12 @@ export default class Utils {
} else { } else {
chrome.runtime.sendMessage(registration); chrome.runtime.sendMessage(registration);
} }
} else {
chrome.declarativeContent.onPageChanged.removeRules(["invidious"], () => {
const conditions = [];
for (const regex of this.getPermissionRegex()) {
conditions.push(new chrome.declarativeContent.PageStateMatcher({
pageUrl: { urlMatches: regex }
}));
}
// Add page rule
const rule = {
id: "invidious",
conditions,
actions: [new chrome.declarativeContent.RequestContentScript({
allFrames: true,
js: this.js,
css: this.css
})]
};
chrome.declarativeContent.onPageChanged.addRules([rule]);
});
}
} }
/** /**
* Removes the permission and content script registration. * Removes the permission and content script registration.
*/ */
removeExtraSiteRegistration(): void { removeExtraSiteRegistration(): void {
if (this.isFirefox()) {
const id = "invidious"; const id = "invidious";
if (this.backgroundScriptContainer) { if (this.backgroundScriptContainer) {
@@ -209,7 +184,8 @@ export default class Utils {
id: id id: id
}); });
} }
} else if (chrome.declarativeContent) {
if (!this.isFirefox() && chrome.declarativeContent) {
// Only if we have permission // Only if we have permission
chrome.declarativeContent.onPageChanged.removeRules(["invidious"]); chrome.declarativeContent.onPageChanged.removeRules(["invidious"]);
} }