Rebuild whitelist menu onShown instead of navigation or tab activation

This commit is contained in:
hensm
2019-05-06 04:17:53 +01:00
parent a52a53d0ad
commit 4ac2dcad65
2 changed files with 89 additions and 67 deletions

View File

@@ -124,9 +124,9 @@ async function createMenus () {
}); });
whitelistMenuId = await browser.menus.create({ whitelistMenuId = await browser.menus.create({
contexts: [ "browser_action", "tools_menu" ] contexts: [ "browser_action" ]
, title: _("contextAddToWhitelist") , title: _("contextAddToWhitelist")
, visible: false , enabled: false
}); });
whitelistRecommendedMenuId = await browser.menus.create({ whitelistRecommendedMenuId = await browser.menus.create({
@@ -141,32 +141,41 @@ async function createMenus () {
} }
browser.webNavigation.onCommitted.addListener( browser.menus.onShown.addListener(info => {
details => { // Only rebuild menus if whitelist menu present
// Only track navigation in top level contexts // Workaround type issues
if (details.frameId === 0) { const menuIds = info.menuIds as unknown as number[];
rebuildWhitelistMenus(details.url); if (!menuIds.includes(whitelistMenuId as number)) {
return;
} }
if (!info.pageUrl) {
browser.menus.update(whitelistMenuId, {
enabled: false
});
browser.menus.refresh();
return;
} }
, { url: [
{ schemes: [ "http", "https" ]}] const url = new URL(info.pageUrl);
const hasOrigin = url.origin !== "null";
/**
* If .origin is "null", hide top-level menu and don't
* bother re-building submenus, since we're probably not on
* a remote page.
*/
browser.menus.update(whitelistMenuId, {
enabled: hasOrigin
}); });
browser.tabs.onActivated.addListener(async () => { if (!hasOrigin) {
const { url } = (await browser.tabs.query({ browser.menus.refresh();
active: true return;
, currentWindow: true }
}))[0];
rebuildWhitelistMenus(url);
});
async function rebuildWhitelistMenus (urlString: string) {
const url = new URL(urlString);
await browser.menus.update(whitelistMenuId, {
visible: url.origin !== "null"
});
function addWhitelistMenuItem (pattern: string) { function addWhitelistMenuItem (pattern: string) {
const menuId = browser.menus.create({ const menuId = browser.menus.create({
@@ -177,6 +186,7 @@ async function rebuildWhitelistMenus (urlString: string) {
whitelistMenuMap.set(menuId, pattern); whitelistMenuMap.set(menuId, pattern);
} }
for (const [ menuId ] of whitelistMenuMap) { for (const [ menuId ] of whitelistMenuMap) {
// Remove all temporary menus // Remove all temporary menus
if (menuId !== whitelistRecommendedMenuId) { if (menuId !== whitelistRecommendedMenuId) {
@@ -188,20 +198,35 @@ async function rebuildWhitelistMenus (urlString: string) {
} }
const recommendedPattern = `${url.origin}/*`; const baseHost = (url.host.match(/\./g) || []).length > 1
? url.host.substring(url.host.indexOf(".") + 1)
: url.host;
const patternRecommended = `${url.origin}/*`;
const patternSearch = `${url.origin}${url.pathname}${url.search}`;
const patternWildcardProtocol = `*://${url.host}/*`;
const patternWildcardSubdomain = `${url.protocol}//*.${baseHost}/*`;
const patternWildcardProtocolAndSubdomain = `*://*.${baseHost}/*`;
// Update recommended menu item
browser.menus.update(whitelistRecommendedMenuId, { browser.menus.update(whitelistRecommendedMenuId, {
title: _("contextAddToWhitelistRecommended", recommendedPattern) title: _("contextAddToWhitelistRecommended", patternRecommended)
}); });
whitelistMenuMap.set(whitelistRecommendedMenuId, patternRecommended);
whitelistMenuMap.set(whitelistRecommendedMenuId, recommendedPattern);
if (url.search) { if (url.search) {
addWhitelistMenuItem(`${url.origin}${url.pathname}${url.search}`); addWhitelistMenuItem(patternSearch);
} }
/**
* Split url path into segments and add menu items for each
* partial path as the segments are removed.
*/
{
const pathTrimmed = url.pathname.endsWith("/") const pathTrimmed = url.pathname.endsWith("/")
? url.pathname.substring(0, url.pathname.length - 1) ? url.pathname.substring(0, url.pathname.length - 1)
: url.pathname; : url.pathname;
@@ -223,21 +248,18 @@ async function rebuildWhitelistMenus (urlString: string) {
index++; index++;
} }
} }
const baseHost = (url.host.match(/\./g) || []).length > 1
? url.host.substring(url.host.indexOf(".") + 1)
: url.host;
// Wildcard protocol
addWhitelistMenuItem(`*://${url.host}/*`);
// Wildcard subdomain
addWhitelistMenuItem(`${url.protocol}//*.${baseHost}/*`);
// Wildcard protocol and subdomain
addWhitelistMenuItem(`*://*.${baseHost}/*`);
} }
// Add remaining menu items
addWhitelistMenuItem(patternWildcardProtocol);
addWhitelistMenuItem(patternWildcardSubdomain);
addWhitelistMenuItem(patternWildcardProtocolAndSubdomain);
browser.menus.refresh();
});
// Google-hosted API loader script // Google-hosted API loader script
const SENDER_SCRIPT_URL = const SENDER_SCRIPT_URL =
"https://www.gstatic.com/cv/js/sender/v1/cast_sender.js"; "https://www.gstatic.com/cv/js/sender/v1/cast_sender.js";