fix per domain disable not working if host is in iframe

This commit is contained in:
2026-07-07 13:26:33 +02:00
parent c09cf1188d
commit e437d5436a
6 changed files with 82 additions and 40 deletions
+17 -3
View File
@@ -1,11 +1,25 @@
import { BackgroundMessageData, BackgroundMessageType } from '@/lib/communication';
import { getHost } from '@/lib/host'; import { getHost } from '@/lib/host';
import { HostSettings, UrlReferer } from '@/lib/settings'; import { HostSettings, UrlReferer } from '@/lib/settings';
export default defineBackground(() => { export default defineBackground(() => {
browser.runtime.onMessage.addListener(async (message) => { browser.runtime.onMessage.addListener(async (message, sender) => {
if (message.action == 'ff2mpv') { const messageType = message.type as BackgroundMessageType;
await browser.runtime.sendNativeMessage('ff2mpv', { url: message.url }); const messageData = message.data as BackgroundMessageData<typeof messageType>;
let response;
switch (messageType) {
case BackgroundMessageType.Ff2mpv:
await browser.runtime.sendNativeMessage('ff2mpv', { url: messageData!.url });
break;
case BackgroundMessageType.RequestTabUrl:
response = sender.tab?.url ?? null;
break;
default:
return;
} }
return response;
}); });
// the following listener is only available in mv2 // the following listener is only available in mv2
+14 -1
View File
@@ -1,5 +1,6 @@
import { BackgroundMessageType, sendBackgroundMessage } from '@/lib/communication';
import { getHost, HostMatchType, hosts, type HostMatch } from '@/lib/host'; import { getHost, HostMatchType, hosts, type HostMatch } from '@/lib/host';
import { FF2MPVSettings } from '@/lib/settings'; import { FF2MPVSettings, PerDomainSettings } from '@/lib/settings';
export default defineContentScript({ export default defineContentScript({
matches: [ matches: [
@@ -16,6 +17,18 @@ async function main() {
const host = await getHost(window.location.host); const host = await getHost(window.location.host);
if (!host) return; if (!host) return;
// if current windows is an iframe, check if the parent has any disabled hosts
if (window !== window.top) {
const tabUrl = await sendBackgroundMessage(BackgroundMessageType.RequestTabUrl, undefined);
if (tabUrl) {
const domain = new URL(tabUrl).host;
const perDomainSetting = await PerDomainSettings.get(domain);
if (perDomainSetting.allDisabled) return;
else if (perDomainSetting.disabledHostIds.indexOf(host.id) !== -1) return;
}
}
let re = null; let re = null;
for (const regex of host.regex) { for (const regex of host.regex) {
if ((re = document.body.innerHTML.match(regex)) !== null) { if ((re = document.body.innerHTML.match(regex)) !== null) {
+4 -4
View File
@@ -1,5 +1,5 @@
import Hls from 'hls.js'; import Hls from 'hls.js';
import { listenMessages, MessageType, sendMessage } from '@/lib/communication'; import { listenTabMessages, sendTabBroadcast, TabMessageType } from '@/lib/communication';
import { HostMatchType, hosts } from '@/lib/host'; import { HostMatchType, hosts } from '@/lib/host';
import { UrlReferer } from '@/lib/settings'; import { UrlReferer } from '@/lib/settings';
@@ -56,7 +56,7 @@ export async function play(videoElem: HTMLVideoElement) {
function initCommunication(id: string, url: string, domain: string) { function initCommunication(id: string, url: string, domain: string) {
const notifyActiveMatch = () => const notifyActiveMatch = () =>
sendMessage(MessageType.NotifyActiveMatch, { sendTabBroadcast(TabMessageType.NotifyActiveMatch, {
id: id, id: id,
url: url, url: url,
domain: domain domain: domain
@@ -67,8 +67,8 @@ function initCommunication(id: string, url: string, domain: string) {
// if an extension popup is opened, the listener will recognize it's active match request and send the match/player // if an extension popup is opened, the listener will recognize it's active match request and send the match/player
// data // data
const cancel = listenMessages((type) => { const cancel = listenTabMessages((type) => {
if (type !== MessageType.RequestActiveMatch) return; if (type !== TabMessageType.RequestActiveMatch) return;
notifyActiveMatch(); notifyActiveMatch();
}); });
window.onbeforeunload = cancel; window.onbeforeunload = cancel;
+6 -15
View File
@@ -8,7 +8,7 @@
import CurrentSite from '@/entrypoints/popup/pages/main/CurrentSite.svelte'; import CurrentSite from '@/entrypoints/popup/pages/main/CurrentSite.svelte';
import Header from '@/entrypoints/popup/pages/main/Header.svelte'; import Header from '@/entrypoints/popup/pages/main/Header.svelte';
import Match from '@/entrypoints/popup/pages/main/Match.svelte'; import Match from '@/entrypoints/popup/pages/main/Match.svelte';
import { listenMessages, MessageType, sendMessageToActiveTab } from '@/lib/communication'; import { listenTabMessages, sendTabMessageToActiveTab, TabMessageType } from '@/lib/communication';
import { hosts, type Host } from '@/lib/host'; import { hosts, type Host } from '@/lib/host';
import { HostSettings } from '@/lib/settings'; import { HostSettings } from '@/lib/settings';
@@ -27,31 +27,22 @@
HostSettings.getAllHostsDisabled().then((val) => (allHostsDisabled = val)); HostSettings.getAllHostsDisabled().then((val) => (allHostsDisabled = val));
/* lifecycle */ /* lifecycle */
const cancel = listenMessages((type, data) => { const cancel = listenTabMessages((type, data) => {
if (type !== MessageType.NotifyActiveMatch) return; if (type !== TabMessageType.NotifyActiveMatch) return;
const match = { const match = {
host: hosts.find((host) => host.id === data.id)!, host: hosts.find((host) => host.id === data.id)!,
url: data.url, url: data.url,
domain: data.domain domain: data.domain
}; };
currentMatch = match; currentMatch = match;
currentDomain = match.domain;
}); });
sendMessageToActiveTab(MessageType.RequestActiveMatch, undefined); sendTabMessageToActiveTab(TabMessageType.RequestActiveMatch, undefined);
browser.tabs browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
.query({ active: true, currentWindow: true })
.then((tabs) => {
if (currentDomain) return;
const url = tabs[0]?.url; const url = tabs[0]?.url;
if (!url) return; if (!url) return;
try {
currentDomain = new URL(url).hostname; currentDomain = new URL(url).hostname;
} catch { });
/* ignore */
}
})
.catch(() => {});
onDestroy(cancel); onDestroy(cancel);
</script> </script>
@@ -3,7 +3,7 @@
import Divider from '@/entrypoints/popup/components/Divider.svelte'; import Divider from '@/entrypoints/popup/components/Divider.svelte';
import Header from '@/entrypoints/popup/pages/site-config/Header.svelte'; import Header from '@/entrypoints/popup/pages/site-config/Header.svelte';
import PerSiteHostsList from '@/entrypoints/popup/pages/site-config/PerSiteHostsList.svelte'; import PerSiteHostsList from '@/entrypoints/popup/pages/site-config/PerSiteHostsList.svelte';
import { listenMessages, MessageType, sendMessageToActiveTab } from '@/lib/communication'; import { listenTabMessages, sendTabMessageToActiveTab, TabMessageType } from '@/lib/communication';
import { hosts, type Host } from '@/lib/host'; import { hosts, type Host } from '@/lib/host';
/* types */ /* types */
@@ -18,8 +18,8 @@
let currentDomain = $state<string | null>(null); let currentDomain = $state<string | null>(null);
/* lifecycle */ /* lifecycle */
const cancel = listenMessages((type, data) => { const cancel = listenTabMessages((type, data) => {
if (type !== MessageType.NotifyActiveMatch) return; if (type !== TabMessageType.NotifyActiveMatch) return;
const match = { const match = {
host: hosts.find((host) => host.id === data.id)!, host: hosts.find((host) => host.id === data.id)!,
url: data.url, url: data.url,
@@ -28,7 +28,7 @@
currentMatch = match; currentMatch = match;
currentDomain = match.domain; currentDomain = match.domain;
}); });
sendMessageToActiveTab(MessageType.RequestActiveMatch, undefined); sendTabMessageToActiveTab(TabMessageType.RequestActiveMatch, undefined);
browser.tabs browser.tabs
.query({ active: true, currentWindow: true }) .query({ active: true, currentWindow: true })
+34 -10
View File
@@ -1,36 +1,60 @@
export enum MessageType { // --- tab communication --- //
export enum TabMessageType {
RequestActiveMatch, RequestActiveMatch,
NotifyActiveMatch NotifyActiveMatch
} }
export type MessageData<T extends MessageType> = { export type TabMessageData<T extends TabMessageType> = {
[MessageType.RequestActiveMatch]: undefined; [TabMessageType.RequestActiveMatch]: undefined;
[MessageType.NotifyActiveMatch]: { [TabMessageType.NotifyActiveMatch]: {
id: string; id: string;
url: string; url: string;
domain: string; domain: string;
}; };
}[T]; }[T];
export async function sendMessage<T extends MessageType>(message: T, data: MessageData<T>) { export async function sendTabBroadcast<T extends TabMessageType>(message: T, data: TabMessageData<T>) {
await browser.runtime.sendMessage({ type: message, data: data }); await browser.runtime.sendMessage({ type: message, data: data });
} }
export async function sendMessageToActiveTab<T extends MessageType>(message: T, data: MessageData<T>) { export async function sendTabMessageToActiveTab<T extends TabMessageType>(message: T, data: TabMessageData<T>) {
const tabs = await browser.tabs.query({ active: true, currentWindow: true }); const tabs = await browser.tabs.query({ active: true, currentWindow: true });
await browser.tabs.sendMessage(tabs[0].id!, { type: message, data: data }).catch(() => {}); await browser.tabs.sendMessage(tabs[0].id!, { type: message, data: data }).catch(() => {});
} }
export function listenMessages(listener: (type: MessageType, data: any) => void): () => void { export function listenTabMessages(listener: (type: TabMessageType, data: any) => void): () => void {
const callback = (callbackData: { type: MessageType; data: any }) => { const callback = (callbackData: { type: TabMessageType; data: any }) => {
const { type, data } = callbackData; const { type, data } = callbackData;
listener(type, data); listener(type, data);
}; };
browser.runtime.onMessage.addListener(callback); browser.runtime.onMessage.addListener(callback);
return () => browser.runtime.onMessage.removeListener(callback);
}
return () => { // --- background script communication --- //
browser.runtime.onMessage.removeListener(callback); export enum BackgroundMessageType {
Ff2mpv,
RequestTabUrl
}
export type BackgroundMessageData<T extends BackgroundMessageType> = {
[BackgroundMessageType.Ff2mpv]: {
url: string;
}; };
[BackgroundMessageType.RequestTabUrl]: undefined;
}[T];
export type BackgroundMessageReply<T extends BackgroundMessageType> = {
[BackgroundMessageType.Ff2mpv]: undefined;
[BackgroundMessageType.RequestTabUrl]: string | null;
}[T];
export async function sendBackgroundMessage<T extends BackgroundMessageType>(
message: T,
data: BackgroundMessageData<T>
): Promise<BackgroundMessageReply<T>> {
const response = await browser.runtime.sendMessage({ type: message, data: data });
return response;
} }