redirect dynamically via injected content script instead of checking

each request
This commit is contained in:
2026-07-07 17:36:01 +02:00
parent 71593f6762
commit 4c9825ebd5
5 changed files with 66 additions and 18 deletions
+42 -7
View File
@@ -4,19 +4,54 @@ import { HostSettings, UrlReferer } from '@/lib/settings';
export default defineBackground(() => {
browser.runtime.onMessage.addListener(async (message, sender) => {
const messageType = message.type as BackgroundMessageType;
const messageData = message.data as BackgroundMessageData<typeof messageType>;
const type = message.type as BackgroundMessageType;
let data = message.data;
let response;
switch (messageType) {
case BackgroundMessageType.Ff2mpv:
await browser.runtime.sendNativeMessage('ff2mpv', { url: messageData!.url });
switch (type) {
case BackgroundMessageType.Ff2mpv: {
data = data as BackgroundMessageData<typeof type>;
await browser.runtime.sendNativeMessage('ff2mpv', { url: data.url });
break;
case BackgroundMessageType.RequestTabUrl:
}
case BackgroundMessageType.RequestTabUrl: {
response = sender.tab?.url ?? null;
break;
}
case BackgroundMessageType.RegisterContentScript: {
// requires "dynamic" host permissions, which is only available in mv2
if (import.meta.env.MANIFEST_VERSION === 3) break;
data = data as BackgroundMessageData<typeof type>;
const contentScript: Browser.scripting.RegisteredContentScript = {
id: 'temporary-hosts',
js: ['content-scripts/content.js'],
matches: [`*://${data.domain}/*`],
persistAcrossSessions: false,
allFrames: true,
runAt: 'document_end'
};
const contentScripts = await browser.scripting.getRegisteredContentScripts({ ids: [contentScript.id] });
if (contentScripts.length !== 0) {
const registeredContentScript = contentScripts[0];
// do not double register for same domain
if (registeredContentScript.matches!.indexOf(contentScript.matches![0]) !== -1) break;
contentScript.matches!.push(...contentScripts[0].matches!);
await browser.scripting.updateContentScripts([contentScript]);
} else {
await browser.scripting.registerContentScripts([contentScript]);
}
break;
}
default:
return;
break;
}
return response;
+1 -5
View File
@@ -3,11 +3,7 @@ import { getHost, HostMatchType, hosts, type HostMatch } from '@/lib/host';
import { FF2MPVSettings, PerDomainSettings } from '@/lib/settings';
export default defineContentScript({
matches: [
...Object.values(hosts).flatMap((h) => h.domains.map((d) => `*://${d}/*`)),
// only mv2 allows to match all urls
...(import.meta.env.MANIFEST_VERSION === 2 ? ['<all_urls>'] : [])
],
matches: [...Object.values(hosts).flatMap((h) => h.domains.map((d) => `*://${d}/*`))],
allFrames: true,
runAt: 'document_end',
main