-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathcontent.js
324 lines (318 loc) · 12.2 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
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
// 设置iframeVisible默认值,刷新后重置storage
chrome.storage.local.set({iframeVisible: true});
function injectedScript (path) {
// 只在最顶层嵌入 https://github.com/PengChen96/ajax-tools/issues/18
if (window.self === window.top) {
const scriptNode = document.createElement('script');
scriptNode.src= chrome.runtime.getURL(path);
document.documentElement.appendChild(scriptNode);
return scriptNode;
}
}
function injectedCss(path) {
if (window.self === window.top) {
const linkElement = document.createElement('link');
linkElement.rel = 'stylesheet';
linkElement.href = chrome.runtime.getURL(path);
document.documentElement.appendChild(linkElement);
return linkElement;
}
}
function injectedStyle(styleContent) {
if (window.self === window.top) {
const styleElement = document.createElement('style');
styleElement.textContent = styleContent;
document.documentElement.appendChild(styleElement);
return styleElement;
}
}
injectedStyle(`
.ajax-interceptor-container {
display: flex;
flex-direction: column;
height: 100% !important;
width: 580px !important;
min-width: 1px !important;
position: fixed !important;
inset: 0px 0px auto auto !important;
z-index: 2147483647 !important;
transform: translateX(0px) !important;
transition: all 0.4s ease 0s !important;
box-shadow: rgba(0, 0, 0, 0.12) 0px 0px 15px 2px !important;
background: #fff;
overflow: hidden;
}
.ajax-interceptor-action-bar {
height: 40px;
min-height: 40px;
padding: 0 12px 0 8px;
display: flex;
justify-content: space-between;
align-items: center;
}
.ajax-interceptor-iframe {
border: none;
height: calc(100% - 40px);
width: 100%;
border-top: 1px solid #d1d3d8;
}
.ajax-interceptor-icon {
cursor: pointer;
position: relative;
}
.ajax-interceptor-new::after {
content: '';
width: 6px;
height: 6px;
border-radius: 50%;
background: #ff0000;
position: absolute;
right: -2px;
top: -2px;
}
.ajax-interceptor-mr-8 {
margin-right: 8px;
}
`);
injectedCss('icons/iconfont/iconfont.css');
injectedScript('html/iframePage/mock.js');
const pageScripts = injectedScript('pageScripts/index.js');
if (pageScripts) {
pageScripts.addEventListener('load', () => {
chrome.storage.local.get(['iframeVisible', 'ajaxToolsSwitchOn', 'ajaxToolsSwitchOnNot200', 'ajaxDataList', 'ajaxToolsSkin'], (result) => {
// console.log('【ajaxTools content.js】【storage】', result);
const {ajaxToolsSwitchOn = true, ajaxToolsSwitchOnNot200 = true, ajaxDataList = []} = result;
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxDataList', value: ajaxDataList});
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxToolsSwitchOn', value: ajaxToolsSwitchOn});
postMessage({type: 'ajaxTools', to: 'pageScript', key: 'ajaxToolsSwitchOnNot200', value: ajaxToolsSwitchOnNot200});
});
});
}
function closeButton (container) {
const closeIcon = document.createElement('i');
closeIcon.title = 'Close';
closeIcon.className='c-iconfont c-icon-close ajax-interceptor-icon ajax-interceptor-mr-8';
closeIcon.addEventListener('click', function () {
container.style.setProperty('transform', 'translateX(calc(100% + 20px))', 'important');
chrome.storage.local.set({iframeVisible: true});
})
return closeIcon;
}
function zoomButton (container) {
let zoomOut = true;
const zoomIcon = document.createElement('i');
zoomIcon.className='c-iconfont c-icon-reduce ajax-interceptor-icon ajax-interceptor-mr-8';
zoomIcon.addEventListener('click', function () {
if (zoomOut) { // 缩小
container.style.setProperty('height', '40px', 'important');
let timer = setTimeout(() => {
container.style.setProperty('width', '180px', 'important');
clearTimeout(timer);
}, 400);
zoomOut = false;
zoomIcon.title = 'Zoom in';
zoomIcon.className='c-iconfont c-icon-fullscreen ajax-interceptor-icon ajax-interceptor-mr-8';
} else { // 放大
container.style.setProperty('width', '580px', 'important');
let timer = setTimeout(() => {
container.style.setProperty('height', '100%', 'important');
clearTimeout(timer);
}, 400);
zoomOut = true;
zoomIcon.title = 'Zoom out';
zoomIcon.className='c-iconfont c-icon-reduce ajax-interceptor-icon ajax-interceptor-mr-8';
}
})
return zoomIcon;
}
function pipButton (container) {
const pipIcon = document.createElement('i');
pipIcon.title = 'Picture in picture';
const className ='c-iconfont c-icon-zoomout ajax-interceptor-icon';
pipIcon.className = className;
chrome.storage.local.get(['ajaxToolsPipBtnNewHideFlag'], ({ ajaxToolsPipBtnNewHideFlag }) => {
pipIcon.className = ajaxToolsPipBtnNewHideFlag ? pipIcon.className : `${pipIcon.className} ajax-interceptor-new`;
});
pipIcon.addEventListener('click', async function() {
if (!('documentPictureInPicture' in window)) {
alert('Your browser does not currently support documentPictureInPicture. You can go to chrome://flags/#document-picture-in-picture-api to enable it.\n' +
'If you have enabled documentPictureInPicture, please use the HTTPS protocol, or localhost/127.0.0.1, or open the configuration page in a new tab and use picture-in-picture there.');
return;
}
pipIcon.className = className;
chrome.storage.local.set({ ajaxToolsPipBtnNewHideFlag: true });
const iframe = document.querySelector('.ajax-interceptor-iframe');
const pipWindow = await documentPictureInPicture.requestWindow({width: 580, height: 680});
// css
const allCSS = [...document.styleSheets]
.map((styleSheet) => {
try {
return [...styleSheet.cssRules].map((r) => r.cssText).join('');
} catch (e) {
const link = document.createElement('link');
link.rel = 'stylesheet';
link.type = styleSheet.type;
link.media = styleSheet.media;
link.href = styleSheet.href;
pipWindow.document.head.appendChild(link);
}
})
.filter(Boolean)
.join('\n');
const style = document.createElement('style');
style.textContent = allCSS;
pipWindow.document.head.appendChild(style);
// js
[...document.scripts].map((v) => {
const script = document.createElement('script');
script.src = v.src;
script.type = v.type;
pipWindow.document.head.appendChild(script);
});
pipWindow.document.body.append(iframe);
// 收起侧边
container.style.setProperty('transform', 'translateX(calc(100% + 20px))', 'important');
iframe.style.setProperty('height', '100%');
pipWindow.addEventListener('pagehide', (event) => {
// 展示侧边
container.style.setProperty('transform', 'translateX(0)', 'important');
iframe.style.setProperty('height', 'calc(100% - 40px)');
container?.append(iframe);
});
});
return pipIcon;
}
function themeModeButton (container) {
let mode = 'light'; // 'light|dark'
const themeIcon = document.createElement('i');
themeIcon.addEventListener('click', function() {
if (mode === 'dark') {
mode = 'light';
themeIcon.title = 'Dark';
themeIcon.className = 'c-iconfont c-icon-heiyemoshi ajax-interceptor-icon ajax-interceptor-mr-8';
container.style.setProperty('filter', 'none');
chrome.storage.local.set({ ajaxToolsSkin: 'light' });
} else {
mode = 'dark';
themeIcon.title = 'Light';
themeIcon.className = 'c-iconfont c-icon-taiyang ajax-interceptor-icon ajax-interceptor-mr-8';
container.style.setProperty('filter', 'invert(1)');
chrome.storage.local.set({ ajaxToolsSkin: 'dark' });
}
});
// 设置初始主题
chrome.storage.local.get(['ajaxToolsSkin'], (result) => {
mode = result.ajaxToolsSkin || 'light';
if (mode === 'dark') {
themeIcon.title = 'Light';
themeIcon.className = 'c-iconfont c-icon-taiyang ajax-interceptor-icon ajax-interceptor-mr-8';
container.style.setProperty('filter', 'invert(1)');
} else {
themeIcon.title = 'Dark';
themeIcon.className = 'c-iconfont c-icon-heiyemoshi ajax-interceptor-icon ajax-interceptor-mr-8';
container.style.setProperty('filter', 'none');
}
});
return themeIcon;
}
function discussionsButton () {
const discussionsIcon = document.createElement('i');
discussionsIcon.title = 'Discussions';
discussionsIcon.className='c-iconfont c-icon-xiaoxi ajax-interceptor-icon ajax-interceptor-mr-8';
discussionsIcon.addEventListener('click', function () {
window.open('https://github.com/PengChen96/ajax-tools/discussions');
})
return discussionsIcon;
}
function codeNetButton () {
const codeNetIcon = document.createElement('i');
codeNetIcon.title = 'Open the Declarative Network Request Configuration page';
const className = 'c-iconfont c-icon-code ajax-interceptor-icon ajax-interceptor-mr-8';
codeNetIcon.className = className;
chrome.storage.local.get(['ajaxToolsCodeNetBtnNewHideFlag'], ({ ajaxToolsCodeNetBtnNewHideFlag }) => {
codeNetIcon.className = ajaxToolsCodeNetBtnNewHideFlag ? className : `${className} ajax-interceptor-new`;
});
codeNetIcon.addEventListener('click', function () {
window.open(chrome.runtime.getURL('html/iframePage/dist/declarativeNetRequest.html'));
codeNetIcon.className = className;
chrome.storage.local.set({ ajaxToolsCodeNetBtnNewHideFlag: true });
})
return codeNetIcon;
}
function newTabButton () {
const newTabIcon = document.createElement('i');
newTabIcon.title = 'Open a new tab';
newTabIcon.className='c-iconfont c-icon-codelibrary ajax-interceptor-icon';
newTabIcon.addEventListener('click', function () {
window.open(chrome.runtime.getURL('html/iframePage/dist/index.html'));
})
return newTabIcon;
}
function actionBar (container) {
const header = document.createElement('header');
header.className = 'ajax-interceptor-action-bar';
// left
const left = document.createElement('div');
const closeBtn = closeButton(container);
left.appendChild(closeBtn);
const zoomBtn = zoomButton(container);
left.appendChild(zoomBtn);
const pipBtn = pipButton(container);
left.appendChild(pipBtn);
header.appendChild(left);
// right
const right = document.createElement('div');
const themeModeBtn = themeModeButton(container);
right.appendChild(themeModeBtn);
const discussionsBtn = discussionsButton();
right.appendChild(discussionsBtn);
const codeNetBtn = codeNetButton();
right.appendChild(codeNetBtn);
const newTabBtn = newTabButton();
right.appendChild(newTabBtn);
header.appendChild(right);
return header;
}
// 只在最顶层页面嵌入iframe
if (window.self === window.top) {
document.onreadystatechange = () => {
if (document.readyState === 'complete') {
let container = document.createElement('div');
container.className = 'ajax-interceptor-container'
container.style.setProperty('transform', 'translateX(calc(100% + 20px))', 'important'); // 470px
const _actionBar = actionBar(container);
container.appendChild(_actionBar);
const iframe = document.createElement('iframe');
iframe.src = chrome.runtime.getURL("html/iframePage/dist/index.html");
iframe.className='ajax-interceptor-iframe';
container.appendChild(iframe);
if (document.body) document.body.appendChild(container);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
// console.log('【content】【ajax-tools-iframe-show】receive message', request);
const {type, iframeVisible} = request;
if (type === 'iframeToggle') {
container.style.setProperty('transform', iframeVisible ? 'translateX(0)' : 'translateX(calc(100% + 20px))', 'important');
sendResponse({nextIframeVisible: !iframeVisible}); // 返回信息到popup.js / App.jsx
}
return true;
});
}
}
}
chrome.storage.onChanged.addListener(function (changes, namespace) {
for (let [key, {oldValue, newValue}] of Object.entries(changes)) {
if (
key === 'ajaxDataList'
|| key === 'ajaxToolsSwitchOn'
|| key === 'ajaxToolsSwitchOnNot200'
) {
// 发送到pageScript/index
postMessage({
type: 'ajaxTools',
to: 'pageScript',
key,
value: newValue,
});
}
}
});