fix(context-menu): inject content script on demand and add logging
Ensure the content script is present by injecting it if no receiver exists, then retry collection. Add debug logs and a badge/title update for feedback.
This commit is contained in:
parent
c34227e3b6
commit
f7e7b3fb99
1 changed files with 27 additions and 2 deletions
|
|
@ -17,6 +17,29 @@ browser.runtime.onMessage.addListener(async (msg, sender) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
async function collectFromTab(tabId) {
|
||||||
|
try {
|
||||||
|
// Try asking an existing content script first
|
||||||
|
const items = await browser.tabs.sendMessage(tabId, { type: 'collectLinks' });
|
||||||
|
return items || [];
|
||||||
|
} catch (err) {
|
||||||
|
// If there is no receiver yet, inject the content script and retry
|
||||||
|
const noReceiver = /Receiving end does not exist|Could not establish connection/i.test(String(err));
|
||||||
|
try {
|
||||||
|
if (noReceiver) {
|
||||||
|
try { await browser.tabs.executeScript(tabId, { file: 'src/content/collect.js' }); } catch (_) {}
|
||||||
|
const items = await browser.tabs.sendMessage(tabId, { type: 'collectLinks' });
|
||||||
|
return items || [];
|
||||||
|
}
|
||||||
|
} catch (err2) {
|
||||||
|
// Log and rethrow below
|
||||||
|
console.warn('collectFromTab retry failed:', err2);
|
||||||
|
}
|
||||||
|
console.warn('collectFromTab failed:', err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Context menu to collect links on archive.org /download/* pages
|
// Context menu to collect links on archive.org /download/* pages
|
||||||
browser.runtime.onInstalled.addListener(() => {
|
browser.runtime.onInstalled.addListener(() => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -34,7 +57,7 @@ browser.runtime.onInstalled.addListener(() => {
|
||||||
browser.contextMenus.onClicked.addListener(async (info, tab) => {
|
browser.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||||
if (info.menuItemId !== 'aolg-collect' || !tab?.id) return;
|
if (info.menuItemId !== 'aolg-collect' || !tab?.id) return;
|
||||||
try {
|
try {
|
||||||
const items = await browser.tabs.sendMessage(tab.id, { type: 'collectLinks' });
|
const items = await collectFromTab(tab.id);
|
||||||
const count = (items || []).length;
|
const count = (items || []).length;
|
||||||
await browser.storage.local.set({
|
await browser.storage.local.set({
|
||||||
lastCollected: { tabId: tab.id, url: tab.url, time: Date.now(), count },
|
lastCollected: { tabId: tab.id, url: tab.url, time: Date.now(), count },
|
||||||
|
|
@ -42,7 +65,9 @@ browser.contextMenus.onClicked.addListener(async (info, tab) => {
|
||||||
});
|
});
|
||||||
try { await browser.browserAction.setBadgeBackgroundColor({ color: '#3b82f6' }); } catch (e) {}
|
try { await browser.browserAction.setBadgeBackgroundColor({ color: '#3b82f6' }); } catch (e) {}
|
||||||
try { await browser.browserAction.setBadgeText({ text: count ? String(count) : '' }); } catch (e) {}
|
try { await browser.browserAction.setBadgeText({ text: count ? String(count) : '' }); } catch (e) {}
|
||||||
|
try { await browser.browserAction.setTitle({ title: count ? `Collected ${count} link(s)` : 'No links collected' }); } catch (e) {}
|
||||||
|
console.debug('Collected links from page:', { count });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Swallow errors; context menu is best-effort
|
console.warn('Context menu collection failed:', e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue