Skip to content

Commit

Permalink
Add support for allFrames (#209)
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante authored Feb 12, 2024
1 parent 35287ef commit 0bd4d71
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
8 changes: 5 additions & 3 deletions source/sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,9 +313,11 @@ function messenger<
return browser.tabs.sendMessage(
tabId,
makeMessage(type, args, target, options),
{
frameId,
}
frameId === "allFrames"
? {}
: {
frameId,
}
);
}
) as ReturnValue;
Expand Down
10 changes: 10 additions & 0 deletions source/test/contentscript/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export type MessengerMessage = Message & {

export interface AnyTarget {
tabId?: number | "this";
frameId?: number;
frameId?: number | "allFrames";
page?: string;
}

Expand All @@ -114,7 +114,7 @@ export interface KnownTarget {

export interface Target {
tabId: number;
frameId?: number;
frameId?: number | "allFrames";
}

export interface PageTarget {
Expand Down

0 comments on commit 0bd4d71

Please sign in to comment.