Mirror NoScript's badge copntent in the context menu to provide more info (e.g. on XSS or TG status) whenever the toolbar icon is hidden.

This commit is contained in:
hackademix 2023-06-16 09:10:43 +02:00
parent c6add32551
commit a57c4cba84
No known key found for this signature in database
GPG Key ID: 231A83AFDA9C2434
1 changed files with 17 additions and 2 deletions

View File

@ -32,7 +32,18 @@
let popupFor = tabId => `${popupURL}#tab${tabId}`;
let ctxMenuId = "noscript-ctx-menu";
let menuShowing = false;
const menuUpdater = async (tabId) => {
if (!menuShowing) return;
try {
let badgeText = await browser.browserAction.getBadgeText({tabId});
let title = "NoScript";
if (badgeText) title = `${title} [${badgeText}]`;
await browser.contextMenus.update(ctxMenuId, {title});
} catch (e) {
// it might break in some load stages / with privileged page: don't spam the console
}
}
async function toggleCtxMenuItem(show = ns.local.showCtxMenuItem) {
if (!("contextMenus" in browser)) return;
let id = ctxMenuId;
@ -40,12 +51,16 @@
await browser.contextMenus.remove(id);
} catch (e) {}
if (show) {
if (menuShowing = show) {
browser.contextMenus.create({
id,
title: "NoScript",
contexts: ["all"]
});
if (!browser.tabs.onUpdated.hasListener(menuUpdater)) {
browser.tabs.onUpdated.addListener(menuUpdater);
browser.tabs.onActivated.addListener(({tabId}) => menuUpdater(tabId));
}
}
}