This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbackground.js
326 lines (197 loc) · 7.73 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function debug(param) {
if (globalDebugMode) {
console.log(param)
}
}
function checkURL(urlstr) {
return urlstr.includes("teaching.applysquare.com") && (urlstr.includes("T-Work-treviewinfo") || urlstr.includes("S-Lesson-index"))
}
function sendUpdatedSettings() {
chrome.storage.sync.get("settings", function (items) {
debug("[Info][background] Load Settings.")
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
//debug(tabs[0])
chrome.tabs.sendMessage(tabs[0].id, { cmd: 'update', info: items.settings }, function (response) {
debug("[Send][background->contentscripts] Send you something new!")
debug(response) //发送给contentscript设置
});
})
})
}
function setClickMode(tab) {
if (checkURL(tab.url)) {
debug("[Info][background] Execute Content Scripts.")
chrome.tabs.executeScript(tab.id, { file: "contentscript.js" })
} else {
commonMode(tab.id)
debug("[Info][background] Only work on specific page.")
}
}
function clickMode(id) {
/* 设置点击运行模式 */
debug("[Info][background] Change to Click Mode.")
/* 改变按钮样式 */
chrome.browserAction.enable(id)
chrome.browserAction.setTitle({ title: "当前正以点击模式运行", tabId: id }, function () { })
chrome.browserAction.setBadgeText({ text: "Click", tabId: id }, function () { })
chrome.browserAction.setBadgeBackgroundColor({ color: "#4a90e2", tabId: id }, function () { })
}
function clickModeScript() {
/* 点击模式 */
if (chrome.tabs.onUpdated.hasListener(setBgMode)) {
chrome.tabs.onUpdated.removeListener(setBgMode)
}
if (chrome.tabs.onUpdated.hasListener(setClickMode)) {
chrome.tabs.onUpdated.removeListener(setClickMode)
}
chrome.browserAction.onClicked.addListener(setClickMode)
bgMode = false
}
function setBgMode(id, change, tab) {
if (tab.active && checkURL(tab.url) && "complete" == change.status) {
chrome.storage.sync.get("settings", function (items) {
if ('{}' === JSON.stringify(items)) {
debug("[Info][background] No Settings.")
} else {
debug("[Info][background] Load Settings.")
debug("[Info][background] Execute Content Scripts.")
chrome.tabs.executeScript(tab.id, { file: "contentscript.js", runAt: "document_end" })
}
})
}else{
commonMode(id)
debug("[Info][background] Only work on specific page.")
}
}
function backgroundMode(id) {
debug("[Info][background] Change to Background Mode.")
chrome.browserAction.enable(id)
chrome.browserAction.setTitle({ title: "当前正以背景模式运行", tabId: id }, function () { })
chrome.browserAction.setBadgeText({ text: "On", tabId: id }, function () { })
chrome.browserAction.setBadgeBackgroundColor({ color: "#2d8652", tabId: id }, function () { })
}
function backgroundModeScript(id) {
/* 背景模式 */
if (chrome.tabs.onUpdated.hasListener(setBgMode)) {
chrome.tabs.onUpdated.removeListener(setBgMode)
}
if (chrome.tabs.onUpdated.hasListener(setClickMode)) {
chrome.tabs.onUpdated.removeListener(setClickMode)
}
chrome.tabs.onUpdated.addListener(setBgMode)
}
function commonMode(id) {
debug("[Info][background] Change to Common Mode.")
chrome.browserAction.setTitle({ title: "当前非可用页面", tabId: id }, function () { })
chrome.browserAction.setBadgeText({ text: "", tabId: id }, function () { })
chrome.browserAction.disable(id)
}
function loadSettings() {
chrome.storage.sync.get("settings", function (items) {
debug("[Info][background] Load Settings.")
if ('{}' === JSON.stringify(items)) {
/* 第一次运行设置 */
debug("[Pass][background] First run.")
} else {
storage = JSON.parse(items.settings)
if (!storage.enableBgMode) {
debug("[Info][background] Click Mode.")
clickModeScript()
} else {
debug("[Info][background] Background Mode.")
backgroundModeScript()
}
}
})
}
function setIcon() {
commonMode()
chrome.tabs.onActivated.addListener(function (info) {
chrome.storage.sync.get("settings", function (items) {
if ('{}' === JSON.stringify(items)) {
debug("[Info][background] No Settings.")
} else {
debug("[Info][background] Load Settings.")
storage = JSON.parse(items.settings)
chrome.tabs.get(info.tabId, function (tab) {
if (checkURL(tab.url)) {
if (!storage.enableBgMode) {
clickMode(tab.tabId)
} else {
backgroundMode(tab.tabId)
}
} else {
commonMode(tab.tabId)
debug("[Info][background] Only work on specific page.")
}
})
}
})
})
chrome.tabs.onUpdated.addListener(function (id, change, tab) {
if (tab.active && checkURL(tab.url) && "complete" == change.status) {
chrome.storage.sync.get("settings", function (items) {
if ('{}' === JSON.stringify(items)) {
debug("[Info][background] No Settings.")
} else {
debug("[Info][background] Load Settings.")
storage = JSON.parse(items.settings)
if (!storage.enableBgMode) {
debug(!storage.enableBgMode)
clickMode(tab.id)
} else {
debug(!items.enableBgMode)
backgroundMode(tab.id)
}
}
})
} else {
commonMode(tab.tabId)
debug("[Info][background] Only work on specific page.")
}
})
}
/* 监听设置改变的消息*/
chrome.storage.onChanged.addListener(function (changes, namespace) {
debug("[Info][background] Update storage.")
loadSettings()
})
/* 监听来自content的消息,立即发送更新数据 */
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if ("wakeup" == message) {
sendUpdatedSettings()
sendResponse(sender, "[Recv][background->contentscripts] I am awake.")
} else if ("done" == message) {
sendResponse(sender, "[Recv][background->contentscripts] Good job.")
}
})
debug("[Info][background] Load extension.")
/* 监听菜单设置事件 */
chrome.contextMenus.onClicked.addListener(function (callback) {
if ('openSetting' == callback.menuItemId) {
chrome.runtime.openOptionsPage()
} else if ('feedback' == callback.menuItemId) {
chrome.tabs.create({ url: "https://support.qq.com/product/142808" })
}
})
var storage
/* 第一次运行设置,当然这个基本没用 */
chrome.runtime.onInstalled.addListener(function (details) {
if ("install" == details.reason) {
debug("[Info][background] First run settings.")
chrome.runtime.openOptionsPage()
}
chrome.contextMenus.create({
title: "设置功能",
contexts: ["browser_action"],
id: 'openSetting'
}, function () { })
chrome.contextMenus.create({
title: "反馈建议",
contexts: ["browser_action"],
id: 'feedback'
}, function () { })
})
var globalDebugMode = true
loadSettings()//载入
setIcon()//设置图标规则