This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
forked from mikecann/Chrome-Crawler
-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
354 lines (308 loc) · 10.9 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
var tabs = ["Report","Queued","Crawling","Crawled","Errors"];
var allPages = {};
var allCookiesSeen = {};
var allCookies = [];
var allSymbolsSeen = {};
var allSymbols = {};
var startingPages = [];
var latestUpdate = new Date();
var appState;
setAppState("stopped");
var targetTabId = null;
// If the tab isn't complete, or we get no response to messages
// then we'll fail the page with an error
var pageLoadTimeout = 10000;
// There are multiple content scripts, i.e. from iframes
// so we need a bit of state to accumulate them
var _anaylses = {};
// We can't predict how many messages well have: one for each iframe
// Hopefully it's reasonable to wait 500ms after each message to see
// if more will appear.
var _anaylsesDebounceTimeout = 500;
chrome.runtime.onMessage.addListener((message) => {
if (message.type != 'get_analysis_response') return;
if (!(message.url in _anaylses)) return;
var url = message.url;
_anaylses[url] = _anaylses[url] || {};
_anaylses[url].links = (_anaylses[url].links || []).concat(message.links);
_anaylses[url].symbols_accessed = (_anaylses[url].symbols_accessed || []).concat(message.symbols_accessed);
_anaylses[url].done(_anaylses[url]);
});
function getAnalysis(tabId, url) {
_anaylses[url] = {};
sendMessage(tabId, {
type: 'get_analysis',
url: url
});
return new Promise((resolve, reject) => {
_anaylses[url].done = debounce(() => {
resolve(_anaylses[url]);
delete _anaylses[url];
}, _anaylsesDebounceTimeout);
});
}
async function beginCrawl(url, maxDepth) {
reset();
setAppState("crawling");
settings.root = url;
settings.maxDepth = maxDepth;
var urls = url.split(',');
urls.forEach((singleUrl) => {
allPages[singleUrl] = {url:singleUrl, state:"queued", depth:0};
});
startingPages = urls;
var allCookies = await getCookies();
console.log("Privacy Crawler: Deleting all cookies");
await Promise.all(allCookies.map(removeCookie));
console.log("Privacy Crawler: All cookies deleted");
crawlMore();
}
function getCookies() {
return new Promise((resolve, reject) => {
chrome.cookies.getAll({}, resolve);
});
}
function removeCookie(cookie) {
var url = (cookie.secure ? "https" : "http") + "://" + cookie.domain + cookie.path;
return new Promise((resolve, reject) => {
chrome.cookies.remove({"url": url, "name": cookie.name}, (details) => {
details === null ? reject() : resolve();
});
});
};
function onTabStatusComplete(tabId) {
return new Promise((resolve, reject) => {
var listener = (updatedTabId, info) => {
if (updatedTabId == tabId && info.status == 'complete') {
resolve();
chrome.tabs.onUpdated.removeListener(listener);
}
}
chrome.tabs.onUpdated.addListener(listener);
});
}
function sendMessage(tabId, message) {
return new Promise((resolve, reject) => {
chrome.tabs.sendMessage(tabId, message, resolve);
});
}
function cookieKey(cookie) {
return '___DOMAIN___' + cookie.domain + "___NAME___" + cookie.name + "___PATH___" + cookie.path;
}
async function crawlPage(page) {
chrome.tabs.update(targetTabId, {
url: page.url
});
await onTabStatusComplete(targetTabId);
// Wait for page (and iframes) to load
await timeout(1000);
var analysis = await getAnalysis(targetTabId, page.url);
var newPages = analysis.links.filter(function(linkURL) {
var anyStartsWith = startingPages.some(function(startingPage) {
return startsWith(linkURL, startingPage);
});
return anyStartsWith && !allPages[linkURL];
}).map((linkURL) => {
return {
depth: page.depth+1,
url: linkURL,
state: page.depth == settings.maxDepth ? "max_depth" : "queued"
}
});
return [newPages, analysis.symbols_accessed];
}
async function getNewCookies(page) {
var cookies = await getCookies();
return cookies.filter(function(cookie) {
return !(cookieKey(cookie) in allCookiesSeen)
}).map((cookie) => {
var expires = cookie.session ? 'session' : dateFns.distanceInWordsStrict(
new Date(),
cookie.expirationDate * 1000,
{partialMethod: 'ceil'}
);
return {
domain: cookie.domain,
path: cookie.path,
name: cookie.name,
expirationDate: expires,
firstSeen: page.url,
firstValue: cookie.value
};
});
}
function getNewSymbols(page, symbols) {
return uniq(symbols, symbolKey).filter((symbol) => {
return !(symbolKey(symbol) in allSymbolsSeen);
}).map((symbol) => {
return {
name: symbol.name,
scriptUrl: symbol.scriptUrl,
firstSeen: page.url,
isExtraSuspicious: isExtraSuspicious(symbol.name)
};
});
}
function timeoutUntilReject(ms, message) {
return new Promise((resolve, reject) => {
setTimeout(() => {
reject(message);
}, ms);
});
}
async function crawlMore() {
appState = "crawling";
while (appState == "crawling" && getURLsInTab("Queued").length > 0) {
var page = getURLsInTab("Queued")[0];
page.state = "crawling";
refreshPage();
var newPages;
var symbolsAccessed;
try {
console.log("Privacy Crawler: Crawling " + page.url);
[newPages, symbolsAccessed] = await Promise.race([crawlPage(page), timeoutUntilReject(pageLoadTimeout)]);
console.log("Privacy Crawler: Crawled " + page.url);
} catch(e) {
console.error("Privacy Crawler: Error crawling " + page.url, e);
page.state = "error";
newPages = [];
symbolsAccessed = [];
} finally {
page.state = page.state != "error" ? "crawled" : page.state;
}
newPages.forEach(function(page) {
allPages[page.url] = page;
});
// Even in the case of error, cookies, might have changed
var newCookies = await getNewCookies(page);
newCookies.forEach(function(cookie) {
allCookiesSeen[cookieKey(cookie)] = true
allCookies.push(cookie)
});
getNewSymbols(page, symbolsAccessed).forEach((symbol) => {
allSymbolsSeen[symbolKey(symbol)] = true;
allSymbols[symbol.scriptUrl] = allSymbols[symbol.scriptUrl] || [];
allSymbols[symbol.scriptUrl].push(symbol);
});
// Sort each in place
Object.values(allSymbols).forEach((symbolList) => {
symbolList.sort((a, b) => {
return isExtraSuspicious(a.name) && !isExtraSuspicious(b.name) ? -1 :
!isExtraSuspicious(a.name) && isExtraSuspicious(b.name) ? 1 :
a.name < b.name ? -1 :
a.name > b.name ? 1 :
0;
});
});
var sortedAllValues = Object.values(allSymbols).sort((a, b) => {
var numExtraSuspiciousA = a.filter((symbol) => {
return isExtraSuspicious(symbol.name);
}).length;
var numA = a.length;
var numExtraSuspiciousB = b.filter((symbol) => {
return isExtraSuspicious(symbol.name);
}).length;
var numB = b.length;
return numExtraSuspiciousA < numExtraSuspiciousB ? 1 :
numExtraSuspiciousA > numExtraSuspiciousB ? -1 :
numA < numB ? 1 :
numA > numB ? -1 :
a[0].scriptUrl < b[0].scriptUrl ? -1 :
a[0].scriptUrl > b[0].scriptUrl ? 1 :
a[0].firstSeen < b[0].firstSeen ? -1 :
a[0].firstSeen > b[0].firstSeen ? 1 :
0;
});
allSymbols = {};
sortedAllValues.forEach((symbols) => {
allSymbols[symbols[0].scriptUrl] = symbols;
});
latestUpdate = new Date();
}
// We are either finished, or we have paused
setAppState((appState == "pausing" && getURLsInTab("Queued").length) ? "paused" : "stopped");
refreshPage();
}
function getURLsInTab(tab) {
return Object.values(allPages).filter((o) => {
return (tab=="Queued" && o.state=="queued") ||
(tab=="Crawling" && o.state=="crawling") ||
(tab=="Crawled" && o.state=="crawled") ||
(tab=="Errors" && o.state=="error");
});
}
function pause() {
setAppState("pausing");
refreshPage();
}
function stop() {
setAppState("stopped");
refreshPage();
}
function reset() {
console.log('resetting');
setAppState("stopped");
allPages = {};
allCookiesSeen = {};
allCookies = [];
allSymbolsSeen = {};
allSymbols = {};
refreshPage();
}
function setAppState(newState) {
appState = newState;
if (targetTabId) {
chrome.tabs.sendMessage(targetTabId, {message: "app_state", app_state: appState});
}
}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message == 'app_state_request') sendResponse(appState);
});
function setBadgeText() {
var text = appState == 'crawling' || appState == 'pausing' ? '▶' : '';
chrome.browserAction.setBadgeText({tabId: targetTabId, text: text});
}
function refreshPage() {
chrome.runtime.sendMessage({message: "refresh_page", tabId: targetTabId});
setBadgeText();
}
// There doesn't seem to be a nicer way to get Chrome to consistently use
// a different icon in incognito. Might have to try to have the same icon
// that looks ok in both
function setLightIcon(tabId) {
chrome.browserAction.setIcon({path: 'images/paws_light.png', tabId: tabId});
setBadgeText();
}
if (chrome.extension.inIncognitoContext) {
(async function setIcon() {
var tabs = await tabQuery({active: true, currentWindow: true});
setLightIcon(tabs[0].id);
setBadgeText();
})();
chrome.tabs.onActivated.addListener((tab) => {
setLightIcon(tab.id);
});
chrome.tabs.onCreated.addListener((tab) => {
setLightIcon(tab.id);
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
setLightIcon(tab.id);
});
}
// The report is ordered with these at the top
var extraSuspicious = [
'AnalyserNode',
'AudioContext',
'CanvasRenderingContext2D',
'GainNode',
'HTMLCanvasElement',
'OfflineAudioContext',
'OscillatorNode',
'RTCPeerConnection',
'ScriptProcessorNode'
];
function isExtraSuspicious(name) {
return extraSuspicious.some((extraSuspiciousName) => {
return name.includes(extraSuspiciousName);
});
}