-
Notifications
You must be signed in to change notification settings - Fork 5
/
domains.js
84 lines (56 loc) · 1.8 KB
/
domains.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
//add tags to domains
function addDomainTags(){
if (domainsWorking) {
setTimeout(function() { addDomainTags(); }, 100);
return;
}
domainsWorking = true;
let domainElems = getDomainElements();
for (let tag in domainElems) {
for (let domain in domainElems[tag]) {
domainElems[tag][domain].forEach(function (domSpan) {
setTimeout(function() { addDomainTag(domSpan, tag); }, Math.random() * 1000);
});
}
}
setTimeout(function() { domainsWorking = false; }, 1000);
}
// get list of domains which need tags
function getDomainElements() {
let domains = {};
let domainElems = document.evaluate(
'//span[(contains(@class, "domain") or contains(@class, "b5szba-0"))]',
document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
let i = 0;
while (domainElems.snapshotItem(i)) {
let domSpan = domainElems.snapshotItem(i);
let domain = domSpan.textContent.replace(/[\(\)]/g, '');
for (let tag in settings.tags.domains) {
if (settings.tags.domains[tag].list.includes(domain)) {
// if tag not enabled, skip it
if (!settings.tags.domains[tag].enabled) { continue; }
// if already tagged, skip this element
if (domSpan.parentNode.children[0].nodeName == 'SPAN') { continue; }
if (!(tag in domains)) {
domains[tag] = {};
}
if (!(domain in domains[tag])) {
domains[tag][domain] = [];
}
domains[tag][domain].push(domSpan);
}
}
i++;
}
return domains;
}
// create and insert tag
function addDomainTag(elem, tag) {
// console.log(tag, elem);
let span = document.createElement('span');
span.textContent = tag;
span.className = 'rptTag rptDomain';
span.style.backgroundColor = '#' + settings.tags.domains[tag].color;
span.style.color = '#' + settings.tags.domains[tag].tcolor;
elem.parentNode.prepend(span);
}