forked from citp/ad-blocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_search.js
executable file
·320 lines (283 loc) · 11.9 KB
/
image_search.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
/* ----------------------------------------------------------------------------------
* Authors: Grant Storey & Dillon Reisman
* Written: 12/28/16
* Last Updated: 3/5/17
* Description: This provides helper functions for image searching using
* fuzzy hashing.
* Dependencies: jquery, perceptual_background.js
* ----------------------------------------------------------------------------------
*/
const srcUrlToIframeContainer = new Map();
const urls_to_match = [
"adssettings.google.com/whythisad",
"https://www.outbrain.com/what-is/",
"optout.aboutads.info"
];
const text_only_ads_to_match = [
"Ad",
"ad",
"Advertisement",
"advertisement",
"ADVERTISEMENT",
"PR",
"广告",
"Sponsored Content",
"SPONSORED CONTENT",
];
// this function hardcodes the styles of oldElement onto newElement;
function hardcodeStyles(oldElement, newElement) {
var computedStyle = window.getComputedStyle(oldElement, null);
for (prop in computedStyle) {
newElement.style[prop] = computedStyle[prop];
}
}
// recursively apply the map function to the children
// of oldElement and its clone newElement
function recursiveMap(oldElement, newElement, mapFunction) {
mapFunction(oldElement, newElement);
for (var i = 0; i < oldElement.children.length; i++) {
var oldChild = oldElement.children[i];
var newChild = newElement.children[i];
recursiveMap(oldChild, newChild, mapFunction)
}
}
// create and return a deep copy of element with
// mapFunction (taking the old element and new element)
// applied to each child element.
function deepMap(element, mapFunction) {
var newEl = element.cloneNode(true);
recursiveMap(element, newEl, mapFunction);
return newEl;
}
// given an svg, determine the bounding box that contains all pixels with
// alpha above alphaThreshold. This allows cropping of images with large
// transparency margins.
var contextBoundingBox = function(ctx,alphaThreshold){
if (alphaThreshold===undefined) alphaThreshold = 15;
var minX=Infinity,minY=Infinity,maxX=-Infinity,maxY=-Infinity;
var w=ctx.canvas.width,h=ctx.canvas.height;
var data = ctx.getImageData(0,0,w,h).data;
for (var x=0;x<w;++x){
for (var y=0;y<h;++y){
var a = data[(w*y+x)*4+3];
if (a>alphaThreshold){
if (x>maxX) maxX=x;
if (x<minX) minX=x;
if (y>maxY) maxY=y;
if (y<minY) minY=y;
}
}
}
return {x:minX,y:minY,maxX:maxX,maxY:maxY,w:maxX-minX,h:maxY-minY};
}
// given an img and crop dimensions, crop the image and get the dataURL
// associated with it.
//http://stackoverflow.com/questions/34242155/how-to-crop-canvas-todataurl
function cropPlusExport(img,cropX,cropY,cropWidth,cropHeight){
// create a temporary canvas sized to the cropped size
var canvas1=document.createElement('canvas');
var ctx1=canvas1.getContext('2d');
canvas1.width=cropWidth;
canvas1.height=cropHeight;
// use the extended from of drawImage to draw the
// cropped area to the temp canvas
ctx1.drawImage(img,cropX,cropY,cropWidth,cropHeight,0,0,cropWidth,cropHeight);
// return the .toDataURL of the temp canvas
return(canvas1.toDataURL());
}
function getBackgroundImagePos(element, url) {
let bgXPos = null;
let bgYPos = null;
let backgroundSizeWidth = null;
let backgroundSizeHeight = null;
let imgStyle = window.getComputedStyle(element);
//console.log(imgStyle);
//console.log(imgStyle.backgroundPositionX);
//console.log(imgStyle.backgroundPositionY);
if (imgStyle.backgroundPositionX && imgStyle.backgroundPositionX.length > 0
&& (imgStyle.backgroundPositionX.indexOf("%") === -1 || imgStyle.backgroundPositionX === "0%")) {
bgXPos = parseInt(imgStyle.backgroundPositionX.replace("px", "").replace("%", ""));
//console.log("bgXPos " + bgXPos);
//console.log(element);
}
if (imgStyle.backgroundPositionY && imgStyle.backgroundPositionY.length > 0
&& (imgStyle.backgroundPositionY.indexOf("%") === -1 || imgStyle.backgroundPositionY === "0%") ) {
bgYPos = parseInt(imgStyle.backgroundPositionY.replace("px", "").replace("%", ""));
//console.log("bgYPos " + bgYPos);
}
if (imgStyle.backgroundSize && imgStyle.backgroundSize.length > 0
&& (imgStyle.backgroundSize.indexOf("%") === -1) && imgStyle.backgroundSize.indexOf("auto") === -1) {
//console.log(imgStyle.backgroundSize);
let stSplit = imgStyle.backgroundSize.split(" ");
if (stSplit.length === 2) {
backgroundSizeWidth = stSplit[0].replace("px", "");
backgroundSizeHeight = stSplit[1].replace("px", "")
}
}
return [bgXPos, bgYPos, backgroundSizeWidth, backgroundSizeHeight];
}
// run an image search on all images (in the src of img tags, in the background
// of divs, links, and spans, and in svgs) found in container, and if any
// matches are found run the result function.
function runImageSearch(container, container_document, resultFunction) {
const container_element = container[0];
// Case 1: adchoices icon is image
container.find('img').each(function( index, element) {
element.classList.add("AdHighlighterConsidered");
const src_url = $(element)[0].src;
if ($(element)[0].width > 1 || $(element)[0].height > 1) {
if (src_url) {
if (!srcUrlToIframeContainer.has(src_url)) {
srcUrlToIframeContainer.set(src_url, []);
}
srcUrlToIframeContainer.get(src_url).push(container_element);
//console.log("found image " + src_url);
chrome.runtime.sendMessage({
data: [src_url, $(element).prop('outerHTML')],
isIFrame: inIframe(),
originalWidth: element.width || element.offsetWidth,
originalHeight: element.height || element.offsetHeight,
searchSrc: "img",
}, resultFunction);
}
} else {
//console.log("image too small " + src_url);
}
});
// Case 2: adchoices icon is a div background
// may need to add more elements to this list as advertisers get
// adversarial.
container.find('div,a,span').each(function(index, element) {
//element.classList.add("AdHighlighterConsidered");
var bg = $(element).css('background-image');
bg = bg.replace('url(','').replace(')','').replace(/\"/gi, "");
if (bg && bg !== 'none') {
if (!srcUrlToIframeContainer.has(bg)) {
srcUrlToIframeContainer.set(bg, []);
}
srcUrlToIframeContainer.get(bg).push(container_element);
let bgXPos = null;
let bgYPos = null;
let backgroundSizeWidth = null;
let backgroundSizeHeight = null;
if(bg.indexOf("http") !== -1 || bg.indexOf("adchoice") !== -1 || bg.startsWith("data:")) {
let bgPos = getBackgroundImagePos(element, bg);
bgXPos = bgPos[0];
bgYPos = bgPos[1];
backgroundSizeWidth = bgPos[2];
backgroundSizeHeight = bgPos[3];
//console.log({bg:bg, originalWidth: element.width || element.offsetWidth,
// originalHeight: element.height || element.offsetHeight,
// bgXPos: bgXPos, bgYPos: bgYPos, backgroundSizeWidth:backgroundSizeWidth, backgroundSizeHeight:backgroundSizeHeight});
}
chrome.runtime.sendMessage(
{data: [bg, $(element).prop('outerHTML'), "bg"],
isIFrame: inIframe(),
searchSrc: "bg",
originalWidth: element.width || element.offsetWidth,
originalHeight: element.height || element.offsetHeight,
bgXPos: bgXPos, bgYPos: bgYPos,
backgroundSizeWidth:backgroundSizeWidth, backgroundSizeHeight:backgroundSizeHeight
}, resultFunction);
}
});
// Case 3; adchoices icon is an <svg> element
container.find('svg').each(function(index, element) {
// if for some reason the exact styles are needed, use
// this (el_2) instead of "element" below.
// example use case would be if an advertiser drew a large square
// over the adchoices icon but then hid the square using a stylesheet
// in another part of the code; in the current configuration this
// style would not be carried over to the background script's
// analysis, but by using deepMap they would.
//var el_2 = deepMap(element, hardcodeStyles);
element.classList.add("AdHighlighterConsidered");
var svg_html = $(element).prop('outerHTML');
// Wrap svg in new <svg><foreignObject> element
var wrapper_open = '<svg xmlns="http://www.w3.org/2000/svg" width="100" height="200">' +
'<foreignObject width="100%" height="100%">';
var wrapper_close = '</foreignObject></svg>';
var data_string = wrapper_open + svg_html + wrapper_close;
var img = new Image();
img.crossOrigin="CITPymous";
var url = 'data:image/svg+xml;charset=utf8, ' + encodeURIComponent(data_string);
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var margin = 2;
img.onload = function () {
ctx.drawImage(img,0,0);
var crop = contextBoundingBox(ctx)
var dataURL = cropPlusExport(img, crop.x-margin, crop.y-margin, crop.w+(2*margin), crop.h+(2*margin));
if (!srcUrlToIframeContainer.has(dataURL)) {
srcUrlToIframeContainer.set(dataURL, []);
}
srcUrlToIframeContainer.get(dataURL).push(container_element);
chrome.runtime.sendMessage({data: [dataURL, svg_html], isIFrame: inIframe(), searchSrc: "svg"},
resultFunction);
}
img.src = url;
});
}
// run a search for a links with href that has suffix
function runURLSearch(container, container_document, resultFunction) {
const container_element = container[0];
// Find a links
container.find('a').each(function(index, element) {
var href = $(element)[0].href;
if (href && !element.classList.contains("AdHighlighterConsidered")) {
element.classList.add("AdHighlighterConsidered");
if (!srcUrlToIframeContainer.has(href)) {
srcUrlToIframeContainer.set(href, []);
}
srcUrlToIframeContainer.get(href).push(container_element);
let match = "";
for (let i = 0; i < urls_to_match.length; i++) {
let url_tmp = urls_to_match[i];
if (href.indexOf(url_tmp) > -1) {
match = url_tmp;
break;
}
}
if (match !== "") {
//console.log("Found AD by matching url " + match);
resultFunction({
element: element,
isIFrame: inIframe(),
src_url: href,
searchSrc: "ByURL"
});
} else {
resultFunction({no_element: "Inside!"});
}
}
});
}
// run a search for a links with href that has suffix
function runTextSearch(container, container_document, resultFunction) {
const container_element = container[0];
for (let i = 0; i < text_only_ads_to_match.length; i++) {
let ad_label = text_only_ads_to_match[i];
let xpath_query = './/*[text()="' + ad_label + '"]';
let found_elements = container_document.evaluate(xpath_query,
container_element,
null,
XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
null);
if(found_elements.snapshotLength > 0) {
for ( let j = 0 ; j < found_elements.snapshotLength; j++){
let el = found_elements.snapshotItem(j);
let key = xpath_query + j;
if (!srcUrlToIframeContainer.has(key)) {
srcUrlToIframeContainer.set(key, []);
}
srcUrlToIframeContainer.get(key).push(container_element);
resultFunction({
element: el,
isIFrame: inIframe(),
src_url: key,
searchSrc: "ByText"
});
}
}
}
}