-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg.js
executable file
·110 lines (94 loc) · 3.69 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
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
var CouponRecorder = function(initParams){
var _isRecord = false,
_watchUrls = ['smzdm.com'],
_logUrl = 'http://promotion.devgo.top/coupon_recorder/v1/view_log/log',
_version = "0.1";
Object.defineProperties(this, {
'isRecord': { set: function(value){ _isRecord = value; _valueUpdateTime = Date.now();}, get : function(){ return _isRecord; } }
});
var _insertFunc = function (tabId,changeInfo,tab){
if(changeInfo.status == 'loading'){//刷新一次标签页,onUpdated会被触发两次,第一次changeInfo.status是loading,第二次是changeInfo.status是complete
var urlPattern = new RegExp("("+_watchUrls.join('|')+")");
if(urlPattern.test(tab.url)){
//让用户界面执行代码。
//chrome.tabs.executeScript(tabId,{code : "alert('看看这是那个页面弹出的!');"});
//让用户界面执行一个文件的JS。
//chrome.tabs.executeScript(integer tabId, object details, function callback)
//可惜的是file只支持单个js文件,如果我们有多个js文件,会带来不便,难不成要合并成一个js?答案是:使用callback参数,在第一个js执行完成,回调时,注入下一个js文件。
chrome.tabs.executeScript(tabId,{file : "bower_components/jquery/dist/jquery.js"},function(){
chrome.tabs.executeScript(tabId,{file : "smzdm.js"});
});
}
}
}
this.start = function(){
if(_isRecord == true) return;//防止重复启动
//注册事件的响应函数
chrome.tabs.onUpdated.addListener(_insertFunc);
localStorage.recordStatus = "on";
_isRecord = true;
};
this.stop = function(){
if(_isRecord == false) return;
chrome.tabs.onUpdated.removeListener(_insertFunc);
localStorage.recordStatus = "off";
_isRecord = false;
};
var _httpGet = function(url, callback){
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if ( xhr.readyState === xhr.DONE ) {
if ( xhr.status === 200 || xhr.status === 0 ) {
if ( xhr.responseText ) {
callback( xhr.responseText );
} else {
console.warn( "[" + url + "] seems to be unreachable or file there is empty" );
}
} else {
console.error( "Couldn't load [" + url + "] [" + xhr.status + "]" );
}
}
};
xhr.open( "GET", url, true );
xhr.send( null );
}
this.log = function(params, callback){
var url = _logUrl+"?user="+params.userId+"&article_id="+params.itemId+"&action="+params.action+"&site="+params.site;
_httpGet(url, function(data){
callback(JSON.parse(data));
});
};
}
//init
var cr = new CouponRecorder();
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse){
switch(message.method){
case 'start':
cr.start();
sendResponse({status: cr.isRecord});
break;
case 'stop':
cr.stop();
sendResponse({status: cr.isRecord});
break;
case 'log':
cr.log(message.params, function(result){
console.log(result);
sendResponse({"status": (result=="success")});
})
return true;//chrome.runtime.onMessage.addListener:This function becomes invalid when the event listener returns, unless you return true from the event listener to indicate you wish to send a response asynchronously (this will keep the message channel open to the other end until sendResponse is called).
//So just add "return true;" to indicate that you'll call the response function asynchronously.
case 'getLocalStorage':
sendResponse(localStorage.getItem(message.key));
break;
case 'setLocalStorage':
localStorage.setItem(message.key, message.value);
break;
}
});
//恢复上次的状态
if(localStorage.recordStatus == "on"){
cr.start();
chrome.browserAction.setBadgeBackgroundColor({color: '#FF0000'});
chrome.browserAction.setBadgeText({text: 'on'});
}