-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.js
More file actions
73 lines (71 loc) · 2.43 KB
/
Copy pathinsert.js
File metadata and controls
73 lines (71 loc) · 2.43 KB
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
setTimeout(() => {
const script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', chrome.extension.getURL('ajax-hooks.js'));
document.head.appendChild(script);
const isActive = localStorage.getItem('__ajaxProxyActive__');
function autoRun(isActive) {
if (isActive) {
const code = localStorage.getItem('__ajaxProxyCode__');
const autoRun = document.createElement('script');
autoRun.setAttribute('type', 'text/javascript');
autoRun.innerHTML = `
try {
ajaxProxyHook(${ code });
} catch (e) {
alert('ajax proxy执行出错,请检查。错误原因:' + e);
}
`;
document.head.appendChild(autoRun);
setTimeout(() => {
document.head.removeChild(autoRun);
});
}
}
script.onload = function() {
document.head.removeChild(script);
autoRun(isActive === '1');
chrome.runtime.onMessage.addListener(
function(request) {
if (request.messageType === 'close') {
const unHooks = document.createElement('script');
unHooks.setAttribute('type', 'text/javascript');
unHooks.innerHTML = `
ajaxProxyUnHook();
localStorage.setItem('__ajaxProxyActive__', '0');
`;
document.body.appendChild(unHooks);
setTimeout(() => {
document.body.removeChild(unHooks);
});
chrome.storage.local.set({
'__ajaxProxyActive__': false,
});
} else if (request.messageType === 'open') {
const hooks = document.createElement('script');
hooks.setAttribute('type', 'text/javascript');
hooks.innerHTML = `
ajaxProxyUnHook();
localStorage.setItem('__ajaxProxyCode__', ${ JSON.stringify(request.code) });
localStorage.setItem('__ajaxProxyActive__', '1');
try {
ajaxProxyHook(${ request.code });
} catch(e) {
alert('ajax proxy执行出错,请检查。错误原因:' + e);
}
`;
chrome.storage.local.set({
'__ajaxProxyActive__': true,
'__ajaxProxyInfo__': {
code: request.code,
origin: location.origin,
},
});
document.body.appendChild(hooks);
setTimeout(() => {
document.body.removeChild(hooks);
});
}
});
};
});