-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
158 lines (145 loc) · 5.47 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
150
151
152
153
154
155
156
157
158
//console.log("backgroung running");
chrome.runtime.onMessage.addListener( function(msg, sender, sendResponse) {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Background Service Running...");
}
});
if (msg.server == "reload") {
reloadStatus(sendResponse);
} else {
//msg will be the URL to submit to the server.
var xhr = new XMLHttpRequest();
var userURI = msg.server;
var apiRequestLoc = "/api?type=new&bm=";
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("URI to send to server: "+userURI);
}
});
chrome.storage.sync.get(['server'], function(resultServer) {
xhr.open("GET", resultServer.server + apiRequestLoc + userURI, true);
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Request StatusCode is 200 OK");
}
});
var resp = JSON.parse(xhr.responseText);
//parse to see result
if (resp == "Success") {
//this is the right format for all return statements
var d = new Date();
chrome.storage.sync.set({lastStatus: "Success", lastStatusTime: d.getTime()}, function() {
});
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Should save Success response...");
}
});
chrome.browserAction.setBadgeBackgroundColor({color: "#00ba41"});
chrome.browserAction.setBadgeText({text: "OK"});
sendResponse({response: "Success"});
} else {
var d = new Date();
chrome.storage.sync.set({lastStatus: resp, lastStatusTime: d.getTime()}, function() {
});
chrome.browserAction.setBadgeBackgroundColor({color: "#ba0000"});
chrome.browserAction.setBadgeText({text: "ERR"});
sendResponse({response: resp});
}
} else {
var dd = new Date();
chrome.storage.sync.set({lastStatus: xhr.status, lastStatusTime: dd.getTime()}, function() {
});
chrome.browserAction.setBadgeBackgroundColor({color: "#ba0000"});
chrome.browserAction.setBadgeText({text: "ERR"});
sendResponse({response: xhr.status});
}
}
}
xhr.send();
});
}
return true;
});
function reloadStatus(sendResponse) {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Checking if status should be reloaded...");
}
});
chrome.storage.sync.get(['lastStatusTime'], function(statusTimeResult) {
if (statusTimeResult.lastStatusTime != null) {
var dd = new Date();
chrome.storage.sync.get(['statusTimeout'], function(statusTimeout) {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Status Timeout is: "+statusTimeout.statusTimeout);
}
});
var debugTimeoutExpiry = statusTimeResult.lastStatusTime+parseInt(statusTimeout.statusTimeout);
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Current Time: " +dd.getTime()+" || Expiry: "+debugTimeoutExpiry);
}
});
if (dd.getTime() > statusTimeResult.lastStatusTime + parseInt(statusTimeout.statusTimeout)) {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Status has expired...");
}
});
chrome.browserAction.setBadgeText({text: ""});
sendResponse({response: "Expired"});
} else {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Status has not expired...");
}
});
chrome.storage.sync.get(['lastStatus'], function(statusResult) {
if (statusResult.lastStatus == "Success") {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Status was a Success");
}
});
sendResponse({response: "Success"});
} else {
chrome.storage.sync.get(['debug'], function(debug) {
if (debug.debug) {
console.log("Status was an ERROR");
}
});
sendResponse({response: statusResult.lastStatus});
}
});
}
});
}
});
return true;
}
chrome.runtime.onInstalled.addListener(function(details) {
if (details.reason == "install") {
new Notification("Tabby Installation", {
icon: "images/tabbyIcon48.png",
body: "Make sure to save your Server URL, thanks for installing!"
});
//on first install set StatusTimeout to default value
chrome.storage.sync.set({statusTimeout: 15000});
//also set the debug to off
chrome.storage.sync.set({debug: false});
} else if (details.reason == "update") {
var thisVersion = chrome.runtime.getManifest().version;
new Notification("Tabby Updated", {
icon: "images/tabbyIcon48.png",
body: "Tabby updated from "+details.previousVersion+" to "+thisVersion+", make sure your Server URL is saved!"
});
}
//this will only fire when the extension is updated or first installed
//ignoring chrome_update or shared_module_update
});