-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
78 lines (72 loc) · 2.72 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
//content.js
$('*:not(:has(*:not(:empty)))').each(async function(index) {
//Builds and changes the HTML for every leaf of the DOM (excluding empty nodes)
htmlText = await buildHTML(splitText($(this).text().toString()));
$(this).html(htmlText);
});
function splitText(t) {
//Splits the text into sentences.
return t.split(/[.!?:]/g);
}
async function buildHTML(textArray) {
//Builds the new HTML with CSS, etc. based on the misinformation type from the server
if(!textArray) {
return "";
}
var c;
for(c = 0; c < textArray.length; c++) {
var txt = new String(textArray[c]).replace(/[^\w\s]/g,"").replace(/\s+/g," ");
if(!(txt === "")) {
var URL = 'https://politifactmisinformation.herokuapp.com/query?s=' + encodeURIComponent(txt);
var misinformationType = await ajaxCall(URL);
if(misinformationType && misinformationType["items"][0]) {
var urlString = misinformationType["items"][0].url;
switch(misinformationType["items"][0].type) {
case 0:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#FF0000\">"+ textArray[c] + "</a>";
break;
case 1:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#FF5454\">"+ textArray[c] + "</a>";
break;
case 2:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#F07627\">"+ textArray[c] + "</a>";
break;
case 3:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#E7CD59\">"+ textArray[c] + "</a>";
break;
case 4:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#C4FF31\">"+ textArray[c] + "</a>";
break;
case 5:
textArray[c] = "<a href=\"" + urlString + "\" style=\"color:#00FF00\">"+ textArray[c] + "</a>";
break;
}
}
}
}
return textArray.join("");
}
async function ajaxCall(URL) {//Sends the request to the server
//The use of await and async everywhere is inspired from this article: https://petetasker.com/using-async-await-jquerys-ajax
let result;
try {
result = await $.ajax(URL,{type:'GET'});
return result;
} catch (error) {
console.error(error);
}
}
//These are used to handle the server's ajax calls for sites like Twitter.
//The idea is from this medium article: https://medium.com/@aidobreen/fixing-twitter-with-a-chrome-extension-1f53320f5a01
function modify() {
$('*').bind('DOMSubtreeModified.event1',DOMModificationHandler);
}
function DOMModificationHandler(){
$(this).unbind('DOMSubtreeModified.event1');
setTimeout(function(){
modify();
$('*').bind('DOMSubtreeModified.event1',
DOMModificationHandler);
},10);
}
$('*').bind('DOMSubtreeModified.event1',DOMModificationHandler);