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 pathcontentscript.js
97 lines (61 loc) · 2.07 KB
/
contentscript.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
function debug(param) {
if (globalDebugMode) {
console.log(param)
}
}
function hasContentscripts() {
var extension = chrome.runtime.id
var script = document.querySelectorAll("script[src*='" + extension + "']")
//debug(script)
return script.length > 0
}
// function rmContentscripts() {
// var extension = chrome.runtime.id
// var script = document.querySelector("script[src*='" + extension + "']")
// var pdata = document.querySelector("p#opt_data")
// script.remove()
// pdata.remove()
// }
function injectScripts() {
var extensionOrigin = 'chrome-extension://' + chrome.runtime.id
if (!location.ancestorOrigins.contains(extensionOrigin)) {
ctrlElement = document.createElement('p')
ctrlElement.id = 'opt_data'
ctrlElement.textContent = settings
ctrlElement.style.display = "none"
document.getElementsByTagName('body')[0].appendChild(ctrlElement)
var s = document.createElement('script')
s.src = chrome.runtime.getURL('catchHomework.js')
document.querySelector('div#content_right').appendChild(s)
chrome.runtime.sendMessage("done", function (response) {
debug("[Send][contentscripts->background] Ready for work.")
debug(response)
})
}
}
function updateContentVar(message, sender, sendResponse) {
if ('update' == message.cmd) {
settings = message.info
sendResponse(sender, "[Recv][contentscripts->background] I'm updated.")
injectScripts()
chrome.runtime.onMessage.removeListener(updateContentVar)
//debug("[Info] Remove listener for updating.")
}
}
function reqBgUpdate(params) {
chrome.runtime.sendMessage("wakeup", function (response) {
debug("[Send][contentscript->background] Wake up!")
//debug(response)
})
}
var settings
var globalDebugMode = true
if (hasContentscripts()) {
debug("[Info] Already run, please refresh.")
} else {
if (!chrome.runtime.onMessage.hasListener(updateContentVar)) {
chrome.runtime.onMessage.addListener(updateContentVar)
//debug("[Info] Set listener for updating.")
}
reqBgUpdate()
}