-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbg.js
executable file
·71 lines (61 loc) · 1.85 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
67
68
69
70
71
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' && !!changeInfo.url) {
var urlPattern = new RegExp("(" + _watchUrls.join('|') + ")");
if (urlPattern.test(tab.url)) {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ['webapi.js', 'ghremark.js']
});
}
}
}
this.start = function () {
if (_started == true) return;
chrome.tabs.onUpdated.addListener(_insertFunc);
chrome.storage.local.set({ recordStatus: "on" });
_started = true;
};
this.stop = function () {
if (_started == false) return;
chrome.tabs.onUpdated.removeListener(_insertFunc);
chrome.storage.local.set({ 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':
// chrome.storage.local.get(message.key).then((result) => {
// console.log("Value is set to " + result[message.key]);
// sendResponse(result[message.key]);
// });
// break;
// case 'setLocalStorage':
// chrome.storage.local.set({ [message.key] : message.value });
// break;
}
});
//恢复上次的状态
chrome.storage.local.get("recordStatus").then((result) => {
if (result["recordStatus"] == "on") {
gr.start();
chrome.action.setBadgeBackgroundColor({ color: '#FF0000' });
chrome.action.setBadgeText({ text: 'on' });
}
});