-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
61 lines (51 loc) · 1.71 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
var tosdrServices = "http://tos-dr.info/index/services.json";
var tosdrServiceBase = "http://tos-dr.info/services/";
var currentService;
var services;
var servicesDetail = new Array();
function updateServiceIndex() {
var now = new Date().getTime();
if(localStorage.lastUpdate == undefined || localStorage.lastUpdate - now > 5*60*60*1000) {
$.getJSON(tosdrServices, function(data) {
//console.log(data);
localStorage.services = JSON.stringify(data);
services = data;
// localStorage.lastUpdate = now;
});
} else {
services = JSON.parse(localStorage.services);
}
}
function getService(serviceName, callback) {
if(localStorage.lastUpdate == undefined)
updateServiceIndex();
if(services[serviceName] != undefined) {
// Service exists...
$.getJSON(tosdrServiceBase+serviceName+".json", function(data) {
currentService = serviceName;
servicesDetail[serviceName] = data;
if(data.tosdr.rated != false)
chrome.browserAction.setBadgeText({"text": data.tosdr.rated});
else
chrome.browserAction.setBadgeText({"text": "Unrated"});
if(data.tosdr.rated == false || data.tosdr.rated > 'C')
chrome.browserAction.setIcon({path:"red.png"});
else
chrome.browserAction.setIcon({path:"green.png"});
// localStorage.lastUpdate = now;
});
}
}
function getCurrentServiceData() {
return servicesDetail[currentService];
}
function getNewInfo(t, info) {
chrome.tabs.get(t, function(tab) {
var domParts = tab.url.replace('http://','').replace('https://','').split(/[/?#]/)[0].split(".");
var service = domParts[domParts.length - 2];
getService(service);
});
}
chrome.tabs.onSelectionChanged.addListener(getNewInfo);
chrome.tabs.onUpdated.addListener(getNewInfo);
updateServiceIndex();