-
Notifications
You must be signed in to change notification settings - Fork 258
/
content.js
124 lines (109 loc) · 4.51 KB
/
content.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
// const elt = document.createElement("script")
// elt.innerHTML = "window.test = 1"
// document.head.appendChild(elt)
// 在页面上插入代码
// const s1 = document.createElement('script')
// s1.setAttribute('type', 'text/javascript')
// s1.setAttribute('src', chrome.runtime.getURL('pageScripts/defaultSettings.js'))
// document.documentElement.appendChild(s1)
// 在页面上插入代码
const script = document.createElement('script')
script.setAttribute('type', 'text/javascript')
script.setAttribute('src', chrome.runtime.getURL('pageScripts/main.js'))
document.documentElement.appendChild(script)
script.addEventListener('load', () => {
chrome.storage.local.get(['ajaxInterceptor_switchOn', 'ajaxInterceptor_rules'], (result) => {
if (result.hasOwnProperty('ajaxInterceptor_switchOn')) {
postMessage({type: 'ajaxInterceptor', to: 'pageScript', key: 'ajaxInterceptor_switchOn', value: result.ajaxInterceptor_switchOn})
}
if (result.ajaxInterceptor_rules) {
postMessage({type: 'ajaxInterceptor', to: 'pageScript', key: 'ajaxInterceptor_rules', value: result.ajaxInterceptor_rules})
}
})
})
let iframe
let iframeLoaded = false
let isDevtoolPosition = false
chrome.storage.local.get(['customFunction'], (result) => {
isDevtoolPosition = !!result.customFunction?.panelPosition
if (!result.customFunction?.panelPosition) {
if (['complete', 'interactive'].includes(document.readyState)) {
insertIframe()
} else {
document.onreadystatechange = () => {
if (document.readyState === 'interactive') {
insertIframe()
}
}
}
}
})
// 只在最顶层页面嵌入iframe
function insertIframe() {
if (window.self === window.top) {
iframe = document.createElement('iframe')
iframe.className = "api-interceptor"
iframe.style.setProperty('height', '100%', 'important')
iframe.style.setProperty('width', '518px', 'important')
iframe.style.setProperty('min-width', '1px', 'important')
iframe.style.setProperty('position', 'fixed', 'important')
iframe.style.setProperty('top', '0', 'important')
iframe.style.setProperty('right', '0', 'important')
iframe.style.setProperty('left', 'auto', 'important')
iframe.style.setProperty('bottom', 'auto', 'important')
iframe.style.setProperty('z-index', '9999999999999', 'important')
iframe.style.setProperty('transform', 'translateX(538px)', 'important')
iframe.style.setProperty('transition', 'all .4s', 'important')
iframe.style.setProperty('box-shadow', '0 0 15px 2px rgba(0,0,0,0.12)', 'important')
iframe.frameBorder = "none"
iframe.src = chrome.runtime.getURL("iframe/index.html")
document.body.appendChild(iframe)
let show = false
chrome.runtime.onMessage.addListener((msg, sender) => {
if (msg == 'toggle') {
show = !show
iframe.style.setProperty('transform', show ? 'translateX(0)' : 'translateX(538px)', 'important')
}
return Promise.resolve("Dummy response to keep the console quiet")
})
}
}
// 接收background.js传来的信息,转发给pageScript
chrome.runtime.onMessage.addListener(msg => {
if (msg.type === 'ajaxInterceptor' && msg.to === 'content') {
if (msg.hasOwnProperty('iframeScriptLoaded')) {
if (msg.iframeScriptLoaded) iframeLoaded = true
} else {
postMessage({...msg, to: 'pageScript'})
}
}
})
// 接收pageScript传来的信息,转发给iframe
window.addEventListener("pageScript", function(event) {
if (iframeLoaded || isDevtoolPosition) {
chrome.runtime.sendMessage({type: 'ajaxInterceptor', to: 'iframe', ...event.detail})
} else {
let count = 0
const checktLoadedInterval = setInterval(() => {
if (iframeLoaded) {
clearInterval(checktLoadedInterval)
chrome.runtime.sendMessage({type: 'ajaxInterceptor', to: 'iframe', ...event.detail})
}
if (count ++ > 500) {
clearInterval(checktLoadedInterval)
}
}, 10)
}
}, false)
// window.addEventListener("message", function(event) {
// console.log(event.data)
// }, false)
// window.parent.postMessage({ type: "CONTENT", text: "Hello from the webpage!" }, "*")
// var s = document.createElement('script')
// s.setAttribute('type', 'text/javascript')
// s.innerText = `console.log('test')`
// document.documentElement.appendChild(s)
chrome.runtime.sendMessage(chrome.runtime.id, {type: 'ajaxInterceptor', to: 'background', contentScriptLoaded: true})
if (isDevtoolPosition) {
chrome.runtime.sendMessage(chrome.runtime.id, {type: 'ajaxInterceptor', to: 'iframe', contentScriptLoaded: true})
}