diff --git a/src/extension/background-script/actions/accounts/select.ts b/src/extension/background-script/actions/accounts/select.ts index 1f158e2e27..8ac7cd69c2 100644 --- a/src/extension/background-script/actions/accounts/select.ts +++ b/src/extension/background-script/actions/accounts/select.ts @@ -47,23 +47,17 @@ export default select; // which will then be posted to the window so websites can sync with the switched account async function notifyAccountChanged() { try { - const tabs = await browser.tabs.query({ - active: true, - currentWindow: true, + const tabs = await browser.tabs.query({}); + // Send message to tabs with URLs starting with "http" or "https" + const validTabs = tabs.filter((tab) => { + const currentUrl = tab.url || ""; + return currentUrl.startsWith("http") || currentUrl.startsWith("https"); }); - const currentUrl = tabs.length && tabs[0].url; - // http for localhost websites - let validTabUrl = null; - if (currentUrl) - validTabUrl = - currentUrl.startsWith("http") || currentUrl.startsWith("https"); - if (validTabUrl) { - browser.tabs.sendMessage(tabs[0].id as number, { - action: "accountChanged", - }); - } else { - throw new Error("Unable to find active tab"); + for (const tab of validTabs) { + if (tab.id) { + await browser.tabs.sendMessage(tab.id, { action: "accountChanged" }); + } } } catch (error) { console.error("Failed to notify account changed", error); diff --git a/src/extension/content-script/onendnostr.js b/src/extension/content-script/onendnostr.js index 79a36dd04d..0322eb17d7 100644 --- a/src/extension/content-script/onendnostr.js +++ b/src/extension/content-script/onendnostr.js @@ -33,7 +33,10 @@ async function init() { browser.runtime.onMessage.addListener((request, sender, sendResponse) => { // forward account changed messaged to inpage script if (request.action === "accountChanged") { - window.postMessage({ action: "accountChanged", scope: "nostr" }, "*"); + window.postMessage( + { action: "accountChanged", scope: "nostr" }, + window.location.origin + ); } }); diff --git a/src/extension/content-script/onendwebln.js b/src/extension/content-script/onendwebln.js index 33ad27bd3a..6527553f80 100644 --- a/src/extension/content-script/onendwebln.js +++ b/src/extension/content-script/onendwebln.js @@ -26,7 +26,6 @@ const disabledCalls = ["webln/enable"]; let isEnabled = false; // store if webln is enabled for this content page let isRejected = false; // store if the webln enable call failed. if so we do not prompt again let callActive = false; // store if a webln call is currently active. Used to prevent multiple calls in parallel - async function init() { const inject = await shouldInject(); if (!inject) { @@ -41,8 +40,11 @@ async function init() { extractLightningData(); } // forward account changed messaged to inpage script - else if (request.action === "accountChanged") { - window.postMessage({ action: "accountChanged", scope: "webln" }, "*"); + else if (request.action === "accountChanged" && isEnabled) { + window.postMessage( + { action: "accountChanged", scope: "webln" }, + window.location.origin + ); } }); diff --git a/src/extension/providers/webln/index.ts b/src/extension/providers/webln/index.ts index 817dabc40d..721c97862a 100644 --- a/src/extension/providers/webln/index.ts +++ b/src/extension/providers/webln/index.ts @@ -113,16 +113,13 @@ export default class WebLNProvider { } async on(...args: Parameters) { - await this.enable(); return this._eventEmitter.on(...args); } async emit(...args: Parameters) { - await this.enable(); return this._eventEmitter.emit(...args); } async off(...args: Parameters) { - await this.enable(); return this._eventEmitter.off(...args); }