diff --git a/package.json b/package.json index 9e4e05c..a88746a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/messaging/createMessenger.ts b/src/messaging/createMessenger.ts index 881f7bf..b9bd86d 100644 --- a/src/messaging/createMessenger.ts +++ b/src/messaging/createMessenger.ts @@ -64,23 +64,11 @@ export function createMessenger(destination: 'background' | 'foreground') { async function sendTabMessageToAllTabs(message: Message) { const tabs = (await chrome.tabs.query({})).filter(tab => tab.id !== undefined && tab.url); - const promises: Promise[] = []; - 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, { @@ -99,12 +87,7 @@ export function createMessenger(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); diff --git a/src/messaging/createUseMessage.ts b/src/messaging/createUseMessage.ts index 2615b32..b63b42c 100644 --- a/src/messaging/createUseMessage.ts +++ b/src/messaging/createUseMessage.ts @@ -1,5 +1,5 @@ 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 @@ -7,7 +7,7 @@ import { Message, MessageData, MessageResponse } from '..'; * @returns a hook that can be used to listen for messages from the background script. */ export function createUseMessage() { - return function useMessage, R extends MessageResponse>( + return function useMessage>( name: N, callback: (data: D) => void ): void {