nostr-links/background.js
2025-04-23 12:43:41 +04:00

34 lines
1.2 KiB
JavaScript

browser.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
if (changeInfo.status !== 'complete') {
browser.pageAction.hide(tabId);
await browser.storage.local.remove(`nl_tab_${sender.tab.id}`);
return;
}
}, { properties: ["status"] });
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
console.log("[nostr-links] onMessage:", message, sender);
if (message.action === "showNostrLinksPageAction" && sender.tab?.id) {
const tabLinks = { [`nl_tab_${sender.tab.id}`]: message.links };
browser.storage.local.set(tabLinks, () => {
browser.pageAction.show(sender.tab.id);
});
}
if (message.action === "getNostrLinks" && message.tabId) {
console.log("[nostr-links] getNostrLinks:", message.tabId);
browser.storage.local.get(`nl_tab_${message.tabId}`, (result) => {
console.log("[nostr-links] tab links:", result);
sendResponse({ links: result[`nl_tab_${message.tabId}`] || [] });
});
return true; // Keep message channel open for async response
}
});
browser.tabs.onRemoved.addListener((tabId) => {
console.log("[nostr-links] tabs.onRemoved:", tabId);
browser.storage.local.remove(`nl_tab_${tabId}`);
});