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");
|
|
|
|
|
|
|
|
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",
|
|
|
|
blink: "icons/browserAction-printedBlink.svg"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-08-21 22:43:41 +02:00
|
|
|
browser.browserAction.setBadgeBackgroundColor({
|
|
|
|
color: "rgba(255, 0, 0, 0.6)"
|
|
|
|
});
|
|
|
|
|
|
|
|
const apiMap = new Map();
|
|
|
|
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);
|
|
|
|
if (
|
|
|
|
settings.get("showNotifications", url) &&
|
|
|
|
!lists.get("ignore").match(url)
|
|
|
|
){
|
|
|
|
browser.pageAction.show(tabId);
|
|
|
|
browser.pageAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.pageAction[settings.highlightPageAction]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
browser.browserAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.browserAction[settings.highlightBrowserAction]
|
|
|
|
});
|
2018-08-21 22:43:41 +02:00
|
|
|
|
|
|
|
let apis = apiMap.get(tabId);
|
|
|
|
if (!apis){
|
|
|
|
apis = new Set();
|
|
|
|
}
|
|
|
|
apis.add(api);
|
|
|
|
apiMap.set(tabId, apis);
|
|
|
|
if (settings.get("displayBadge", url)){
|
|
|
|
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
|
|
|
});
|
|
|
|
browser.browserAction.setTitle({
|
|
|
|
tabId: tabId,
|
2018-08-22 22:37:06 +02:00
|
|
|
title: browser.i18n.getMessage("browserAction_title_notified") + apiList
|
2018-08-21 22:43:41 +02:00
|
|
|
});
|
2018-08-20 21:48:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
scope.hide = function hideNotification(tabId){
|
|
|
|
logging.notice("Hide page action for tab", tabId);
|
2018-08-21 22:43:41 +02:00
|
|
|
apiMap.delete(tabId);
|
2018-08-20 21:48:05 +02:00
|
|
|
browser.pageAction.hide(tabId);
|
|
|
|
browser.pageAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.pageAction.none
|
|
|
|
});
|
|
|
|
browser.browserAction.setIcon({
|
|
|
|
tabId: tabId,
|
|
|
|
path: paths.browserAction.none
|
|
|
|
});
|
2018-08-21 22:43:41 +02:00
|
|
|
browser.browserAction.setBadgeText({
|
|
|
|
tabId: tabId,
|
|
|
|
text: ""
|
|
|
|
});
|
|
|
|
browser.browserAction.setTitle({
|
|
|
|
tabId: tabId,
|
2018-08-22 22:37:06 +02:00
|
|
|
title: browser.i18n.getMessage("browserAction_title_default")
|
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){
|
|
|
|
apiMap.delete(tabId);
|
|
|
|
});
|
|
|
|
settings.on("displayBadge", function({newValue}){
|
|
|
|
if (!newValue){
|
|
|
|
logging.message("badge was disabled -> hide all badges");
|
|
|
|
browser.tabs.query({}).then(function(tabs){
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.browserAction.setBadgeText({
|
|
|
|
tabId: tab.id,
|
|
|
|
text: ""
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2018-08-20 21:48:05 +02:00
|
|
|
}());
|