-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
149 lines (137 loc) · 5.14 KB
/
background.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
'use strict';
// Think of this backgroud script as running at all times out of view when the
// extension is enabled. It is the controller that allows us to interact with
// other content scripts that we throw into the DOM of specific pages.
async function postData(url = '', data = {}) {
const response = await fetch(url, {
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
});
return response.json();
}
function incrementProgress(url) {
// UPDATE THE STUDY BACKEND HERE... The server must include logic to ensure that repeated increments are not made for the same trigger
// Send a post request to the Server to increment Progress.
var siteURL = url;
var postURL = ""
if(siteURL == "https://www.huffpost.com/entry/georgia-election-trump-voter-fraud-claims_n_5ff36b73c5b61817a538bd20" || siteURL == "https://www.breitbart.com/politics/2020/12/17/fraud-happened-sen-rand-paul-says-the-election-in-many-ways-was-stolen/") {
postURL = "https://www.annotatednews.com/readArticle";
chrome.storage.local.get('stats', function(result){
console.log(result.stats);
var newStatsObj = {};
if(typeof(result.stats) !== "undefined") {
newStatsObj = result.stats;
}
if(!("article" in newStatsObj)) {
newStatsObj["article"] = Math.round(Date.now() / 1000);
}
chrome.storage.local.set({"stats": newStatsObj}, () => {});
});
} else if (siteURL == "https://twitter.com/jaketapper/status/1400532544555307009" || siteURL == "https://twitter.com/HawleyMO/status/1344307458085412867") {
postURL = "https://www.annotatednews.com/readSocial";
chrome.storage.local.get('stats', function(result){
console.log(result.stats);
var newStatsObj = {};
if(typeof(result.stats) !== "undefined") {
newStatsObj = result.stats;
}
if(!("social" in newStatsObj)) {
newStatsObj["social"] = Math.round(Date.now() / 1000);
}
chrome.storage.local.set({"stats": newStatsObj}, () => {});
});
} else {
// do nothing
postURL = "nothing";
}
if(postURL != "nothing") {
chrome.storage.local.get('cNumber', function(result){
var data = {
'c_number': result.cNumber
}
postData(postURL, data)
.then(response => console.log(response));
});
}
}
function loadExtension(currentURL, studyPin, currentTAB) {
let url = "https://www.annotatednews.com/loadExtension";
var data = {
"user_current_url": currentURL,
"study_pin": studyPin
};
postData(url, data)
.then(serverResponse => {
chrome.tabs.sendMessage(currentTAB, {
"type": "server_output",
"command": "incoming_data",
"payload": JSON.stringify(serverResponse)
}, function(response) {
//After all the elements have been modified and added to the page... load Active.JS.
if (response.responseCode == "success") {
// adds active.js to the context in its own "isolated world"
chrome.scripting.executeScript({
target: { tabId: currentTAB },
files: ['content/active.js']
});
// once we successfully load the extension, increment study progress through another .fetch()
incrementProgress(response.url);
}
return true;
});
});
}
function updateAnnotationData(annotId, studyPin, opened, helpful) {
let url = "https://www.annotatednews.com/updateAnnotationStats";
var data = {
"annot_id": annotId,
"study_pin": studyPin,
"opened": opened,
"helpful": helpful,
};
postData(url, data)
.then(serverResponse => console.log(serverResponse));
}
function updateUserInteractionsTable(studyPin, sourceType, loadTime, endTime){
let url = "https://www.annotatednews.com/updateExtensionStats";
var data = {
"study_pin": studyPin,
"source_type": sourceType,
"load_time": loadTime,
"end_time": endTime
};
postData(url, data)
.then(serverResponse => console.log(serverResponse));
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
switch (request.type) {
case "server_request":
switch (request.command) {
case "turn_on":
let thisURL = request.url;
let thisTAB = sender.tab.id;
chrome.storage.local.get('studyPin', function(result){
let thisUSER = result.studyPin;
console.log(thisURL);
console.log(thisUSER);
loadExtension(thisURL, thisUSER, thisTAB);
});
break;
case "update_annotation_database":
var annotId = request.annotId;
var opened = request.newOpen; // 1 if this is a new open, 0 if no event...
// sum all entries on same annotId in order to get the total number of opened and helpful/unhelpful.
// if this is just an "open" event, helpful will be 0 and there is no change to the total
// helpful value.
var helpful = request.helpful;
chrome.storage.local.get('studyPin', function(result){
updateAnnotationData(annotId, result.studyPin, opened, helpful);
});
}
}
return true;
});