-
Notifications
You must be signed in to change notification settings - Fork 7
/
bg.js
232 lines (219 loc) · 9.76 KB
/
bg.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
var previousCpuInfo;
var highload = 0;
var number= 0;
function CpuUsage() {
totaluse = 0;
chrome.system.cpu.getInfo(function(cpuInfo) {
for (var i = 0; i < cpuInfo.numOfProcessors; i++) {
var usage = cpuInfo.processors[i].usage;
var usedSectionWidth = 0;
if (previousCpuInfo) {
var oldUsage = previousCpuInfo.processors[i].usage;
usedSectionWidth = Math.floor((usage.kernel + usage.user - oldUsage.kernel - oldUsage.user) / (usage.total - oldUsage.total) * 100);
totaluse += usedSectionWidth;
} else {
usedSectionWidth = Math.floor((usage.kernel + usage.user) / usage.total * 100);
}
}
previousCpuInfo = cpuInfo;
totaluse = totaluse/cpuInfo.numOfProcessors;
console.log('%c CPU USAGE: '+totaluse+'% ', 'background:#b7b7b7; color: red')
if(totaluse > 50){
highload++
console.log('Countdown to CPU-alert: '+highload+'/30')
}else{
highload = 0;
}
if(highload === 30){
chrome.storage.sync.get('notification', function(res){
if(res.notification == true || res.notification == 'undefined'){
fetch('https://raw.githubusercontent.com/andreas0607/CoinHive-blocker/master/ignore.json').then(function(response) {
response.json().then(function(ignore) {
var opt = {
type: "basic",
title: 'High CPU-usage detected',
message: 'Please click this notification so that we can look into it',
priority: 1,
'requireInteraction': true,
iconUrl: 'chrome-extension://ccagdbjcbhmcdcbbknfebhhdbolnfimo/128logo.png'
};
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs){
var site = tabs[0].url;
chrome.notifications.clear("HIGHCPU")
console.log(ignore.length)
for(var i=0; i<ignore.length; i++){
if(site.indexOf(ignore[i]) !== -1){
console.log('Cleared')
break;
}else if(i == (ignore.length - 1)){
chrome.notifications.create('HIGHCPU', opt);
chrome.notifications.onClicked.addListener(function(res){
if(res == "HIGHCPU"){
chrome.notifications.clear('HIGHCPU')
console.log('Reported: '+site)
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://docs.google.com/forms/d/1cB1urxRqau67pWoN5s1oSfUyi9t0G5T3dMT_txc-o3Q/formResponse', true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'entry.2146017821='+site;
xhr.send(data);
}
})
}
}
})
})
})
}
});
}
});
}
CpuUsage()
setInterval(function(){
CpuUsage()
},10000)
function block(blacklist) {
console.log('Ready to block')
chrome.storage.sync.get('block', function(res){
if(res.block){
chrome.webRequest.onBeforeRequest.addListener(
callback, {
urls : blacklist}, ["blocking"]);
chrome.browserAction.setBadgeText({text:'On'})
}else if(res.block == false){
chrome.browserAction.setBadgeText({text:'Off'});
chrome.webRequest.onBeforeRequest.removeListener(callback);
}else if(res.block == null){
chrome.browserAction.setBadgeText({text:'Off'});
}
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
chrome.storage.sync.get('block', function(res){
if(res.block){
chrome.webRequest.onBeforeRequest.addListener(
callback,{
urls : blacklist}, ["blocking"]);
chrome.browserAction.setBadgeText({text:'On'})
}else if(res.block == false){
chrome.webRequest.onBeforeRequest.removeListener(callback);
chrome.browserAction.setBadgeText({text:'Off'});
}
});
});
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.browserAction.getBadgeText({}, function(result) {
if(result == 'On'){
chrome.browserAction.setBadgeText({text:'Stats'});
chrome.browserAction.setPopup({
popup: 'popup.html'
})
}else if(result == 'Stats'){
chrome.storage.sync.set({'block': false}, function() {
console.log('Turn Off');
chrome.browserAction.setPopup({
popup: ''
})
});
}else if(result == 'Off'){
chrome.storage.sync.set({'block': true}, function() {
console.log('Turn On');
chrome.browserAction.setPopup({
popup: ''
})
});
}
});
});
if(chrome.webRequest.onBeforeRequest.hasListener(callback)){
console.log('Listning')
}
}
chrome.storage.onChanged.addListener(function(ress){
if(ress.stat){
number++;
if(number == 10){
chrome.storage.sync.get('feedback', function(res){
console.log(res.feedback)
if(res.feedback == null || res.feedback == 'undefined'){
var inf = {
type: "basic",
title: 'Enjoy miner-free browsing?',
message: 'Please consider leaving some feedback on our extension. Enjoy your mining-free browsing🎉',
priority: 1,
'requireInteraction': true,
iconUrl: 'chrome-extension://ccagdbjcbhmcdcbbknfebhhdbolnfimo/128logo.png'
};
chrome.notifications.create('review', inf);
chrome.notifications.onClicked.addListener(function(res){
if(res == "review"){
chrome.notifications.clear("review")
chrome.tabs.create({ url: 'https://chrome.google.com/webstore/detail/coin-hive-blocker/ccagdbjcbhmcdcbbknfebhhdbolnfimo/reviews' });
}
chrome.storage.sync.set({'feedback': true})
})
}else{
console.log("Already prompted")
}
});
}
}
})
function start(){
fetch('https://raw.githubusercontent.com/andreas0607/CoinHive-blocker/master/blacklist.json').then(function(response) {
console.log(response.ok)
if(!response.ok) {
throw new Error('Network response was not ok.');
}
return response.json()
}).then(block).catch(function(error) {
console.log(error.message)
//Retry in 10 sec
console.log('Fetching local')
fetch('blacklist.json').then(function(response) {
return response.json()
}).then(block)
})
}
var callback = function deny(block) {
chrome.storage.sync.get('stat', function(res){
if(res.stat === undefined){var obj = {}
chrome.storage.sync.set({'stat': obj}, function() {});
}else{
var obj = res.stat;
chrome.tabs.query({active: true, lastFocusedWindow: true}, function(tabs){
console.log(tabs[0].url)
var site = tabs[0].url;
if(!(site in obj)){
var l = site;
obj[l] = 1;
chrome.storage.sync.set({'stat': obj}, function() {});
}else{
var l = site;
obj[l] = obj[l] + 1;
chrome.storage.sync.set({'stat': obj}, function() {});
}
});
}
});
return {cancel: true};
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
chrome.storage.sync.get('stat', function(res){
var obj = res.stat
if(res.stat === null){
chrome.storage.sync.set({'stat': {}}, function() {});
}else{
if(!(request.site in obj)){
var l = request.site;
obj[l] = 1;
chrome.storage.sync.set({'stat': obj}, function() {});
}else{
var l = request.site;
obj[l] = obj[l] + 1;
chrome.storage.sync.set({'stat': obj}, function() {});
}
}
});
})
start()