From 98a4a076bc1d8b2d3a9b64355b9aaf84e2a4b685 Mon Sep 17 00:00:00 2001 From: Ajay Date: Wed, 19 Jun 2024 23:14:27 +0530 Subject: [PATCH] Support invidious in mv3 --- maze-utils | 2 +- src/background.ts | 13 ++++++++++--- src/content.ts | 2 +- src/globals.d.ts | 15 --------------- src/utils.ts | 36 +++++++++++++++++------------------- src/utils/logger.ts | 4 ++++ webpack/webpack.manifest.js | 3 +++ 7 files changed, 36 insertions(+), 39 deletions(-) diff --git a/maze-utils b/maze-utils index 6212c29b..42888f80 160000 --- a/maze-utils +++ b/maze-utils @@ -1 +1 @@ -Subproject commit 6212c29b9e86c9b06835f44882514fde454b357e +Subproject commit 42888f800e38213f5e5d79dda953e790ada502eb diff --git a/src/background.ts b/src/background.ts index e14f8e48..a5a70219 100644 --- a/src/background.ts +++ b/src/background.ts @@ -9,7 +9,7 @@ import { generateUserID } from "../maze-utils/src/setup"; import Utils from "./utils"; import { getExtensionIdsToImportFrom } from "./utils/crossExtension"; -import { isFirefoxOrSafari } from "../maze-utils/src"; +import { isFirefoxOrSafari, waitFor } from "../maze-utils/src"; import { injectUpdatedScripts } from "../maze-utils/src/cleanup"; import { logWarn } from "./utils/logger"; import { chromeP } from "../maze-utils/src/browserApi"; @@ -138,9 +138,16 @@ chrome.runtime.onInstalled.addListener(function () { } }, 1500); - // Only do this once the old version understands how to clean itself up - if (!isFirefoxOrSafari() && chrome.runtime.getManifest().version !== "5.4.13") { + if (!isFirefoxOrSafari()) { injectUpdatedScripts().catch(logWarn); + + waitFor(() => Config.isReady()).then(() => { + if (Config.config.supportInvidious) { + injectUpdatedScripts([ + utils.getExtraSiteRegistration() + ]) + } + }).catch(logWarn); } }); diff --git a/src/content.ts b/src/content.ts index 226caba7..d22628a1 100644 --- a/src/content.ts +++ b/src/content.ts @@ -127,7 +127,7 @@ setupVideoModule({ updateVisibilityOfPlayerControlsButton(); }, resetValues, - documentScript + documentScript: chrome.runtime.getManifest().manifest_version === 2 ? documentScript : undefined }, () => Config); setupThumbnailListener(); diff --git a/src/globals.d.ts b/src/globals.d.ts index aaaab851..b420496a 100644 --- a/src/globals.d.ts +++ b/src/globals.d.ts @@ -1,19 +1,4 @@ import { SBObject } from "./config"; declare global { interface Window { SB: SBObject } - // Remove this once the API becomes stable and types are shipped in @types/chrome - namespace chrome { - namespace declarativeContent { - export interface RequestContentScriptOptions { - allFrames?: boolean; - css?: string[]; - instanceType?: "declarativeContent.RequestContentScript"; - js?: string[]; - matchAboutBlanck?: boolean; - } - export class RequestContentScript { - constructor(options: RequestContentScriptOptions); - } - } - } } diff --git a/src/utils.ts b/src/utils.ts index 7c3f985e..610e93c6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -46,10 +46,7 @@ export default class Utils { */ setupExtraSitePermissions(callback: (granted: boolean) => void): void { const permissions = []; - if (!isFirefoxOrSafari()) { - permissions.push("declarativeContent"); - } - if (!isFirefoxOrSafari() || isSafari()) { + if (isSafari()) { permissions.push("webNavigation"); } @@ -67,6 +64,17 @@ export default class Utils { }); } + getExtraSiteRegistration(): Registration { + return { + message: "registerContentScript", + id: "invidious", + allFrames: true, + js: this.js, + css: this.css, + matches: this.getPermissionRegex() + }; + } + /** * Registers the content scripts for the extra sites. * Will use a different method depending on the browser. @@ -75,14 +83,7 @@ export default class Utils { * For now, it is just SB.config.invidiousInstances. */ setupExtraSiteContentScripts(): void { - const registration: Registration = { - message: "registerContentScript", - id: "invidious", - allFrames: true, - js: this.js, - css: this.css, - matches: this.getPermissionRegex() - }; + const registration = this.getExtraSiteRegistration(); if (this.backgroundScriptContainer) { this.backgroundScriptContainer.registerFirefoxContentScript(registration); @@ -106,11 +107,6 @@ export default class Utils { }); } - if (!isFirefoxOrSafari() && chrome.declarativeContent) { - // Only if we have permission - chrome.declarativeContent.onPageChanged.removeRules(["invidious"]); - } - chrome.permissions.remove({ origins: this.getPermissionRegex() }); @@ -135,8 +131,10 @@ export default class Utils { containsInvidiousPermission(): Promise { return new Promise((resolve) => { - let permissions = ["declarativeContent"]; - if (isFirefoxOrSafari()) permissions = []; + const permissions = []; + if (isSafari()) { + permissions.push("webNavigation"); + } chrome.permissions.contains({ origins: this.getPermissionRegex(), diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 76a5fb6b..8e9928b5 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -8,11 +8,15 @@ if (typeof (window) !== "undefined") { export function logDebug(message: string) { if (typeof (window) !== "undefined") { window["SBLogs"].debug.push(`[${new Date().toISOString()}] ${message}`); + } else { + console.log(`[${new Date().toISOString()}] ${message}`) } } export function logWarn(message: string) { if (typeof (window) !== "undefined") { window["SBLogs"].warn.push(`[${new Date().toISOString()}] ${message}`); + } else { + console.warn(`[${new Date().toISOString()}] ${message}`) } } \ No newline at end of file diff --git a/webpack/webpack.manifest.js b/webpack/webpack.manifest.js index 3041fe6d..a7a5ffcb 100644 --- a/webpack/webpack.manifest.js +++ b/webpack/webpack.manifest.js @@ -11,6 +11,7 @@ const chromeManifestExtra = require("../manifest/chrome-manifest-extra.json"); const safariManifestExtra = require("../manifest/safari-manifest-extra.json"); const betaManifestExtra = require("../manifest/beta-manifest-extra.json"); const firefoxBetaManifestExtra = require("../manifest/firefox-beta-manifest-extra.json"); +const manifestV2ManifestExtra = require("../manifest/manifest-v2-extra.json"); // schema for options object const schema = { @@ -41,12 +42,14 @@ class BuildManifest { // Add missing manifest elements if (this.options.browser.toLowerCase() === "firefox") { + mergeObjects(manifest, manifestV2ManifestExtra); mergeObjects(manifest, firefoxManifestExtra); } else if (this.options.browser.toLowerCase() === "chrome" || this.options.browser.toLowerCase() === "chromium" || this.options.browser.toLowerCase() === "edge") { mergeObjects(manifest, chromeManifestExtra); } else if (this.options.browser.toLowerCase() === "safari") { + mergeObjects(manifest, manifestV2ManifestExtra); mergeObjects(manifest, safariManifestExtra); }