From 0bd4d718ab6c4692974a9a712a651b610ec18257 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Mon, 12 Feb 2024 16:06:02 +0800 Subject: [PATCH] Add support for `allFrames` (#209) --- source/sender.ts | 8 +++++--- source/test/contentscript/api.test.ts | 10 ++++++++++ source/types.ts | 4 ++-- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/source/sender.ts b/source/sender.ts index cae2091..0c1d402 100644 --- a/source/sender.ts +++ b/source/sender.ts @@ -313,9 +313,11 @@ function messenger< return browser.tabs.sendMessage( tabId, makeMessage(type, args, target, options), - { - frameId, - } + frameId === "allFrames" + ? {} + : { + frameId, + } ); } ) as ReturnValue; diff --git a/source/test/contentscript/api.test.ts b/source/test/contentscript/api.test.ts index bd01d87..87a5066 100644 --- a/source/test/contentscript/api.test.ts +++ b/source/test/contentscript/api.test.ts @@ -431,6 +431,16 @@ function additionalTests() { await closeTab(await tabIdPromise); }); + + test("`frameId: allFrames` messages every frame in tab, receives only one response", async (t) => { + // `chrome.*.sendMessage` can exclusively receive one response + const tabId = await openTab( + "https://ephiframe.vercel.app/No-content-script-in-top-frame?iframe=https://fregante.github.io/pixiebrix-testing-ground/Will-receive-CS-calls/Receives-with-allFrames-on-tab" + ); + + const title = await getPageTitle({ tabId, frameId: "allFrames" }); + t.equal(title, "Receives with allFrames on tab"); + }); } void testEveryTarget(); diff --git a/source/types.ts b/source/types.ts index 3a314e3..5eb14cf 100644 --- a/source/types.ts +++ b/source/types.ts @@ -92,7 +92,7 @@ export type MessengerMessage = Message & { export interface AnyTarget { tabId?: number | "this"; - frameId?: number; + frameId?: number | "allFrames"; page?: string; } @@ -114,7 +114,7 @@ export interface KnownTarget { export interface Target { tabId: number; - frameId?: number; + frameId?: number | "allFrames"; } export interface PageTarget {