This repository has been archived by the owner on Dec 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
confirm.js
73 lines (69 loc) · 2.57 KB
/
confirm.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function isFirefox(){
if(typeof chrome !== "undefined" && typeof browser !== "undefined"){
return true;
}
return false;
}
function splitDomainPathFromNotificationId(notificationId){
var split = notificationId.split('|');
return {domain: split[0], path: split[1]};
}
function saveDomain(notificationId, type, fireCallback){
var split = splitDomainPathFromNotificationId(notificationId);
var domain = split.domain;
var path = split.path;
chrome.storage.sync.get(domain, function(data){
if(data[domain] === null || typeof data[domain] !== 'object'){
data[domain] = {};
}
data[domain][path] = (type === 0);
chrome.storage.sync.set(data, function(){
if(fireCallback){
chrome.tabs.query({url: 'https://' + domain + '/*'}, function(tabs){
if(data[domain][path]){
chrome.tabs.reload(tabs[0].id);
}else{
chrome.tabs.sendMessage(tabs[0].id, {message: 'remove', scriptURL: 'https://' + domain + path});
}
});
}
});
});
}
if(isFirefox()){
chrome.notifications.onClicked.addListener(function(notificationId){
saveDomain(notificationId, 0, true);
chrome.notifications.clear(notificationId);
});
}else{
chrome.notifications.onButtonClicked.addListener(function(notificationId, buttonIndex){
saveDomain(notificationId, buttonIndex, true);
chrome.notifications.clear(notificationId);
});
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
if(request.message == "ask" && request.domain && request.path && request.pathname){
var path = request.path.replace(/\/\//g, "/").replace(/\.js\?.*/, '.js');
if(path.charAt(0) !== '/'){
path = request.pathname.replace(/\/\//g, "/").replace(/\.js\?.*/, '.js') + path;
}
var options = {
type: "basic",
iconUrl: chrome.extension.getURL("logo.png")
};
if(isFirefox()){ // disable, unless user allows manually
saveDomain(request.domain + '|' + path, 1, false);
options['title'] = "A Service Worker has been blocked for this website (" + request.domain + path + ")?";
options['message'] = "Click this notification to re-enable this Service Worker";
}else{
options['buttons'] = [
{title: "YES"},
{title: "NO"}
];
options['title'] = "Do you want to ALLOW this Service Worker for this website (" + request.domain + path + ")?";
options['message'] = "Click YES to allow, or NO to block";
options['requireInteraction'] = true;
}
chrome.notifications.create(request.domain + '|' + path, options);
}
});