-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
80 lines (74 loc) · 2.57 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
71
72
73
74
75
76
77
78
79
80
chrome.runtime.onInstalled.addListener(function(){
chrome.contextMenus.create({id:'incognigo', title:'Incogni Go',
contexts:['page','link','image','video','audio','frame','selection']});
chrome.storage.local.set({
'userList' :['medium.com','economist.com'],
'closeTab' :true,
'clearHistory' :false,
'state' :'normal'
});
});
function Incogni_Go(tab){
return new Promise(function(resolve,reject) {
chrome.storage.local.get(['closeTab','clearHistory','state'],function(result){
if (result.closeTab) chrome.tabs.remove(tab.id, function(){});
if (result.clearHistory) chrome.history.deleteUrl({url:tab.url});
chrome.windows.getAll(function(all_windows){
let first_incognito=all_windows.find(window=>window.incognito);
if (first_incognito)
chrome.tabs.create({url:tab.url, windowId:first_incognito.id},
function(tab){
resolve('hi new tab in old window!');
});
else
chrome.windows.create({url:tab.url, incognito:true, state:result.state},
function(window){
resolve('hi new window!');
});
});
});
});
}
function readUserList(){
return new Promise(function(resolve,reject){
chrome.storage.local.get(['userList'],function(result){
let userList=result['userList'];
resolve(userList);
})
});
}
function matchUserList(url){
return new Promise(function(resolve,reject){
readUserList()
.then(userList=>{
match=userList.find(item=>
url.match(new RegExp(item,'i')));
if (match)
resolve(true);
else
resolve(false);
});
})
}
chrome.contextMenus.onClicked.addListener(function(info,tab){
const before = window.performance.now();
Incogni_Go(tab)
.then(test=>{
console.log(test, window.performance.now()-before)
});
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab){
if((changeInfo.url)&&(!tab.incognito)){
const before = window.performance.now();
let newUrl=changeInfo.url;
matchUserList(newUrl)
.then(match=>{
if (match){
Incogni_Go(tab)
.then(test=>{
console.log(test, window.performance.now()-before)
});
}
});
}
});