Skip to content

Commit

Permalink
Merge pull request #2630 from getAlby/events-improvements
Browse files Browse the repository at this point in the history
fix: events improvements
  • Loading branch information
bumi authored Aug 5, 2023
2 parents 0fb8b93 + b6beda0 commit 2930312
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 22 deletions.
24 changes: 9 additions & 15 deletions src/extension/background-script/actions/accounts/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/extension/content-script/onendnostr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
});

Expand Down
8 changes: 5 additions & 3 deletions src/extension/content-script/onendwebln.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
);
}
});

Expand Down
3 changes: 0 additions & 3 deletions src/extension/providers/webln/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,13 @@ export default class WebLNProvider {
}

async on(...args: Parameters<EventEmitter["on"]>) {
await this.enable();
return this._eventEmitter.on(...args);
}
async emit(...args: Parameters<EventEmitter["emit"]>) {
await this.enable();
return this._eventEmitter.emit(...args);
}

async off(...args: Parameters<EventEmitter["off"]>) {
await this.enable();
return this._eventEmitter.off(...args);
}

Expand Down

0 comments on commit 2930312

Please sign in to comment.