-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
70 lines (66 loc) · 1.66 KB
/
background.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
var enable = true;
const notification = {
type: 'basic',
iconUrl: 'images/icon.png',
title: 'no mo history'
};
const historyListener = item => {
let noMoHistoryData = JSON.parse(localStorage.getItem('noMoHistory')) || [];
const domainName = getDomainName(item.url);
if (noMoHistoryData.includes(domainName)) {
chrome.history.deleteUrl({ url: item.url }, function(res) {
console.log(res);
});
}
};
chrome.extension.onConnect.addListener(port => {
port.onMessage.addListener(message => {
if (message === 'DISABLE') {
enable = false;
toggle(enable);
const notification = chrome.notifications.create(
'enable',
{
...notification,
message: 'no mo history has been disabled'
},
notificationId => {}
);
chrome.notifications.clear('enable', () => {});
}
if (message === 'ENABLE') {
enable = true;
toggle(enable);
const notification = chrome.notifications.create(
'disabled',
{
...notification,
message: 'no mo history has been enabled'
},
notificationId => {}
);
chrome.notifications.clear('disabled', () => {});
}
});
});
function toggle(enable) {
if (enable === true) {
chrome.history.onVisited.addListener(historyListener);
} else {
chrome.history.onVisited.removeListener(historyListener);
}
}
function getDomainName(url) {
var match = url.match(/:\/\/(www[0-9]?\.)?(.[^/:]+)/i);
if (
match != null &&
match.length > 2 &&
typeof match[2] === 'string' &&
match[2].length > 0
) {
return match[2];
} else {
return null;
}
}
toggle(enable);