From e437d5436a5874031a836740e4ae71972081340d Mon Sep 17 00:00:00 2001 From: bytedream Date: Tue, 7 Jul 2026 13:26:33 +0200 Subject: [PATCH] fix per domain disable not working if host is in iframe --- src/entrypoints/background/index.ts | 20 +++++++-- src/entrypoints/content/index.ts | 15 ++++++- src/entrypoints/player/player.ts | 8 ++-- src/entrypoints/popup/pages/main/Main.svelte | 27 ++++-------- .../popup/pages/site-config/SiteConfig.svelte | 8 ++-- src/lib/communication.ts | 44 ++++++++++++++----- 6 files changed, 82 insertions(+), 40 deletions(-) diff --git a/src/entrypoints/background/index.ts b/src/entrypoints/background/index.ts index 43f2f84..3c98631 100644 --- a/src/entrypoints/background/index.ts +++ b/src/entrypoints/background/index.ts @@ -1,11 +1,25 @@ +import { BackgroundMessageData, BackgroundMessageType } from '@/lib/communication'; import { getHost } from '@/lib/host'; import { HostSettings, UrlReferer } from '@/lib/settings'; export default defineBackground(() => { - browser.runtime.onMessage.addListener(async (message) => { - if (message.action == 'ff2mpv') { - await browser.runtime.sendNativeMessage('ff2mpv', { url: message.url }); + browser.runtime.onMessage.addListener(async (message, sender) => { + const messageType = message.type as BackgroundMessageType; + const messageData = message.data as BackgroundMessageData; + + 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 diff --git a/src/entrypoints/content/index.ts b/src/entrypoints/content/index.ts index 407419f..4d3a746 100644 --- a/src/entrypoints/content/index.ts +++ b/src/entrypoints/content/index.ts @@ -1,5 +1,6 @@ +import { BackgroundMessageType, sendBackgroundMessage } from '@/lib/communication'; import { getHost, HostMatchType, hosts, type HostMatch } from '@/lib/host'; -import { FF2MPVSettings } from '@/lib/settings'; +import { FF2MPVSettings, PerDomainSettings } from '@/lib/settings'; export default defineContentScript({ matches: [ @@ -16,6 +17,18 @@ async function main() { const host = await getHost(window.location.host); 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; for (const regex of host.regex) { if ((re = document.body.innerHTML.match(regex)) !== null) { diff --git a/src/entrypoints/player/player.ts b/src/entrypoints/player/player.ts index 55621c6..e6578b3 100644 --- a/src/entrypoints/player/player.ts +++ b/src/entrypoints/player/player.ts @@ -1,5 +1,5 @@ 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 { UrlReferer } from '@/lib/settings'; @@ -56,7 +56,7 @@ export async function play(videoElem: HTMLVideoElement) { function initCommunication(id: string, url: string, domain: string) { const notifyActiveMatch = () => - sendMessage(MessageType.NotifyActiveMatch, { + sendTabBroadcast(TabMessageType.NotifyActiveMatch, { id: id, url: url, 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 // data - const cancel = listenMessages((type) => { - if (type !== MessageType.RequestActiveMatch) return; + const cancel = listenTabMessages((type) => { + if (type !== TabMessageType.RequestActiveMatch) return; notifyActiveMatch(); }); window.onbeforeunload = cancel; diff --git a/src/entrypoints/popup/pages/main/Main.svelte b/src/entrypoints/popup/pages/main/Main.svelte index 54c2eff..18ca8c3 100644 --- a/src/entrypoints/popup/pages/main/Main.svelte +++ b/src/entrypoints/popup/pages/main/Main.svelte @@ -8,7 +8,7 @@ import CurrentSite from '@/entrypoints/popup/pages/main/CurrentSite.svelte'; import Header from '@/entrypoints/popup/pages/main/Header.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 { HostSettings } from '@/lib/settings'; @@ -27,31 +27,22 @@ HostSettings.getAllHostsDisabled().then((val) => (allHostsDisabled = val)); /* lifecycle */ - const cancel = listenMessages((type, data) => { - if (type !== MessageType.NotifyActiveMatch) return; + const cancel = listenTabMessages((type, data) => { + if (type !== TabMessageType.NotifyActiveMatch) return; const match = { host: hosts.find((host) => host.id === data.id)!, url: data.url, domain: data.domain }; currentMatch = match; - currentDomain = match.domain; }); - sendMessageToActiveTab(MessageType.RequestActiveMatch, undefined); + sendTabMessageToActiveTab(TabMessageType.RequestActiveMatch, undefined); - browser.tabs - .query({ active: true, currentWindow: true }) - .then((tabs) => { - if (currentDomain) return; - const url = tabs[0]?.url; - if (!url) return; - try { - currentDomain = new URL(url).hostname; - } catch { - /* ignore */ - } - }) - .catch(() => {}); + browser.tabs.query({ active: true, currentWindow: true }).then((tabs) => { + const url = tabs[0]?.url; + if (!url) return; + currentDomain = new URL(url).hostname; + }); onDestroy(cancel); diff --git a/src/entrypoints/popup/pages/site-config/SiteConfig.svelte b/src/entrypoints/popup/pages/site-config/SiteConfig.svelte index b27852e..1d1cf50 100644 --- a/src/entrypoints/popup/pages/site-config/SiteConfig.svelte +++ b/src/entrypoints/popup/pages/site-config/SiteConfig.svelte @@ -3,7 +3,7 @@ import Divider from '@/entrypoints/popup/components/Divider.svelte'; import Header from '@/entrypoints/popup/pages/site-config/Header.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'; /* types */ @@ -18,8 +18,8 @@ let currentDomain = $state(null); /* lifecycle */ - const cancel = listenMessages((type, data) => { - if (type !== MessageType.NotifyActiveMatch) return; + const cancel = listenTabMessages((type, data) => { + if (type !== TabMessageType.NotifyActiveMatch) return; const match = { host: hosts.find((host) => host.id === data.id)!, url: data.url, @@ -28,7 +28,7 @@ currentMatch = match; currentDomain = match.domain; }); - sendMessageToActiveTab(MessageType.RequestActiveMatch, undefined); + sendTabMessageToActiveTab(TabMessageType.RequestActiveMatch, undefined); browser.tabs .query({ active: true, currentWindow: true }) diff --git a/src/lib/communication.ts b/src/lib/communication.ts index bb1a2f5..7daae85 100644 --- a/src/lib/communication.ts +++ b/src/lib/communication.ts @@ -1,36 +1,60 @@ -export enum MessageType { +// --- tab communication --- // +export enum TabMessageType { RequestActiveMatch, NotifyActiveMatch } -export type MessageData = { - [MessageType.RequestActiveMatch]: undefined; - [MessageType.NotifyActiveMatch]: { +export type TabMessageData = { + [TabMessageType.RequestActiveMatch]: undefined; + [TabMessageType.NotifyActiveMatch]: { id: string; url: string; domain: string; }; }[T]; -export async function sendMessage(message: T, data: MessageData) { +export async function sendTabBroadcast(message: T, data: TabMessageData) { await browser.runtime.sendMessage({ type: message, data: data }); } -export async function sendMessageToActiveTab(message: T, data: MessageData) { +export async function sendTabMessageToActiveTab(message: T, data: TabMessageData) { const tabs = await browser.tabs.query({ active: true, currentWindow: true }); await browser.tabs.sendMessage(tabs[0].id!, { type: message, data: data }).catch(() => {}); } -export function listenMessages(listener: (type: MessageType, data: any) => void): () => void { - const callback = (callbackData: { type: MessageType; data: any }) => { +export function listenTabMessages(listener: (type: TabMessageType, data: any) => void): () => void { + const callback = (callbackData: { type: TabMessageType; data: any }) => { const { type, data } = callbackData; listener(type, data); }; browser.runtime.onMessage.addListener(callback); + return () => browser.runtime.onMessage.removeListener(callback); +} - return () => { - browser.runtime.onMessage.removeListener(callback); +// --- background script communication --- // +export enum BackgroundMessageType { + Ff2mpv, + RequestTabUrl +} + +export type BackgroundMessageData = { + [BackgroundMessageType.Ff2mpv]: { + url: string; }; + [BackgroundMessageType.RequestTabUrl]: undefined; +}[T]; + +export type BackgroundMessageReply = { + [BackgroundMessageType.Ff2mpv]: undefined; + [BackgroundMessageType.RequestTabUrl]: string | null; +}[T]; + +export async function sendBackgroundMessage( + message: T, + data: BackgroundMessageData +): Promise> { + const response = await browser.runtime.sendMessage({ type: message, data: data }); + return response; }