-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
178 lines (153 loc) · 3.97 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
var MESSAGE_REGISTRY = {};
var GLOBAL_DATA = {};
var __downloadConcerns = {};
var __contentCommand;
class TabManager {
constructor() {
this.forks = {}; // {url: parentTabId}
}
register(url, parentTabId) {
this.fork[url] = parentTabId;
}
get(url) {
return this.fork[url];
}
remove(url) {
let parentId = this.fork[url];
delete this.fork[url];
return parentId;
}
}
var TAB_CREATE_REGISTRY = new TabManager();
// ----------- commands from content pages --------------
var GLOABAL_COMMANDS = {
'storeData': function(data){
if( data && data.key ) {
GLOBAL_DATA[data.key] = data.val;
}
},
'retrieveData': function(key){
let data = key && GLOBAL_DATA[key];
return data;
},
'removeData': function(key) {
let data = key && GLOBAL_DATA[key];
delete GLOBAL_DATA[key];
return data;
},
'tabSet': function(data, sender){
if( data && data.key ) {
let key = '' + data.key + '__<<' + sender.tab.id;
GLOBAL_DATA[key] = data.val;
}
},
'tabGet': function(localKey, sender){
let key = '' + localKey + '__<<' + sender.tab.id;
let data = GLOBAL_DATA[key];
return data;
},
'tabRemove': function(localKey, sender) {
let key = '' + localKey + '__<<' + sender.tab.id;
let data = GLOBAL_DATA[key];
delete GLOBAL_DATA[key];
return data;
},
'setExtentionCommand': function(cmd){
__contentCommand = cmd;
},
'setBadge': function(data, sender){
Extension.setBadgeText(data.text, false);
},
'downloadFile': function(data, sender){
chrome.downloads.download({
url: data.url,
filename: data.filename
},
function(downloadId){
data.tabId = sender.tab && sender.tab.id;
Extension.watchDownload(downloadId, data);
}
);
},
// Create a new tab
// data.url
// data.active
// data.selected
'createTab': function(data, sender) {
let attachedData = data.data;
chrome.tabs.create({
url: data.url,
active: (data.active===false ? false : true),
selected: (data.selected===false ? false : true)
},
function(tab) {
let key = 'parentTab_' + tab.id;
let val = attachedData;
attachedData.parentTabId = sender.tab.id;
GLOBAL_DATA[key] = attachedData;
}
);
},
// send a message to specified tab
'sendTabMessage': function(data, sender) {
data = data || {};
let message = data.message;
let tabId = data.tabId;
data = data.data;
if( tabId && message ) {
let tabData = {};
tabData.tabMessageData = data;
tabData.fromTabId = sender.tab.id;
Extension.sendMessageToTab(tabId, message, tabData, function(rsp){
return rsp
});
}
},
'getCurrentTabId': function(data, sender) {
return sender.tab.id;
}
};
for( let key in GLOABAL_COMMANDS ) {
Extension.onMessage(key, GLOABAL_COMMANDS[key]);
}
//--------------------- Extension listeners -----------------------------------
// message listener
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse){
let rsp = undefined;
if( request && request.noryal_message) {
let message = request.noryal_message;
let data = request.noryal_data;
var callback = MESSAGE_REGISTRY[message];
rsp = ScCallback(callback, this||{}, data, sender);
}
rsp = rsp || {};
sendResponse(rsp);
}
);
chrome.browserAction.onClicked.addListener(function(tab) {
Extension.runCommand();
});
// monitoring download complete / failed event
// download.onChanged event tells the real filename downloaded
chrome.downloads.onChanged.addListener(function(delta){
if( delta ) {
let key = '' + delta.id;
let concern = __downloadConcerns[key];
delete __downloadConcerns[key];
if( concern ) {
if( delta.filename )
concern.localfile = delta.filename.current;
let state = delta.state && delta.state.current;
if( state == 'interrupted' || state == 'complete') {
let successMapping = {
'interrupted': false,
'complete': true
}
concern.success = successMapping[state];
Extension.sendMessageToTab(concern.tabId, 'onFileDownload', concern);
}
}
}
});
//-----------------------------------------------------