-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
60 lines (55 loc) · 1.95 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
var pollInterval = 1000 * 60 * 3; // 3 minutes, in milliseconds
function updateBadge() {
var terms = localStorage['terms'];
if (terms !== undefined && terms != null) {
var myterms = JSON.parse(terms);
myterms.__proto__ = Terms.prototype;
//var thisvar = myterms.asArray().length;
var count = 0;
myterms.asArray().forEach(function(myterm) {
myterm.__proto__ = Term.prototype;
if (!myterm.visited) {
count++;
}
});
//alert('total '+myterms.asArray().length+' non-visited '+count);
chrome.browserAction.setBadgeText({text:count+''}); //+'' to convert integer to text
chrome.browserAction.setBadgeBackgroundColor({color: "#0000FF"});
}
}
function startRequest() {
updateBadge();
window.setTimeout(startRequest, pollInterval);
}
this.startRequest();
chrome.runtime.onMessage.addListener(function(msg, sender) {
/* First, validate the message's structure */
if (msg.from && (msg.from === "content")
&& msg.subject && (msg.subject = "showPageAction")) {
if (sender != undefined && sender.tab != undefined && sender.tab.id != undefined) {
/* Enable the page-action for the requesting tab */
chrome.pageAction.show(sender.tab.id);
}
}
});
chrome.runtime.onMessage.addListener(function(msg, sender) {
/* First, validate the message's structure */
if (msg.from && (msg.from === "popup")
&& msg.subject && (msg.subject === "updateContent")) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
chrome.runtime.sendMessage(
{from: "background", subject: "updateContent", data: msg.data}
);
}
);
//alert('have updates from popup');
updateBadge();
} else if (msg.from && (msg.from === "contents")
&& msg.subject && (msg.subject === "visitedLink")) {
//alert('have visited links from the content page');
updateBadge();
}
});