forked from citp/ad-blocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
executable file
·240 lines (213 loc) · 9.65 KB
/
content.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
/* ----------------------------------------------------------------------------------
* Authors: Grant Storey & Dillon Reisman
* Written: Dec '16
* Last Updated: 3/7/17
* Description: Content script that runs in iframes, determines whether they
* contain an adchoices icon, and if so covers them with a red box that says
* "Adchoices identified." Can optionally overlay non ads with "Adchoices not identified."
* as well for testing purposes.
* Dependencies: jquery, perceptual_background.js, image_search.js, utils.js.
* ----------------------------------------------------------------------------------
*/
// The perceptual adblocker logic.
// stores repeated check interval id to allow it to be canceled
var intervalID;
var intervalIDTop;
// This response is triggered by the background script.
// If the background script found adchoices, then response.element
// will have a stringified version of the dom element that triggered it.
var handleBkgdResponse = function(response) {
if (typeof response === 'undefined'){
return true;
}
if ('element' in response) {
//console.log(response['element']);
// cover the body, place text "ADCHOICES IDENTIFIED", no local info,
// not only the deepest container, this is an ad, there is an interval,
// and the interval's id is intervalID
if (response.isIFrame !== undefined && !response.isIFrame) {
let ad_cover_label = "TOP AD IDENTIFIED";
if (response.searchSrc === "ByURL") {
ad_cover_label = "WHY AD IDENTIFIED"
}
if (response.searchSrc === "ByText") {
ad_cover_label = "TEXT AD IDENTIFIED"
}
if (response.src_url !== undefined && srcUrlToIframeContainer.has(response.src_url)) {
for (let frame_container of srcUrlToIframeContainer.get(response.src_url)) {
try {
//console.log("is NOT iframe with src url " + response.src_url);
coverContainer($(frame_container), ad_cover_label, "", false,
true, false, null, response.src_url, response.searchSrc);
//console.log("covering ad from the top frame");
} catch(error) {
console.warn("Could not cover ad from the top");
}
}
}
} else {
if (inIframe()) {
let ad_cover_label = "AD IDENTIFIED";
if (response.searchSrc === "ByURL") {
ad_cover_label = "WHY AD IDENTIFIED"
}
if (response.searchSrc === "ByText") {
ad_cover_label = "TEXT AD IDENTIFIED"
}
coverContainer($('body'), ad_cover_label, "", false,
true, true, intervalID, response.src_url, response.searchSrc);
}
}
}
else if ('no_element' in response) {
//console.log('Not adchoices image!');
// cover the body, place text "NOT ADCHOICES", no local info,
// not only the deepest container, this is not an ad, there is an
// interval, and the interval's id is intervalID
coverContainer($('body'), "NOT AD", "", false,
false, true, intervalID);
}
return true;
};
// return whether this is in an iFrame
// http://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}
function try_process_frame_from_top(first_frame) {
let frames_to_explore = [first_frame];
while (frames_to_explore.length > 0) {
let frame = frames_to_explore.pop()
try {
frame.contentWindow; // tests if we can access this, this will error out if we cannot access it.
let frame_body = frame.contentWindow.document.body;
let frame_document = frame.contentWindow.document;
if (frame_body && !alreadyCoveredSameType($(frame_body), false)) {
frame_body.classList.add("AdHighlighterObservedFromTopFrame");
// uncomment this for debugging to make sure that the container
// with the adchoices icon has been examined at all.
//$('body').addClass("CITPObserved");
runImageSearch($(frame_body), frame_document, handleBkgdResponse);
runURLSearch($(frame_body), frame_document, handleBkgdResponse);
runTextSearch($(frame_body), frame_document, handleBkgdResponse);
for (let subframe of frame.contentWindow.document.getElementsByTagName("iframe")) {
frames_to_explore.push(subframe);
}
}
} catch (error) {
//console.log(error);
}
frame.classList.add("AdHighlighterObserved");
}
}
var MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
// if we are in an iframe (which contains the vast majority of adchoices ads)
if (inIframe()) {
// set an interval to check every 2 seconds. Probably
// best to do some sort of DOM observer trigger for a production version
// of this, but the interval works fine for demo purposes.
intervalID = setInterval(function() {
// Only consider the iframe if it is larger than 1x1
if (document.body && !alreadyCoveredSameType($(document.body), false)) {
document.body.classList.add("AdHighlighterObserved");
// uncomment this for debugging to make sure that the container
// with the adchoices icon has been examined at all.
//$('body').addClass("CITPObserved");
const body_node = $('body');
runImageSearch(body_node, document, handleBkgdResponse);
runURLSearch(body_node, document, handleBkgdResponse);
runTextSearch(body_node, document, handleBkgdResponse);
for (let frame of document.getElementsByTagName("iframe")) {
try_process_frame_from_top(frame);
}
}
}, 2000);
} else {
// relying on this loop is better because we don't know when the iframe will be done loading
intervalIDTop = setInterval(function() {
for (let frame of document.getElementsByTagName("iframe")) {
try_process_frame_from_top(frame);
}
}, 2000);
/* To set this event off, inject JS into the page that does this:
var event = new Event('reset_adchoice_counter');
document.dispatchEvent(event);
*/
document.addEventListener("reset_adchoice_counter", event => {
console.log("Received request from site to reset the adchoice counter");
chrome.runtime.sendMessage({reset_adchoice_counter: true});
}, true);
/* To set this event off, inject JS into the page that does this:
var event = new Event('set_adchoice_total_in_DOM');
document.dispatchEvent(event);
*/
// add a DOM element with the value of ad_choices_found
document.addEventListener("set_adchoice_total_in_DOM", event => {
console.log("Received request from site to add adchoice total to the DOM");
chrome.runtime.sendMessage({get_adchoice_total_in_DOM: true},
function(response) {
if (typeof response === 'undefined'){
console.log("Could not retrieve the adchoice total ")
return true;
}
let ad_choices_total = response["ad_choices_total"];
console.log("Retrieved ad_choices_total " + ad_choices_total);
let ad_urls = response["src_urls"];
const el_id = "ad-highlighter-counter";
let el = document.getElementById(el_id);
if (el == null) {
el = document.createElement("div");
el.setAttribute("id", el_id)
document.body.appendChild(el);
}
// update total
el.setAttribute("total", ad_choices_total);
// update urls of logos
el.innerText = ad_urls;
// potential ad urls
let potential_urls = response["potential_ads_urls"];
const el_id_2 = "ad-highlighter-potential-urls";
let el2 = document.getElementById(el_id_2);
if (el2 == null) {
el2 = document.createElement("div");
el2.setAttribute("id", el_id_2)
document.body.appendChild(el2);
}
// update total
el2.setAttribute("total", potential_urls.length);
// update urls of logos
el2.innerText = potential_urls;
return true;
}
);
}, true);
document.addEventListener("set_dissimilar_hashes_in_DOM", event => {
console.log("Received request from site to add dissimilar hashes to the DOM");
chrome.runtime.sendMessage({get_dissimilar_hashes_in_DOM: true},
function(response) {
if (typeof response === 'undefined'){
console.log("Could not retrieve the dissimilar hashes ")
return true;
}
let hashes = response["hashes"];
const el_class = "ad-highlighter-hashes";
for (let src_url in hashes) {
let values = hashes[src_url];
let hex = values["hex"]
let sim = values["sim"]
let el = document.createElement("div");
el.setAttribute("class", el_class);
el.setAttribute("src_url", src_url);
el.setAttribute("hex", hex);
el.setAttribute("sim", sim);
document.body.appendChild(el);
}
return true;
}
);
}, true);
}