From caa16365c1d61a48d23ef4401d004b492dc3ea3b Mon Sep 17 00:00:00 2001 From: hackademix Date: Tue, 1 Oct 2019 22:39:49 +0200 Subject: Simplified, less noisy and more resilient Messages abstraction implementation. --- src/lib/Messages.js | 20 +++----------------- 1 file changed, 3 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/lib/Messages.js b/src/lib/Messages.js index de11e66..56d6bf2 100644 --- a/src/lib/Messages.js +++ b/src/lib/Messages.js @@ -7,7 +7,8 @@ if (!__meta) { // legacy message from embedder? if (!_messageName) { - throw new Error(`NoScript cannot handle message %s`, JSON.stringify(msg)); + debug(`Message not in NoScript-specific format: %s`, JSON.stringify(msg)); + return undefined; } __meta = {name: _messageName}; } @@ -24,22 +25,7 @@ answers.length === 1 ? answers.pop(): Promise.all(answers) ); } - let context = typeof window === "object" && window.location.href || "?"; - let originalSender = __meta.originalSender || sender; - let {url} = originalSender; - - if (url && context.replace(/[?#].*/, '') === url.replace(/[?#].*/, '')) { - throw new Error(`Message ${name} ${JSON.stringify(msg)} looping to its sender (${context})`); - } - console.debug("Warning: no handler for message %o in context %s", msg, context); - if (originalSender.tab && originalSender.tab.id) { - // if we're receiving a message from content, there might be another - // Messages instance in a different context (e.g. background page vs - // options page vs browser action) capable of processing it, and we've - // just "steal" it. Let's rebroadcast. - return await Messages.send(name, msg, {originalSender}); - } - throw new Error(`No handler registered for message "${name}" in context ${context}`); + debug("Warning: no handler for message %s %s in context %s", name, JSON.stringify(msg), context); }; var Messages = { -- cgit v1.2.3