Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: events improvements #2630

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line below, can we replace "*"? we only want the inpage script to receive the message so it can emit the event for any listeners. Currently all frames on the website are receiving it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can use window.location.origin here. which returns domain without any routes/ query parameters

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
Loading