From a642465691152f115f97466ddfc4ccaa7033712b Mon Sep 17 00:00:00 2001 From: bytedream Date: Fri, 17 Nov 2023 14:48:04 +0100 Subject: [PATCH] Make mv3 compatible --- package-lock.json | 16 +--------------- package.json | 4 +--- src/entries/background/{main.ts => mv2.ts} | 8 ++------ src/entries/background/mv3.ts | 1 + src/entries/background/shared.ts | 5 +++++ src/entries/contentScript/main.ts | 5 ++--- src/lib/match.ts | 4 ++-- src/lib/settings.ts | 11 +++++------ src/manifest.ts | 15 ++++++++++----- 9 files changed, 29 insertions(+), 40 deletions(-) rename src/entries/background/{main.ts => mv2.ts} (77%) create mode 100644 src/entries/background/mv3.ts create mode 100644 src/entries/background/shared.ts diff --git a/package-lock.json b/package-lock.json index 6708b49..495da6a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,7 +13,6 @@ "@sveltejs/vite-plugin-svelte": "^2.1.1", "@tsconfig/svelte": "^4.0.1", "@types/chrome": "^0.0.228", - "@types/webextension-polyfill": "^0.10.0", "@typescript-eslint/eslint-plugin": "^6.10.0", "@typescript-eslint/parser": "^6.10.0", "eslint": "^8.53.0", @@ -29,8 +28,7 @@ "tslib": "^2.5.0", "typescript": "^5.0.4", "vite": "~4.3.3", - "web-ext": "^7.6.2", - "webextension-polyfill": "^0.10.0" + "web-ext": "^7.6.2" } }, "node_modules/@aashutoshrathi/word-wrap": { @@ -1096,12 +1094,6 @@ "integrity": "sha512-+d+WYC1BxJ6yVOgUgzK8gWvp5qF8ssV5r4nsDcZWKRWcDQLQ619tvWAxJQYGgBrO1MnLJC7a5GtiYsAoQ47dJg==", "dev": true }, - "node_modules/@types/webextension-polyfill": { - "version": "0.10.6", - "resolved": "https://registry.npmjs.org/@types/webextension-polyfill/-/webextension-polyfill-0.10.6.tgz", - "integrity": "sha512-tHMENOd86LDISoxw/8C3KFazU1T6X5+eMhI3AS0KU9pYVLYb4kuQ2tIdrpE9aBqd2y8Ix4cVwR/Jwm2fzABTow==", - "dev": true - }, "node_modules/@types/yauzl": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.10.0.tgz", @@ -7295,12 +7287,6 @@ "node": ">= 8" } }, - "node_modules/webextension-polyfill": { - "version": "0.10.0", - "resolved": "https://registry.npmjs.org/webextension-polyfill/-/webextension-polyfill-0.10.0.tgz", - "integrity": "sha512-c5s35LgVa5tFaHhrZDnr3FpQpjj1BB+RXhLTYUxGqBVN460HkbM8TBtEqdXWbpTKfzwCcjAZVF7zXCYSKtcp9g==", - "dev": true - }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", diff --git a/package.json b/package.json index 922c0d5..f86f544 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,6 @@ "@sveltejs/vite-plugin-svelte": "^2.1.1", "@tsconfig/svelte": "^4.0.1", "@types/chrome": "^0.0.228", - "@types/webextension-polyfill": "^0.10.0", "@typescript-eslint/eslint-plugin": "^6.10.0", "@typescript-eslint/parser": "^6.10.0", "eslint": "^8.53.0", @@ -45,8 +44,7 @@ "tslib": "^2.5.0", "typescript": "^5.0.4", "vite": "~4.3.3", - "web-ext": "^7.6.2", - "webextension-polyfill": "^0.10.0" + "web-ext": "^7.6.2" }, "type": "module" } diff --git a/src/entries/background/main.ts b/src/entries/background/mv2.ts similarity index 77% rename from src/entries/background/main.ts rename to src/entries/background/mv2.ts index 389cc97..e21f743 100644 --- a/src/entries/background/main.ts +++ b/src/entries/background/mv2.ts @@ -1,13 +1,9 @@ +import './shared'; + import type { Match } from '~/lib/match'; import { storageDelete, storageGet, storageSet } from '~/lib/settings'; import { getMatch } from '~/lib/match'; -chrome.runtime.onMessage.addListener(async (message) => { - if (message.action == 'ff2mpv') { - await chrome.runtime.sendNativeMessage('ff2mpv', { url: message.url }); - } -}); - chrome.webRequest.onBeforeRedirect.addListener( async (details) => { // check if redirects origins from a previous redirect diff --git a/src/entries/background/mv3.ts b/src/entries/background/mv3.ts new file mode 100644 index 0000000..c21c06a --- /dev/null +++ b/src/entries/background/mv3.ts @@ -0,0 +1 @@ +import './shared'; diff --git a/src/entries/background/shared.ts b/src/entries/background/shared.ts new file mode 100644 index 0000000..8ad7a92 --- /dev/null +++ b/src/entries/background/shared.ts @@ -0,0 +1,5 @@ +chrome.runtime.onMessage.addListener(async (message) => { + if (message.action == 'ff2mpv') { + await chrome.runtime.sendNativeMessage('ff2mpv', { url: message.url }); + } +}); diff --git a/src/entries/contentScript/main.ts b/src/entries/contentScript/main.ts index fe5b7d7..da6c42f 100644 --- a/src/entries/contentScript/main.ts +++ b/src/entries/contentScript/main.ts @@ -1,7 +1,6 @@ import type { Match } from '~/lib/match'; import { getMatch } from '~/lib/match'; import { Other, Redirect } from '~/lib/settings'; -import browser from 'webextension-polyfill'; async function main() { let match: Match; @@ -25,7 +24,7 @@ async function main() { // send the url to the ff2mpv (https://github.com/woodruffw/ff2mpv) application if (await Other.getFf2mpv()) { - await browser.runtime.sendMessage({ action: 'ff2mpv', url: url }); + await chrome.runtime.sendMessage({ action: 'ff2mpv', url: url }); } if (match.replace && !url.includes('.m3u8')) { @@ -39,7 +38,7 @@ async function main() { document.body.append(player); } else { window.location.assign( - browser.runtime.getURL( + chrome.runtime.getURL( `src/entries/player/player.html?id=${match.id}&url=${encodeURIComponent(url)}&domain=${ window.location.hostname }` diff --git a/src/lib/match.ts b/src/lib/match.ts index 0ffe004..c6c7873 100644 --- a/src/lib/match.ts +++ b/src/lib/match.ts @@ -1,5 +1,5 @@ -import { unpack } from '~/lib/utils'; -import { Hosters } from '~/lib/settings'; +import { unpack } from './utils'; +import { Hosters } from './settings'; export enum Reliability { HIGH, diff --git a/src/lib/settings.ts b/src/lib/settings.ts index c4f2563..d05327d 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -1,6 +1,5 @@ -import browser from 'webextension-polyfill'; -import type { Match } from '~/lib/match'; -import { matches } from '~/lib/match'; +import type { Match } from './match'; +import { matches } from './match'; export const Hosters = { getDisabled: async () => { @@ -56,7 +55,7 @@ export const Other = { }; export async function storageGet(key: string, defaultValue?: T): Promise { - const entry = await browser.storage.local.get(key); + const entry = await chrome.storage.local.get(key); const value = entry[key]; return value === undefined ? defaultValue : value; } @@ -65,9 +64,9 @@ export async function storageSet(key: string, value: T) { const obj = { [key]: value }; - await browser.storage.local.set(obj); + await chrome.storage.local.set(obj); } export async function storageDelete(key: string) { - await browser.storage.local.remove(key); + await chrome.storage.local.remove(key); } diff --git a/src/manifest.ts b/src/manifest.ts index 596e8d3..dede0b6 100644 --- a/src/manifest.ts +++ b/src/manifest.ts @@ -1,4 +1,5 @@ import pkg from '../package.json'; +import { matches } from './lib/match'; const sharedManifest: Partial = { browser_specific_settings: { @@ -9,7 +10,7 @@ const sharedManifest: Partial = { content_scripts: [ { all_frames: true, - matches: [''], + matches: Object.values(matches).flatMap((m) => m.domains.map((d) => `*://${d}/*`)), js: ['src/entries/contentScript/main.ts'], run_at: 'document_end' } @@ -21,7 +22,7 @@ const sharedManifest: Partial = { 96: 'icons/stream-bypass@96px.png', 128: 'icons/stream-bypass@128px.png' }, - permissions: ['storage', 'webRequest', 'nativeMessaging', ''] + permissions: ['storage', 'nativeMessaging'] }; const browserAction = { @@ -35,18 +36,19 @@ const browserAction = { const ManifestV2 = { ...sharedManifest, background: { - scripts: ['src/entries/background/main.ts'], + scripts: ['src/entries/background/mv2.ts'], persistent: true }, + content_scripts: [{ ...sharedManifest.content_scripts[0], matches: [''] }], browser_action: browserAction, - permissions: [...sharedManifest.permissions] + permissions: [...sharedManifest.permissions, 'webRequest', ''] }; const ManifestV3 = { ...sharedManifest, action: browserAction, background: { - service_worker: 'src/entries/background/main.ts' + service_worker: 'src/entries/background/mv3.ts' } }; @@ -71,6 +73,9 @@ export function getManifest( if (manifestVersion === 3) { return { ...manifest, + // just like all the adblockers which are unable to fully work under MV3, we need access to every website + // the user enters in order to work correctly, which is forbidden when using MV3 + author: `${manifest.author} Lite`, ...ManifestV3, manifest_version: manifestVersion };