-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
141 lines (122 loc) · 3.6 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
var endPointHashtag = 'http://clawenv.m3e3mwc9r8.us-west-2.elasticbeanstalk.com/flaskhw/hashtag';
var endPointSentiment = 'http://clawenv.m3e3mwc9r8.us-west-2.elasticbeanstalk.com/flaskhw/visualize';
var endPointMalicious = 'http://clawenv.m3e3mwc9r8.us-west-2.elasticbeanstalk.com/flaskhw/checkFake';
var endPointReport = 'http://clawenv.m3e3mwc9r8.us-west-2.elasticbeanstalk.com/flaskhw/updateWeights';
var userHandle = '';
var lastEvent = -1000;
var lastEventHandled = 0;
var tweet = '';
var sched = false;
var port1;
chrome.webNavigation.onHistoryStateUpdated.addListener(function(details){
//if navigated to a page that does not have the injection, peform the injection
console.log('State pushed to '+details.url);
chrome.tabs.executeScript(null,{file:"jquery.js"});
chrome.tabs.executeScript(null,{file:"inject.js"});
//else do nothing, as injected components are preserved in app state history
});
chrome.runtime.onConnect.addListener(function(port) {
//receive timestamp of last keyup event from content
port1 = port;
if(port.name == "my-channel"){
port.onMessage.addListener(function(msg) {
console.log('Received new keyup from content');
lastEvent = msg.lastEvent;
tweet = msg.tweet;
if(!sched){
setTimeout(keyfunction,1000);
sched = true;
sendToContentScripts({type:"user-typing"});
}
});
}
});
function keyfunction() {
var currTime = + new Date();
console.log(currTime,lastEvent);
if(currTime-lastEvent>=1000)
{
console.log('Calling API');
callAPI();
lastEventHandled = lastEvent;
}
if (lastEventHandled!=lastEvent) {
setTimeout(keyfunction,1000);
}
else
sched = false;
}
var callAPI = function(msg){
//POST req to get hastags
$.ajax({
type: "POST",
data: "query="+tweet,
url: endPointHashtag,
success: function(data){
// do something with data
data.type = 'hashtag-result';
console.log(data);
//port.postMessage({data1: data});
sendToContentScripts(data);
}
});
//POST req to get past sentiment
$.ajax({
type: "POST",
data: "query="+tweet,
url: endPointSentiment,
success: function(data){
var resp = {type:'sentiment-result',charts:data};
console.log(resp);
//port.postMessage({data1: data});
sendToContentScripts(resp);
}
});
}
var sendToContentScripts = function (data){
chrome.tabs.getSelected(null, function (tab){
//console.log('BG sending to iframe', tab.id);
chrome.tabs.sendMessage(tab.id, data);
});
};
//listen from content scripts
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if(request.type=='get-malicious') //from inject.js
{
console.log('Checking malicious tweets');
//make API call to get IDs of malicious tweets
$.ajax({
type: "POST",
data: 'query='+request.ids,
url: endPointMalicious,
success: function(data){
var resp = {type:'malicious-ids-result', ids:data};
console.log(resp);
sendToContentScripts(resp);
}
});
}
else if(request.type == 'report-tweet')
{
console.log('Reporting tweet '+request.tweetID + ' by @'+userHandle);
//send user handle and ID of reported tweet to backend
$.ajax({
type: "POST",
data: JSON.stringify({'user_id':userHandle, 'tweet_id':request.tweetID}),
contentType: "application/json",
dataType : 'json',
url: endPointReport,
success: function(data){
console.log('Successfully reported tweet');
}
});
}
else if(request.type == 'user-handle-msg')
{
//receive user handle from content.js on page load
//one time operation
console.log('Hi @'+request.handle);
userHandle = request.handle;
}
});