From 6477572f79ddd9a02eef27e9a8961a3a2182d0c3 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Thu, 6 Sep 2018 20:13:16 +0200 Subject: [PATCH 1/2] Added indicator for whitelisted tab First draft for #241 --- .vscode/settings.json | 1 + _locales/de/messages.json | 6 +- _locales/en/messages.json | 6 +- icons/browserAction-whitelisted.svg | 128 ++++++++++++++++++++++++++++ lib/main.js | 2 +- lib/notification.js | 45 ++++++++-- releaseNotes.txt | 1 + 7 files changed, 180 insertions(+), 9 deletions(-) create mode 100644 icons/browserAction-whitelisted.svg diff --git a/.vscode/settings.json b/.vscode/settings.json index ed9cc7b..beba24a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -22,6 +22,7 @@ "prefs", "spodermenpls", "webgl", + "whitelisted", "yfdyh" ], "cSpell.language": "en,de,en-GB" diff --git a/_locales/de/messages.json b/_locales/de/messages.json index f2ff162..c4c4783 100644 --- a/_locales/de/messages.json +++ b/_locales/de/messages.json @@ -13,7 +13,11 @@ "description": "" }, "browserAction_title_notified": { - "message": "CanvasBlocker \n\nSchutz erfolgreich für:", + "message": " \n\nSchutz erfolgreich für:", + "description": "" + }, + "browserAction_title_whitelisted": { + "message": " (APIs erlaubt für {url})", "description": "" }, "browserAction_title_protectedAPIs": { diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 6666151..be166e4 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -13,7 +13,11 @@ "description": "" }, "browserAction_title_notified": { - "message": "CanvasBlocker \n\nprotection successful for:", + "message": " \n\nprotection successful for:", + "description": "" + }, + "browserAction_title_whitelisted": { + "message": " (APIs whitelisted for {url})", "description": "" }, "browserAction_title_protectedAPIs": { diff --git a/icons/browserAction-whitelisted.svg b/icons/browserAction-whitelisted.svg new file mode 100644 index 0000000..bcf5f1a --- /dev/null +++ b/icons/browserAction-whitelisted.svg @@ -0,0 +1,128 @@ + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/lib/main.js b/lib/main.js index 4393f2d..8107858 100644 --- a/lib/main.js +++ b/lib/main.js @@ -65,7 +65,7 @@ notification.show(port.sender.tab.id, url, data["canvasBlocker-notify"].api); } if (data.hasOwnProperty("canvasBlocker-clear-page-action")){ - notification.hide(port.sender.tab.id); + notification.hide(port.sender.tab.id, url); } verbose("got data", data, "from port", port); }); diff --git a/lib/notification.js b/lib/notification.js index 0878cab..ed68e70 100644 --- a/lib/notification.js +++ b/lib/notification.js @@ -17,6 +17,27 @@ const lists = require("./lists"); const logging = require("./logging"); + function isWhitelisted(url){ + if (!(url instanceof URL)){ + url = new URL(url); + } + return lists.get("white").match(url) || + lists.get("sessionWhite").match(url) || + settings.get("blockMode", url).startsWith("allow"); + } + + function getBrowserActionIconName(url, notified){ + if (isWhitelisted(url)){ + return "whitelisted"; + } + else if (notified) { + return settings.highlightBrowserAction; + } + else { + return "none"; + } + } + const paths = { pageAction: { none: "icons/pageAction-printed.svg", @@ -26,7 +47,8 @@ browserAction: { none: "icons/browserAction-notPrinted.svg", color: "icons/browserAction-printed.svg", - blink: "icons/browserAction-printedBlink.svg" + blink: "icons/browserAction-printedBlink.svg", + whitelisted: "icons/browserAction-whitelisted.svg" } }; @@ -52,7 +74,7 @@ } browser.browserAction.setIcon({ tabId: tabId, - path: paths.browserAction[settings.highlightBrowserAction] + path: paths.browserAction[getBrowserActionIconName(url, true)] }); let apis = apiMap.get(tabId); @@ -72,13 +94,20 @@ apis.forEach(function(api){ apiList += browser.i18n.getMessage("browserAction_title_protectedAPIs").replace(/{api}/g, api); }); + + let browserActionTitle = browser.i18n.getMessage("browserAction_title_default"); + if (isWhitelisted(url)){ + browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, url); + } + browserActionTitle += browser.i18n.getMessage("browserAction_title_notified"); + browserActionTitle += apiList; browser.browserAction.setTitle({ tabId: tabId, - title: browser.i18n.getMessage("browserAction_title_notified") + apiList + title: browserActionTitle }); }; - scope.hide = function hideNotification(tabId){ + scope.hide = function hideNotification(tabId, url){ logging.notice("Hide page action for tab", tabId); apiMap.delete(tabId); browser.pageAction.hide(tabId); @@ -88,15 +117,19 @@ }); browser.browserAction.setIcon({ tabId: tabId, - path: paths.browserAction.none + path: paths.browserAction[getBrowserActionIconName(url, false)] }); browser.browserAction.setBadgeText({ tabId: tabId, text: "" }); + let browserActionTitle = browser.i18n.getMessage("browserAction_title_default"); + if (isWhitelisted(url)){ + browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, url); + } browser.browserAction.setTitle({ tabId: tabId, - title: browser.i18n.getMessage("browserAction_title_default") + title: browserActionTitle }); }; diff --git a/releaseNotes.txt b/releaseNotes.txt index efa48a7..e5d6ff4 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -4,6 +4,7 @@ Version 0.5.4: new features: - added save/load directly to/from file option + - browser action icon gets grayed out if the page is whitelisted fixes: - window and audio API were always blocked when using any of the "block ..." modes From e147c119efc443b5f68dc63fc5a96993f0ebcd83 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sat, 8 Sep 2018 00:44:37 +0200 Subject: [PATCH 2/2] Only use whitelisted status of tab url For #241 --- lib/notification.js | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/notification.js b/lib/notification.js index ed68e70..941285c 100644 --- a/lib/notification.js +++ b/lib/notification.js @@ -26,8 +26,8 @@ settings.get("blockMode", url).startsWith("allow"); } - function getBrowserActionIconName(url, notified){ - if (isWhitelisted(url)){ + function getBrowserActionIconName(tabData, notified){ + if (tabData.whitelisted){ return "whitelisted"; } else if (notified) { @@ -56,12 +56,25 @@ color: "rgba(255, 0, 0, 0.6)" }); - const apiMap = new Map(); + const tabsData = new Map(); + function getTabData(tabId){ + let data = tabsData.get(tabId); + if (!data){ + data = { + url: "", + apis: new Set(), + whitelisted: false + }; + tabsData.set(tabId, data); + } + return data; + } scope.show = function showNotification(tabId, url, api){ if (settings.ignoredAPIs[api]){ return; } logging.notice("Show notification for tab", tabId); + const tabData = getTabData(tabId); if ( settings.get("showNotifications", url) && !lists.get("ignore").match(url) @@ -74,15 +87,11 @@ } browser.browserAction.setIcon({ tabId: tabId, - path: paths.browserAction[getBrowserActionIconName(url, true)] + path: paths.browserAction[getBrowserActionIconName(tabData, true)] }); - let apis = apiMap.get(tabId); - if (!apis){ - apis = new Set(); - } + const apis = tabData.apis; apis.add(api); - apiMap.set(tabId, apis); if (settings.get("displayBadge", url)){ browser.browserAction.setBadgeText({ tabId: tabId, @@ -96,8 +105,8 @@ }); let browserActionTitle = browser.i18n.getMessage("browserAction_title_default"); - if (isWhitelisted(url)){ - browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, url); + if (tabData.whitelisted){ + browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, tabData.url); } browserActionTitle += browser.i18n.getMessage("browserAction_title_notified"); browserActionTitle += apiList; @@ -109,7 +118,12 @@ scope.hide = function hideNotification(tabId, url){ logging.notice("Hide page action for tab", tabId); - apiMap.delete(tabId); + // clear old data + tabsData.delete(tabId); + const tabData = getTabData(tabId); + tabData.url = url; + tabData.whitelisted = isWhitelisted(url); + browser.pageAction.hide(tabId); browser.pageAction.setIcon({ tabId: tabId, @@ -117,14 +131,14 @@ }); browser.browserAction.setIcon({ tabId: tabId, - path: paths.browserAction[getBrowserActionIconName(url, false)] + path: paths.browserAction[getBrowserActionIconName(tabData, false)] }); browser.browserAction.setBadgeText({ tabId: tabId, text: "" }); let browserActionTitle = browser.i18n.getMessage("browserAction_title_default"); - if (isWhitelisted(url)){ + if (tabData.whitelisted){ browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, url); } browser.browserAction.setTitle({ @@ -145,7 +159,7 @@ }); browser.tabs.onRemoved.addListener(function(tabId){ - apiMap.delete(tabId); + tabsData.delete(tabId); }); settings.on("displayBadge", function({newValue}){ if (!newValue){