webext-messenger
Advanced tools
Comparing version 0.9.3 to 0.10.0
@@ -17,10 +17,6 @@ import pRetry from "p-retry"; | ||
const handlers = new Map(); | ||
async function handleMessage(message, sender) { | ||
const handler = handlers.get(message.type); | ||
if (!handler) { | ||
throw new Error("No handler registered for " + message.type); | ||
} | ||
async function handleCall(message, sender, call) { | ||
console.debug(`Messenger:`, message.type, message.args, "from", { sender }); | ||
// The handler could actually be a synchronous function | ||
const response = await Promise.resolve(handler.call(sender, ...message.args)).then((value) => ({ value }), (error) => ({ | ||
const response = await Promise.resolve(call).then((value) => ({ value }), (error) => ({ | ||
// Errors must be serialized because the stacktraces are currently lost on Chrome and | ||
@@ -33,2 +29,13 @@ // https://github.com/mozilla/webextension-polyfill/issues/210 | ||
} | ||
async function handleMessage(message, sender) { | ||
if (message.target) { | ||
const publicMethod = getContentScriptMethod(message.type); | ||
return handleCall(message, sender, publicMethod(message.target, ...message.args)); | ||
} | ||
const handler = handlers.get(message.type); | ||
if (handler) { | ||
return handleCall(message, sender, handler.apply(sender, message.args)); | ||
} | ||
throw new Error("No handler registered for " + message.type); | ||
} | ||
// Do not turn this into an `async` function; Notifications must turn `void` | ||
@@ -72,3 +79,3 @@ function manageConnection(type, options, sendMessage) { | ||
} | ||
function makeMessage(type, args) { | ||
function makeMessage(type, args, target) { | ||
return { | ||
@@ -78,2 +85,3 @@ __webext_messenger__, | ||
args, | ||
target, | ||
}; | ||
@@ -83,8 +91,11 @@ } | ||
const publicMethod = (target, ...args) => { | ||
const sendMessage = async () => { | ||
var _a; | ||
return browser.tabs.sendMessage(target.tabId, makeMessage(type, args), | ||
// `frameId` must be specified. If missing, the message would be sent to every frame | ||
{ frameId: (_a = target.frameId) !== null && _a !== void 0 ? _a : 0 }); | ||
}; | ||
// eslint-disable-next-line no-negated-condition -- Looks better | ||
const sendMessage = !browser.tabs | ||
? async () => browser.runtime.sendMessage(makeMessage(type, args, target)) | ||
: async () => { | ||
var _a; | ||
return browser.tabs.sendMessage(target.tabId, makeMessage(type, args), | ||
// `frameId` must be specified. If missing, the message is sent to every frame | ||
{ frameId: (_a = target.frameId) !== null && _a !== void 0 ? _a : 0 }); | ||
}; | ||
return manageConnection(type, options, sendMessage); | ||
@@ -91,0 +102,0 @@ }; |
{ | ||
"name": "webext-messenger", | ||
"version": "0.9.3", | ||
"version": "0.10.0", | ||
"description": "Browser Extension component messaging framework", | ||
@@ -114,3 +114,4 @@ "keywords": [], | ||
"startUrl": [ | ||
"https://legiblenews.com/" | ||
"https://legiblenews.com/", | ||
"https://rawtext.club/" | ||
] | ||
@@ -117,0 +118,0 @@ } |
11610
161