Skip to content

Commit

Permalink
reverting response on foreground
Browse files Browse the repository at this point in the history
  • Loading branch information
sghsri committed May 5, 2024
1 parent 758398d commit b511fb6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 25 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chrome-extension-toolkit",
"version": "0.0.92",
"version": "0.0.93",
"description": "A template for creating npm packages using TypeScript and VSCode",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
27 changes: 5 additions & 22 deletions src/messaging/createMessenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,11 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {

async function sendTabMessageToAllTabs(message: Message<M>) {
const tabs = (await chrome.tabs.query({})).filter(tab => tab.id !== undefined && tab.url);
const promises: Promise<void>[] = [];
tabs.forEach(tab =>
promises.push(
new Promise((resolve, reject) => {
chrome.tabs.sendMessage(tab.id!, message, onMessageResponse(resolve, reject));
})
)
);

// and also send it using chrome.runtime.sendMessage for the extension popup or any extension page
promises.push(
new Promise((resolve, reject) => {
chrome.runtime.sendMessage(message, onMessageResponse(resolve, reject));
})
);

return Promise.all(promises);
return Promise.all([
...tabs.map(tab => chrome.tabs.sendMessage(tab.id!, message)),
chrome.runtime.sendMessage(message),
]);
}

const sender = new Proxy({} as any, {
Expand All @@ -99,12 +87,7 @@ export function createMessenger<M>(destination: 'background' | 'foreground') {
// for messages sent to the tabs, we want to send to the tabs using chrome.tabs.sendMessage,
const { tabId } = options;
if (typeof tabId === 'number') {
return chrome.tabs.sendMessage(
tabId,
message,
{ frameId: options.frameId },
onMessageResponse(resolve, reject)
);
return chrome.tabs.sendMessage(tabId, message, { frameId: options.frameId });
}
if (tabId === 'ALL') {
return sendTabMessageToAllTabs(message);
Expand Down
4 changes: 2 additions & 2 deletions src/messaging/createUseMessage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { useEffect } from 'react';
import { Message, MessageData, MessageResponse } from '..';
import { Message, MessageData } from '..';

/**
* A helper function to create a hook that can listen for messages coming through chrome.runtime.onMessage
* with e2e type safety
* @returns a hook that can be used to listen for messages from the background script.
*/
export function createUseMessage<M>() {
return function useMessage<N extends keyof M, D extends MessageData<M, N>, R extends MessageResponse<M, N>>(
return function useMessage<N extends keyof M, D extends MessageData<M, N>>(
name: N,
callback: (data: D) => void
): void {
Expand Down

1 comment on commit b511fb6

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines Statements Branches Functions
Coverage: 8%
8.03% (27/336) 12.22% (11/90) 8.43% (7/83)

Please sign in to comment.