2018-08-20 21:48:05 +02:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var scope;
|
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope = {};
|
|
|
|
window.scope.notification = scope;
|
|
|
|
}
|
|
|
|
|
|
|
|
const settings = require("./settings");
|
|
|
|
const lists = require("./lists");
|
|
|
|
const logging = require("./logging");
|
|
|
|
|
2018-09-06 20:13:16 +02:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2018-09-08 00:44:37 +02:00
|
|
|
function getBrowserActionIconName(tabData, notified){
|
|
|
|
if (tabData.whitelisted){
|
2018-09-06 20:13:16 +02:00
|
|
|
return "whitelisted";
|
|
|
|
}
|
|
|
|
else if (notified) {
|
|
|
|
return settings.highlightBrowserAction;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "none";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 21:48:05 +02:00
|
|
|
const paths = {
|
|
|
|
pageAction: {
|
|
|
|
none: "icons/pageAction-printed.svg",
|
|
|
|
color: "icons/pageAction-printedHighlight.svg",
|
|
|
|
blink: "icons/pageAction-printedBlink.svg"
|
|
|
|
},
|
|
|
|
browserAction: {
|
|
|
|
none: "icons/browserAction-notPrinted.svg",
|
|
|
|
color: "icons/browserAction-printed.svg",
|
2018-09-06 20:13:16 +02:00
|
|
|
blink: "icons/browserAction-printedBlink.svg",
|
|
|
|
whitelisted: "icons/browserAction-whitelisted.svg"
|
2018-08-20 21:48:05 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-10-02 19:23:59 +02:00
|
|
|
if (browser.browserAction.setBadgeBackgroundColor){
|
|
|
|
browser.browserAction.setBadgeBackgroundColor({
|
|
|
|
color: "rgba(255, 0, 0, 0.6)"
|
|
|
|
});
|
|
|
|
}
|
2018-08-21 22:43:41 +02:00
|
|
|
|
2018-09-08 00:44:37 +02:00
|
|
|
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;
|
|
|
|
}
|
2018-08-21 22:43:41 +02:00
|
|
|
scope.show = function showNotification(tabId, url, api){
|
2018-08-24 16:47:27 +02:00
|
|
|
if (settings.ignoredAPIs[api]){
|
|
|
|
return;
|
|
|
|
}
|
2018-08-20 21:48:05 +02:00
|
|
|
logging.notice("Show notification for tab", tabId);
|
2018-09-08 00:44:37 +02:00
|
|
|
const tabData = getTabData(tabId);
|
2018-08-20 21:48:05 +02:00
|
|
|
if (
|
|
|
|
settings.get("showNotifications", url) &&
|
|
|
|
!lists.get("ignore").match(url)
|
|
|
|
){
|
|
|
|
browser.pageAction.show(tabId);
|
2018-10-02 19:23:59 +02:00
|
|
|
if (browser.pageAction.setIcon){
|
|
|
|
browser.pageAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.pageAction[settings.highlightPageAction]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (browser.browserAction.setIcon){
|
|
|
|
browser.browserAction.setIcon({
|
2018-08-20 21:48:05 +02:00
|
|
|
tabId: tabId,
|
2018-10-02 19:23:59 +02:00
|
|
|
path: paths.browserAction[getBrowserActionIconName(tabData, true)]
|
2018-08-20 21:48:05 +02:00
|
|
|
});
|
|
|
|
}
|
2018-08-21 22:43:41 +02:00
|
|
|
|
2018-09-08 00:44:37 +02:00
|
|
|
const apis = tabData.apis;
|
2018-08-21 22:43:41 +02:00
|
|
|
apis.add(api);
|
2018-10-02 19:23:59 +02:00
|
|
|
if (
|
|
|
|
settings.get("displayBadge", url) &&
|
|
|
|
browser.browserAction.setBadgeText
|
|
|
|
){
|
2018-08-21 22:43:41 +02:00
|
|
|
browser.browserAction.setBadgeText({
|
|
|
|
tabId: tabId,
|
|
|
|
text: apis.size > 1? apis.size.toString(): api.charAt(0).toUpperCase()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let apiList = "";
|
|
|
|
apis.forEach(function(api){
|
2018-08-22 22:37:06 +02:00
|
|
|
apiList += browser.i18n.getMessage("browserAction_title_protectedAPIs").replace(/{api}/g, api);
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
2018-09-06 20:13:16 +02:00
|
|
|
|
|
|
|
let browserActionTitle = browser.i18n.getMessage("browserAction_title_default");
|
2018-09-08 00:44:37 +02:00
|
|
|
if (tabData.whitelisted){
|
2018-10-02 13:27:01 +02:00
|
|
|
browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted")
|
|
|
|
.replace(/{url}/g, tabData.url);
|
2018-09-06 20:13:16 +02:00
|
|
|
}
|
|
|
|
browserActionTitle += browser.i18n.getMessage("browserAction_title_notified");
|
|
|
|
browserActionTitle += apiList;
|
2018-08-21 22:43:41 +02:00
|
|
|
browser.browserAction.setTitle({
|
|
|
|
tabId: tabId,
|
2018-09-06 20:13:16 +02:00
|
|
|
title: browserActionTitle
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
2018-08-20 21:48:05 +02:00
|
|
|
};
|
|
|
|
|
2018-09-06 20:13:16 +02:00
|
|
|
scope.hide = function hideNotification(tabId, url){
|
2018-08-20 21:48:05 +02:00
|
|
|
logging.notice("Hide page action for tab", tabId);
|
2018-09-08 00:44:37 +02:00
|
|
|
// clear old data
|
|
|
|
tabsData.delete(tabId);
|
|
|
|
const tabData = getTabData(tabId);
|
|
|
|
tabData.url = url;
|
|
|
|
tabData.whitelisted = isWhitelisted(url);
|
|
|
|
|
2018-08-20 21:48:05 +02:00
|
|
|
browser.pageAction.hide(tabId);
|
2018-10-02 19:23:59 +02:00
|
|
|
if (browser.pageAction.setIcon){
|
|
|
|
browser.pageAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.pageAction.none
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (browser.browserAction.setIcon){
|
|
|
|
browser.browserAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.browserAction[getBrowserActionIconName(tabData, false)]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (browser.browserAction.setBadgeText){
|
|
|
|
browser.browserAction.setBadgeText({
|
|
|
|
tabId: tabId,
|
|
|
|
text: ""
|
|
|
|
});
|
|
|
|
}
|
2018-09-06 20:13:16 +02:00
|
|
|
let browserActionTitle = browser.i18n.getMessage("browserAction_title_default");
|
2018-09-08 00:44:37 +02:00
|
|
|
if (tabData.whitelisted){
|
2018-09-06 20:13:16 +02:00
|
|
|
browserActionTitle += browser.i18n.getMessage("browserAction_title_whitelisted").replace(/{url}/g, url);
|
|
|
|
}
|
2018-08-21 22:43:41 +02:00
|
|
|
browser.browserAction.setTitle({
|
|
|
|
tabId: tabId,
|
2018-09-06 20:13:16 +02:00
|
|
|
title: browserActionTitle
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
2018-08-20 21:48:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
settings.on("showNotifications", function({newValue}){
|
|
|
|
if (!newValue){
|
|
|
|
logging.message("notifications were disabled -> hide all page actions");
|
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.pageAction.hide(tab.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-08-21 22:43:41 +02:00
|
|
|
|
|
|
|
browser.tabs.onRemoved.addListener(function(tabId){
|
2018-09-08 00:44:37 +02:00
|
|
|
tabsData.delete(tabId);
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
|
|
|
settings.on("displayBadge", function({newValue}){
|
|
|
|
if (!newValue){
|
|
|
|
logging.message("badge was disabled -> hide all badges");
|
2018-10-02 19:23:59 +02:00
|
|
|
if (browser.browserAction.setBadgeText){
|
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.browserAction.setBadgeText({
|
|
|
|
tabId: tab.id,
|
|
|
|
text: ""
|
|
|
|
});
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
|
|
|
});
|
2018-10-02 19:23:59 +02:00
|
|
|
}
|
2018-08-21 22:43:41 +02:00
|
|
|
}
|
|
|
|
});
|
2018-08-20 21:48:05 +02:00
|
|
|
}());
|