forked from citp/ad-blocking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
executable file
·163 lines (143 loc) · 5.65 KB
/
utils.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
/* ----------------------------------------------------------------------------------
* Authors: Grant Storey & Dillon Reisman
* Written: 3/5/17
* Last Updated: 3/7/17
* Description: Helper code for covering advertisements that have been found.
* Dependencies: jquery.
* ----------------------------------------------------------------------------------
*/
// sets logging level
var VERBOSE = false;
if (typeof NON_ENGLISH_LOCALE === 'undefined') {
var NON_ENGLISH_LOCALE = false;
}
// return whether the container is already covered
function alreadyCovered(container) {
return (container.find(".FAH_adBlockerCover").length > 0);
}
// true if we want to overlay non-ads as well
var showNonAd = false;
// return whether the container is already covered by the same type
// of container. That is, if there is already an ad or non-ad container
// we don't want to ad a container of a new type, but if an adchoices
// icon has been added and it changed from non-ad to ad, we do want
// to update.
function alreadyCoveredSameType(container, newCoverIsAd) {
var alreadyCovered = (container.find(".CITP_adBlockerCover").length > 0);
var alreadyAd = (container.find(".CITP_isAnAd").length > 0)
return alreadyCovered && (alreadyAd || !newCoverIsAd);
}
// Add a cover with "THIS IS AN AD" and the "Sponsored" text in the given
// locale's language (if non-english).
// container is the container to cover.
// coverText is the text to show on the cover
// matchingText only has a value if we are on Facebook in a non-english locale.
// deepestOnly is true if we only want to include the deepest cover for this
// area.
// isAd is true if it is an ad
// hasInterval is true if there is an interval check associated with this cover
// intervalID is the id of that interval
// src_url is the URL of the image
// searchSrc is the source of how we found the image
function coverContainer(container, coverText, matchingText, deepestOnly, isAd,
hasInterval, intervalID, src_url, searchSrc) {
// if we aren't doing anything to non-ads and this isn't an ad, do nothing.
if (!isAd && !showNonAd) {
return false;
}
// don't cover if this container is already covered;
if (alreadyCoveredSameType(container, false)) {
return false;
}
if (isAd) {
// notify background that it covered ads
chrome.runtime.sendMessage({ad_covered: true, src_url: src_url});
}
// remove any existing covers (if we are moving from non-ad to ad)
container.find(".CITP_adBlockerCover").remove();
// vary the color and classes based on whether this is an ad or not.
var color;
var classes = "CITP_adBlockerCover";
var border_style = "auto";
var border_color = "white";
if (isAd) {
if (searchSrc) {
classes += " " + searchSrc;
}
color = "rgba(255, 0, 0, 0.2)";
border_style = "solid";
border_color = "red";
classes += " CITP_isAnAd";
} else {
if (showNonAd) {
color = "rgba(255, 255, 255, 0.8)";
}
}
// some google ads have a height of 0 and then everything in overflow,
// so if that is the case set the height of the cover to be the overflow
// height.
var setHeight;
var containerHeight = container.height();
var containerScrollHeight = container.prop('scrollHeight');
if (containerHeight === 0 && containerScrollHeight > 0) {
setHeight = containerScrollHeight;
} else {
setHeight = "100%"
}
var src_url_tmp = "";
if (src_url) {
src_url_tmp = src_url;
}
// create the cover to prepend.
var prepend = "<div class=\"" + classes + "\" data-ad-highlighter-src-url=\"" + src_url_tmp + "\" style=\"height: " + setHeight + ";position: absolute; width: 100%; background-color: " + color + " !important;z-index: 2147483648; visibility: visible;\">";
prepend += "<div class=\"FAH_closeButton\" style=\"position: absolute; right: 5px; top: 5px; cursor: pointer; padding: 0px 3px; border: 1px solid black; border-radius: 5px;\">";
prepend += "<strong>";
prepend += "X";
prepend += "</strong>";
prepend += "</div>";
prepend += "<div style=\"width: 100%;text-align:center;\">";
prepend += "<span style=\"color: black; font-size:30px;\">";
prepend += coverText;
prepend += "</span>";
// if we have "Sponsored" text in another language, add it below "THIS IS AN AD"
if (NON_ENGLISH_LOCALE && matchingText !== "") {
prepend += "<br/>"
prepend += "<span style=\"color: black; font-size:40px; background: rgba(255,255,255,.8);\">";
prepend += "(" + matchingText + ")";
prepend += "</span>";
}
prepend += "</div>";
prepend += "</div>";
var myPrepend = prepend;
// if we only want the deepest, remove any above this
if (deepestOnly) {
container.parents().each(function (index) {
$(this).children(".FAH_adBlockerCover").remove();
});
}
// if we only want the deepest covers and there is a cover within
// this container already, don't ad this cover.
if (!deepestOnly || !(container.find(".FAH_adBlockerCover").length > 0)) {
// prepend the cover
container.css("position", "relative");
container.prepend(myPrepend);
// make sure the close button closes the cover
container.children().children(".FAH_closeButton").on("click", function () {
container.children(".FAH_adBlockerCover").css("visibility", "hidden");
});
}
// if this is an ad and we have an interval, stop the search for ads.
if (hasInterval && isAd) {
clearInterval(intervalID);
}
}
/******************************************************
***** Demo of link clicking by x/y coordinate. This will work on a logged-in
***** facebook.com page.
***** The code clicks on the user's profile in the top bar.
*/
/*
setTimeout(function() {
simulateClickByPoint(502,21);
}, 5000);
*/