-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetUrlsScript.js
executable file
·83 lines (65 loc) · 2.16 KB
/
getUrlsScript.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
function extractUrls(domNodes = document.body) {
var allUrls = []
var googUrls = []
if (!domNodes) return;
try {
allUrls = domNodes.querySelectorAll('a[href^=http]') // Most websites
googUrls = domNodes.querySelectorAll('a[href^="./articles"]') // On news.google.com
} catch (error) {
// TODO: remove this logging, occurs when the domNodes object isn't valid
console.log(`myBias error parsing urls: ${error}`)
}
var domain = window.location.href;
domain = url2Domain(domain)
var urlList = [];
for (i = 0; i < allUrls.length; i++) {
var url = allUrls[i].href
urlList.push(url);
}
for (i = 0; i < googUrls.length; i++) {
var url = googUrls[i].href
urlList.push(url);
}
if (allUrls.length == 0) {
return
}
chrome.runtime.sendMessage({
content: urlList,
currentDomain: domain
}, function (response) {
const lastErr = chrome.runtime.lastError;
if (lastErr) {
console.log(`myBias Logging Error: ` + JSON.stringify(lastErr));
} else {
console.log("myBias URLs logged")
}
});
}
extractUrls()
// Fires constantly:
MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var mutationCache = new Set() // Cache to prevent the same mutation from being read twice
var observer = new MutationObserver(function (mutations, observer) {
// fired when a mutation occurs
for (let mutation of mutations) {
if (!mutationCache.has(mutation)) {
mutationCache.add(mutation);
for (let node of mutation.addedNodes) {
extractUrls(node)
}
}
}
});
gStorageAPI.get({
followDeepLinks: false
}, function (items) {
if (items.followDeepLinks) {
// define what element should be observed by the observer
// and what types of mutations trigger the callback
observer.observe(document.body, {
subtree: true,
attributes: true,
childList: true
});
}
})