Skip to content

Commit

Permalink
Make mv3 compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
bytedream committed Nov 17, 2023
1 parent fffe236 commit a642465
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 40 deletions.
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/entries/background/mv3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './shared';
5 changes: 5 additions & 0 deletions src/entries/background/shared.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
chrome.runtime.onMessage.addListener(async (message) => {
if (message.action == 'ff2mpv') {
await chrome.runtime.sendNativeMessage('ff2mpv', { url: message.url });
}
});
5 changes: 2 additions & 3 deletions src/entries/contentScript/main.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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')) {
Expand All @@ -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
}`
Expand Down
4 changes: 2 additions & 2 deletions src/lib/match.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
11 changes: 5 additions & 6 deletions src/lib/settings.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down Expand Up @@ -56,7 +55,7 @@ export const Other = {
};

export async function storageGet<T>(key: string, defaultValue?: T): Promise<T | undefined> {
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;
}
Expand All @@ -65,9 +64,9 @@ export async function storageSet<T>(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);
}
15 changes: 10 additions & 5 deletions src/manifest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pkg from '../package.json';
import { matches } from './lib/match';

const sharedManifest: Partial<chrome.runtime.ManifestBase> = {
browser_specific_settings: {
Expand All @@ -9,7 +10,7 @@ const sharedManifest: Partial<chrome.runtime.ManifestBase> = {
content_scripts: [
{
all_frames: true,
matches: ['<all_urls>'],
matches: Object.values(matches).flatMap((m) => m.domains.map((d) => `*://${d}/*`)),
js: ['src/entries/contentScript/main.ts'],
run_at: 'document_end'
}
Expand All @@ -21,7 +22,7 @@ const sharedManifest: Partial<chrome.runtime.ManifestBase> = {
96: 'icons/stream-bypass@96px.png',
128: 'icons/stream-bypass@128px.png'
},
permissions: ['storage', 'webRequest', 'nativeMessaging', '<all_urls>']
permissions: ['storage', 'nativeMessaging']
};

const browserAction = {
Expand All @@ -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: ['<all_urls>'] }],
browser_action: browserAction,
permissions: [...sharedManifest.permissions]
permissions: [...sharedManifest.permissions, 'webRequest', '<all_urls>']
};

const ManifestV3 = {
...sharedManifest,
action: browserAction,
background: {
service_worker: 'src/entries/background/main.ts'
service_worker: 'src/entries/background/mv3.ts'
}
};

Expand All @@ -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
};
Expand Down

0 comments on commit a642465

Please sign in to comment.