This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
blocker.js
238 lines (205 loc) · 7.5 KB
/
blocker.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
(function (){
"use strict";
// Initiate closures
var then = "", rval = "";
var fromCache = false, fullblock = false;
var blockLists = [], types = [];
var type = 0;
// Defined closures
var tab = window.location.toString(), anchors = document.getElementsByTagName('a')
// Objects
var master = new Object(), slave = new Object(), now = new Date(), weekFromNow = new Date(now.getTime() + 604800000);
master.sites = [];
now.setHours(0,0,0,0);
// Indexes
var line = 0, k = 0, j = 0, a = 0, l = 0, index = 0;
// Lib functions
function retrieve(key) {
chrome.storage.local.get(key, function(result){
rval = result[key];
}); return rval;
}
function provide(slaveData) {
chrome.runtime.onMessage.addListener(
function(message, sender, sendResponse) {
sendResponse(slaveData);
}
);
}
// Runtime functions
window.onload = function() {
getLists();
document.styleSheets[0].addRule('.FNB_link:before','content: url(https://github.com/Fdebijl/FakeNewsBlocker/raw/master/logo16_fake.png); display: inline !important; visibility: visible !important; height: 1em !important; width: 1em !important; z-index: 999999999 !important; paddhing: .3em !important; ');
};
// Retrieve all the lists as set in options. If none are set, load the default list from our Github repo.
function getLists() {
chrome.storage.sync.get({
lists: "https://raw.githubusercontent.com/Fdebijl/FakeNewsBlocker/master/blocklist.txt",
blacklist: false,
types: [1,2],
fullblock: false
}, function(items) {
if (items.lists.split(/\r\n|\r|\n/).length > 1) {
blockLists = (items.lists.split('\n'));
} else {
blockLists = [items.lists];
}
types = items.types;
fullblock = items.fullblock;
if (items.blacklist) {
master.blacklist = items.blacklist.split("\n");
}
if (JSON.stringify(master) != retrieve("FakeNews_blockList")) {
// Setting has changed since we last imported the blocklists, time to craft a new master list
loadFile(1);
} else {
loadFile();
}
});
}
// Import the blocklists as generated by getLists
function loadFile(skipCheck) {
if (retrieve("FakeNews_blocklist") != null && skipCheck !== 1) {
then = retrieve("FakeNews_expires");
if (then < now || typeof(then) == null) {
loadFile(1);
} else {
fromCache = true;
matchURL();
}
} else {
buildList();
}
}
// Construct our master blocklist from all supplied URL's and the one-off blacklist
function buildList() {
var link = blockLists[index];
var limit = blockLists.length;
if (index === limit) {
chrome.storage.local.set({
'FakeNews_blocklist': JSON.stringify(master),
'FakeNews_expires': weekFromNow.toString()
}, function() {
matchURL();
});
return;
}
var reader = new XMLHttpRequest();
reader.open('get', link, true);
reader.onreadystatechange = function() {
if (reader.readyState === 4) {
var exar = blockLists[index].split(".");
if (exar[exar.length - 1] === "json") {
try {
var thisList = JSON.parse(reader.responseText);
} catch (e) {
alert(e + " - Invalid JSON in blocklist at " + link);
}
for (j = 0; j < thisList.sites.length; j++) {
var y = {
"url": thisList.sites[j].url,
"type": thisList.sites[j].type,
"proof": thisList.sites[j].proof,
"notes": thisList.sites[j].notes,
"origin": blockLists[index],
"name": thisList.name,
"author": thisList.author,
"version": thisList.version,
"kind": "Advanced"
};
console.dir(y);
master.sites.push(y);
}
} else {
var listArray = reader.responseText.split("\n");
for (k = 0; k < listArray.length; k++) {
var x = {
"url": (listArray[k]).split(',')[0],
"type": (listArray[k]).split(',')[1],
"proof": (listArray[k]).split(',')[2],
"notes": (listArray[k]).split(',')[3],
"origin": blockLists[index],
"kind": "Simple"
};
master.sites.push(x);
}
}
index++;
buildList();
}
};
reader.send(null);
}
function matchURL() {
master = fromCache ? JSON.parse(retrieve("FakeNews_blocklist")) : master;
var lines = master.sites;
// Looping over the master blocklist
// lines: array of all lines in the master blocklist
// line: pointer (index) for the current line
for (line = 0; line < lines.length; line++) {
var url = lines[line].url,
type = Number(lines[line].type),
proof = lines[line].proof,
notes = lines[line].notes,
origin = lines[line].origin,
name = lines[line].name,
version = lines[line].version,
kind = lines[line].kind;
if (url.toUpperCase() == location.host.replace('www.','').toUpperCase() && types.indexOf(type) > -1 && fullblock) {
var blockr = document.createElement('div');
var blockrCore = document.createElement('div');
var blockrImage = document.createElement('img');
var blockrButton = document.createElement('button');
blockrImage.src = chrome.extension.getURL("icons/logo128_fake.png");
var blockrStyle = 'display:block;position:fixed;top:0;left:0;height:100vh;width:100vw;background:#FFF;content:"";z-index:99999999999999999999999999;opacity:0.97';
var blockrCoreStyle = 'display:flex;flex-flow: column wrap;justify-content:center;align-items: center;align-content: center;width: 100%;height: 100%;font-size: 22px;';
var blockrImageStyle = 'margin-bottom: 10px;';
var blockrButtonStyle = 'font-size: 14px; margin-top: 20px;';
blockr.style.cssText += ';' + blockrStyle;
blockrCore.style.cssText += ';' + blockrCoreStyle;
blockrImage.style.cssText += ';' + blockrImageStyle;
blockrButton.style.cssText += ';' + blockrButtonStyle;
blockr.id = "FNB_blockrmodal";
blockrCore.appendChild(blockrImage);
blockrCore.appendChild(document.createTextNode(chrome.i18n.getMessage("Attent", url)));
blockrButton.appendChild(document.createTextNode("Continue anyway"));
blockrButton.id = "FNB_continueButton";
blockrCore.appendChild(blockrButton);
blockr.appendChild(blockrCore);
document.body.appendChild(blockr);
document.getElementById("FNB_continueButton").addEventListener("click", function() {
document.body.removeChild(document.getElementById("FNB_blockrmodal"));
});
slave.url = url;
slave.type = type;
slave.proof = proof;
slave.notes = notes;
slave.origin = origin;
slave.name = name;
slave.version = version;
slave.kind = kind;
provide(slave);
} else if (url.toUpperCase() == location.host.replace('www.','').toUpperCase() && types.indexOf(type) > -1) {
chrome.runtime.sendMessage({
t: "notu",
l: url
});
slave.url = url;
slave.type = type;
slave.proof = proof;
slave.notes = notes;
slave.origin = origin;
slave.name = name;
slave.version = version;
slave.kind = kind;
provide(slave);
}
for (a = 0; a < anchors.length; a++) {
if(anchors[a].href.toUpperCase() == location.host.replace('www.','').toUpperCase() && url.toUpperCase() !== location.host.replace('www.','').toUpperCase && types.indexOf(type) > -1) {
anchors[a].className += " FNB_link";
anchors[a].setAttribute('title', "Possible fake news");
}
}
}
}
})();