forked from dpy1123/GithubRemark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg.js
executable file
·66 lines (55 loc) · 1.59 KB
/
bg.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
var GithubRemark = function(initParams){
var _started = false,
_watchUrls = ['github.com'];
Object.defineProperties(this, {
'isStart': { set: function(value){ _started = value; _valueUpdateTime = Date.now();}, get : function(){ return _started; } }
});
var _insertFunc = function (tabId,changeInfo,tab){
if(changeInfo.status == 'loading'){
var urlPattern = new RegExp("("+_watchUrls.join('|')+")");
if(urlPattern.test(tab.url)){
chrome.tabs.executeScript(tabId,{file : "webapi.js"},function(){
chrome.tabs.executeScript(tabId,{file : "ghremark.js"});
});
}
}
}
this.start = function(){
if(_started == true) return;
chrome.tabs.onUpdated.addListener(_insertFunc);
localStorage.recordStatus = "on";
_started = true;
};
this.stop = function(){
if(_started == false) return;
chrome.tabs.onUpdated.removeListener(_insertFunc);
localStorage.recordStatus = "off";
_started = false;
};
}
//init
var gr = new GithubRemark();
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
switch(message.method){
case 'start':
gr.start();
sendResponse({status: gr.isStart});
break;
case 'stop':
gr.stop();
sendResponse({status: gr.isStart});
break;
case 'getLocalStorage':
sendResponse(localStorage.getItem(message.key));
break;
case 'setLocalStorage':
localStorage.setItem(message.key, message.value);
break;
}
});
//恢复上次的状态
if(localStorage.recordStatus == "on"){
gr.start();
chrome.browserAction.setBadgeBackgroundColor({color: '#FF0000'});
chrome.browserAction.setBadgeText({text: 'on'});
}