forked from insightbrowser/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probable_adblock.js
173 lines (162 loc) · 4.95 KB
/
probable_adblock.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
const adBlockers = [
{
"site": "twitter.com",
"adText": "Promoted",
"adElementSelector": "article"
},
{
"site": "facebook.com",
"adText": "Sponsored",
"adElementSelector": "article"
},
{
"site": "reddit.com",
"adText": "Promoted, Sponsored",
"adElementSelector": "article"
},
{
"site": "m.facebook.com",
"adText": "Sponsored",
"adElementSelector": "article"
},
{
"site": "instagram.com",
"adText": "Sponsored",
"adElementSelector": "article"
},
{
"site": "mobile.twitter.com",
"adText": "Promoted",
"adElementSelector": "article"
},
{
"site": "amazon.com",
"adText": "Sponsored",
"adElementSelector": "div.s-result-item"
},
{
"site": "m.youtube.com",
"adTextContainer": "ytm-badge",
"adText": "AD",
"adElementSelector": "ytm-item-section-renderer"
},
{
"site": "m.youtube.com",
"adTextContainer": "ytm-badge",
"adText": "AD",
"adElementSelector": "ytm-rich-item-renderer"
},
{
"site": "linkedin.com",
"adText": "Promoted",
"adElementSelector": "li.feed-item"
},
{
"site": "google.com",
"adText": "Ad,Ad·",
"adElementSelector": "#tads"
},
{
"site": "google.com",
"adText": "Ads",
"adElementSelector": ".mnr-c"
},
{
"site": "google.com",
"adText": "Ads",
"adElementSelector": "._-is"
},
{
"site": "google.com",
"adText": "Ads",
"adElementSelector": ".commercial-unit-mobile-top"
},
{
"site": "google.com",
"adText": "Ads,Ads·",
"adElementSelector": ".ptJHdc"
},
{
"site": "duckduckgo.com",
"adText": "Ad",
"adElementSelector": ".result--ad"
}
]
const host = document.location.host.replace('www.', '');
const adBlocks = adBlockers.filter(adBlock => {
var site = adBlock.site.split(',');
return site.includes(host);
});
adBlocks.forEach((adBlock) => {
var adText = adBlock.adText.split(',');
var adTextContainer = adBlock.adTextContainer || 'span';
var adElementSelector = adBlock.adElementSelector;
setInterval(function() {
var search = adText.map(adText => 'normalize-space()=\'' + adText + '\'').join(' or ');
var xpath = "//" + adTextContainer + "[" + search + "]";
var matchingElements = document.evaluate(xpath, document, null, XPathResult.ANY_TYPE, null);
var nodes = [];
var node;
while(node = matchingElements.iterateNext()) {
nodes.push(node)
}
adBlockNodes(nodes, adElementSelector);
}, 1000);
});
const parentIsAdBlocked = (element) => {
if (element === document.body) { return false; }
return element.getAttribute('adblocked') === 'true' ||
element.parentNode && parentIsAdBlocked(element.parentNode);
};
const adBlockNodes = (nodes, adElementSelector) => {
for (let node of nodes) {
let adstory = node.closest(adElementSelector)
// Preventing same adstory to be handled more than once
if (!adstory || adstory.getAttribute('adblocked') === 'true') {
continue;
}
// Preventing ad block inside ad block
if (adstory.parentNode && parentIsAdBlocked(adstory.parentNode)) {
continue;
}
adstory.setAttribute('adblocked', 'true');
let overlay = document.createElement('div')
overlay.setAttribute('style', `
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
position: absolute; left: 0; top: 0; right: 0; bottom: 0;
background: linear-gradient(hsla(0,0%,100%,.9) 0%,#fff);
z-index: 2147483647`);
overlay.setAttribute('class', 'adblock');
let overlaytext = document.createElement('div');
overlaytext.setAttribute('style', `
position: absolute; left: 20px; top: 30px;
font-weight: bold;
font-size: 24px;
color:#444;`)
overlaytext.innerText = 'Ad';
let overlaytextinner = document.createElement('div');
overlaytextinner.setAttribute('style', `
font-weight: normal;
margin-top: 10px;
font-size: 16px;`);
overlaytextinner.innerText = 'Blocked by Hyperweb. Tap to show likely ad.'
overlay.appendChild(overlaytext);
overlaytext.appendChild(overlaytextinner);
overlay.addEventListener("click", (e) => {
if (adstory.getAttribute('adblock-protected') !== 'true') {
e.preventDefault();
let ol = e.target.closest('.adblock');
ol.parentElement.style.maxHeight = 'none';
ol.parentElement.style.overflow = 'auto';
ol.parentNode.removeChild(ol);
adstory.setAttribute('adblock-protected', 'true');
}
});
if (adstory.querySelectorAll('.adblock').length === 0 && adstory.getAttribute('adblock-protected') !== 'true') {
adstory.style.position = "relative"
adstory.style.maxHeight = '120px';
adstory.style.overflow = 'hidden';
adstory.insertBefore(overlay, adstory.firstChild);
}
}
};