1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/lib/notifications.js

124 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-02-13 12:48:22 +01:00
/* jslint moz: true */
/* 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/. */
2016-02-13 12:28:36 +01:00
var URL = require("sdk/url").URL;
2016-02-13 12:28:36 +01:00
const {parseErrorStack} = require("./callingStack");
2016-02-13 12:28:36 +01:00
var tabUtils = require("sdk/tabs/utils");
exports.notify = function({url, errorStack}, {lists, notificationPref, _, browser, window}){
2015-09-08 11:42:32 +02:00
"use strict";
2016-02-13 12:28:36 +01:00
var callingStackMsg = parseErrorStack(errorStack);
2015-09-08 11:42:32 +02:00
2016-02-13 12:28:36 +01:00
var contentURL = new URL(url);
if (notificationPref.doShow() && !lists.get("ignore").match(contentURL)){
2016-02-13 12:48:22 +01:00
url = contentURL.href;
var domain = contentURL.hostname;
2016-02-13 12:28:36 +01:00
var message = _("fakedReadout").replace(/\{url\}/g, domain || url);
2016-02-13 12:28:36 +01:00
var tab, tabBrowser;
if (browser){
window = tabUtils.getOwnerWindow(browser);
tab = tabUtils.getTabForBrowser(browser);
tabBrowser = tabUtils.getTabBrowser(window);
}
else if (window){
tab = tabUtils.getTabForContentWindow(window);
tabBrowser = tabUtils.getTabBrowserForTab(tab);
browser = tabUtils.getBrowserForTab(tab);
}
var notifyBox = tabBrowser.getNotificationBox(browser);
var notification = notifyBox.getNotificationWithValue("fake-readout");
if (notification){
notification.label = message;
notification.url = url;
notification.domain = domain;
notification.callingStackMsg = callingStackMsg;
}
else {
var buttons = [
{
label: _("displayFullURL"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
window.alert(notification.url);
// only way to prevent closing... see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/appendNotification#Notification_box_events
throw new Error("Do not close notification.");
}
},
{
label: _("displayCallingStack"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
window.alert(notification.callingStackMsg);
// only way to prevent closing... see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Method/appendNotification#Notification_box_events
throw new Error("Do not close notification.");
}
},
{
label: _("ignorelistDomain"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
var domain = window.prompt(
_("inputIgnoreDomain"),
notification.domain
);
if (domain){
lists.appendTo("ignore", domain);
}
}
},
{
label: _("whitelistURL"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
var url = window.prompt(
_("inputWhitelistDomain"),
"^" + notification.url.replace(/([\\\+\*\?\[\^\]\$\(\)\{\}\=\!\|\.])/g, "\\$1") + "$"
);
if (url){
lists.appendTo("white", url);
}
}
},
{
label: _("whitelistDomain"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
var domain = window.prompt(
_("inputWhitelistURL"),
notification.domain
);
if (domain){
lists.appendTo("white", domain);
}
}
},
{
label: _("disableNotifications"),
accessKey: "",
callback: function(){
2016-02-13 12:28:36 +01:00
notificationPref.setShow(false);
}
}
];
var priority = notifyBox.PRIORITY_WARNING_MEDIUM;
notification = notifyBox.appendNotification(
message,
"fake-readout",
"chrome://browser/skin/Info.png",
priority,
buttons
);
notification.url = url;
notification.domain = domain;
notification.callingStackMsg = callingStackMsg;
}
return notification;
}
};