-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
235 lines (210 loc) · 7.78 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
const babel = require("@babel/core");
const types = require("@babel/types");
const generator = require("@babel/generator");
const hookFunctionName = "astHook";
var tabList = [];
function sendMessage(message) {
window.chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
tabList.push(tabs[0].id)
window.chrome.tabs.sendMessage(tabs[0].id, message);
});
};
window.chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
var frameId;
if (sender.frameId) {
frameId = sender.frameId;
}
else if (request.uniqueId) {
frameId = request.uniqueId;
}
else {
frameId = sender.id;
}
frameId += "";
if (request.action) {
}
else if (request.present === 1) {
window.chrome.pageAction.show(sender.tab.id);
}
// In case we are enabled, change the icon to green andd enable the popup.
else if (request.present === 2) {
window.chrome.pageAction.setIcon({
tabId: sender.tab.id, path: {
"19": "./icon/19-enable.png",
"38": "./icon/38-enable.png"
}
});
window.chrome.pageAction.show(sender.tab.id);
}
// Return the frameid for reference.
sendResponse({ frameId: frameId });
});
window.chrome.pageAction.onClicked.addListener(function (tab) {
sendMessage({ action: "pageAction" });
});
// let counter = 0, now = 0;
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
let redirectUrl = details.url;
let tabId = details.tabId;
// console.log(tabId, tabList);
if (tabList.indexOf(tabId) > -1) {
// todo 过滤log&ad&error相关服务插件
// console.time("parseCode");
// console.log(redirectUrl);
// now = Date.now();
request(details.url, (data) => {
// console.timeEnd("parseCode");
// counter += Date.now() - now;
// console.log(counter);
redirectUrl = `data:text/plain;base64,${window.btoa(unescape(encodeURIComponent(parseCode(data))))}`;
}, () => {
console.log(`加载异常:${redirectUrl}`);
// console.timeEnd("parseCode");
});
}
return { redirectUrl };
},
{
// urls: ["<all_urls>"],
urls: ["http://*/*", "https://*/*"], // todo 其他协议:blob data
types: ["script"]
},
["blocking"]
);
const parseCode = (content) => {
const syntax = babel.parse(content, { "sourceType": "script" });
babel.traverse(syntax, {
// 变量声明
VariableDeclaration(path) {
const node = path.node;
if (!node.declarations?.length) {
return;
}
for (let variableDeclarator of node.declarations) {
if (!variableDeclarator.init) {
continue;
}
if (types.isFunctionExpression(variableDeclarator.init)) {
continue;
}
let varName = "";
if (types.isIdentifier(variableDeclarator.id) || types.isMemberExpression(variableDeclarator.id)) {
varName = generator.default(variableDeclarator.id).code;
}
try {
const hookFunctionArguments = [
types.stringLiteral(varName),
variableDeclarator.init,
types.stringLiteral("var-init")
];
variableDeclarator.init = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
} catch (e) {
console.error(e);
}
}
},
AssignmentExpression(path) {
const node = path.node;
if (types.isFunctionExpression(node)) {
return;
}
let varName = "";
if (types.isIdentifier(node.left) || types.isMemberExpression(node.left)) {
varName = generator.default(node.left).code;
}
try {
const hookFunctionArguments = [
types.stringLiteral(varName),
node.right,
types.stringLiteral("assign")
];
node.right = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments)
} catch (e) {
console.error(e);
}
},
// 对象表达式
ObjectExpression(path) {
const node = path.node;
if (!node.properties?.length) {
return;
}
for (let objectProperty of node.properties) {
const propertyValue = objectProperty.value;
if (types.isFunctionExpression(propertyValue)) {
continue;
}
if (types.isObjectExpression(propertyValue)) {
continue;
}
if (!propertyValue) {
return;
}
let objectKey = objectProperty.key;
if (types.isIdentifier(objectKey)) {
objectKey = types.stringLiteral(objectKey.name);
}
try {
const hookFunctionArguments = [
objectKey,
propertyValue,
types.stringLiteral("object-key-init")
];
objectProperty.value = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
} catch (e) {
console.error(e);
}
}
},
// 函数的形参
FunctionDeclaration(path) {
const node = path.node;
if (!node.params?.length) {
return;
}
const params = node.params;
if (types.isBlockStatement(node.body)) {
// 函数体是个代码块的,则在代码块最前面插入Hook,检查参数的值
for (let i = params.length - 1; i >= 0; i--) {
try {
const paramName = params[i];
const hookFunctionArguments = [
types.stringLiteral(generator.default(paramName).code),
paramName,
types.stringLiteral("function-parameter")
];
const hookFunction = types.callExpression(types.identifier(hookFunctionName), hookFunctionArguments);
node.body.body.unshift(types.expressionStatement(hookFunction));
} catch (e) {
console.error(e);
}
}
}
}
});
let val = generator.default(syntax).code;
return val;
}
function request(url, success, failure) {
var xhr = new XMLHttpRequest();
xhr.open("GET", url, false);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
success && success(xhr.responseText);
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
failure && failure();
console.error(xhr.statusText);
};
xhr.ontimeout = function (e) {
failure && failure();
console.error(xhr.statusText);
};
xhr.send(null);
}