-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
134 lines (111 loc) · 3.94 KB
/
popup.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
// Testing event handlers
// document.getElementById('real').addEventListener('click', showReal);
// document.getElementById('fake').addEventListener('click', showFake);
// document.getElementById('error').addEventListener('click', showError);
var apiURL = "http://www.fakenewsai.com/detect"
chrome.storage.sync.get('userid', function(items) {
var userid = items.userid;
if (userid) {
useToken(userid);
} else {
userid = getRandomToken();
chrome.storage.sync.set({userid: userid}, function() {
useToken(userid);
});
}
});
// use token, load URL and then make request
function useToken(id) {
chrome.tabs.query({active:true,currentWindow:true},function(tabArray){
url = tabArray[0].url;
pageLocation = getLocation(url);
if (!pageLocation.protocol.startsWith("http")){
showError();
return;
}
shortenedURL = pageLocation.protocol + "//" + pageLocation.hostname;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4){
if (this.status == 200) {
data = JSON.parse(this.responseText);
if (data.error){
showError("Oops! There was an error while we were checking the site.");
} else {
if (data.fake){
showFake();
} else {
showReal();
}
}
} else {
showError("Oops! There was a connection error while we were checking the site.");
}
}
};
xmlhttp.open("GET", apiURL + "?url=" + encodeURIComponent(shortenedURL), true);
xmlhttp.send();
});
}
// display code
var analyzed = "We analyzed this website to see if it was similar to known fake news sites using a machine learning model. The same technology is used to power other artificial intelligence applications, like Siri and self-driving cars!";
function showReal(){
var body = document.body;
var displayDiv = document.getElementById("displayText");
var spinner = document.getElementById("spinner");
var header = document.getElementById("headerText");
spinner.style.display = "none";
header.innerText = "Real!";
displayDiv.innerHTML = "<span>This site is probably not a fake news site.</span><br /><br />";
displayDiv.innerHTML += analyzed;
displayDiv.className += " fadeIn";
header.className += " fadeIn";
body.style.backgroundColor = "#009900";
body.style.color = "white";
};
function showFake(){
var body = document.body;
var displayDiv = document.getElementById("displayText");
var spinner = document.getElementById("spinner");
var header = document.getElementById("headerText");
spinner.style.display = "none";
header.innerText = "Fake!";
displayDiv.innerHTML = "<span>This site is probably not a reliable news source.</span><br /><br />";
displayDiv.innerHTML += analyzed;
displayDiv.className += " fadeIn";
header.className += " fadeIn";
body.style.backgroundColor = "#c00000";
body.style.color = "white";
};
function showError(errorMessage){
var body = document.body;
var displayDiv = document.getElementById("displayText");
var spinner = document.getElementById("spinner");
var header = document.getElementById("headerText");
spinner.style.display = "none";
header.innerText = "Error!";
if (!errorMessage){
displayDiv.innerHTML = "<span>This site does not appear to be a valid news site.</span>";
} else {
displayDiv.innerHTML = errorMessage;
}
displayDiv.className += " fadeIn";
header.className += " fadeIn";
}
// util code
function getRandomToken() {
// E.g. 8 * 32 = 256 bits token
var randomPool = new Uint8Array(32);
crypto.getRandomValues(randomPool);
var hex = '';
for (var i = 0; i < randomPool.length; ++i) {
hex += randomPool[i].toString(16);
}
return hex;
}
// generate location object from link
var getLocation = function(href) {
var l = document.createElement("a");
l.href = href;
return l;
};