forked from larack8/wxappUnpacker
-
Notifications
You must be signed in to change notification settings - Fork 5
/
wuWxss.js
370 lines (344 loc) · 15.1 KB
/
wuWxss.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
const wu = require("./wuLib.js");
const path = require("path");
const fs = require("fs");
const {VM} = require('vm2');
const cssbeautify = require('cssbeautify');
const csstree = require('css-tree');
const cheerio = require('cheerio');
function chomp_balanced(input_str, scan_start, open_char, close_char) {
quote_chars = ['\"', '\''];
let now = scan_start - 1;
let depth = 0;
let in_quote = false;
let start = -1;
let end = -1;
let end_index = input_str.length - 1;
let now_quote_char = undefined;
let c = undefined;
let last_char = undefined;
let escape_char = '\\';
while (true) {
now += 1;
if (now > end_index) {
break
}
last_char = c;
c = input_str[now];
if (quote_chars.indexOf(c) > -1 && c !== open_char) {
if (in_quote && c === now_quote_char && last_char !== escape_char) {
in_quote = false;
now_quote_char = undefined
} else {
if (!in_quote) {
now_quote_char = c;
}
in_quote = true;
}
continue;
}
if (in_quote) {
continue;
}
if (open_char !== close_char) {
if (c === open_char) {
depth += 1;
if (start === -1) {
start = now
}
// let left = input_str.substr(now);
// console.log(left);
} else if (c === close_char) {
depth -= 1;
if (depth === 0) {
end = now;
break;
}
}
} else {
//开闭相同的时候,相同即可退出
if (c === open_char) {
depth += 1;
if (start === -1) {
start = now
}
if (depth === 2) {
// 开闭相同,这个时候就满足条件了
end = now;
break
}
}
}
}
if (start >= 0 && end > 0) {
return {
start: start,
end: end
}
}
return undefined;
}
function doWxss(dir, cb, mainDir, nowDir) {
let saveDir = dir;
let isSubPkg = mainDir && mainDir.length > 0;
if (isSubPkg) {
saveDir = mainDir
}
function GwxCfg() {
}
GwxCfg.prototype = {
$gwx() {
}
};
for (let i = 0; i < 300; i++) GwxCfg.prototype["$gwx" + i] = GwxCfg.prototype.$gwx;
let runList = {}, pureData = {}, result = {}, actualPure = {}, importCnt = {}, frameName = "", onlyTest = true,
blockCss = [];//custom block css file which won't be imported by others.(no extension name)
function cssRebuild(data) {//need to bind this as {cssFile:__name__} before call
let cssFile;
function statistic(data) {
function addStat(id) {
if (!importCnt[id]) importCnt[id] = 1, statistic(pureData[id]);
else ++importCnt[id];
}
if (typeof data === "number") return addStat(data);
for (let content of data) if (typeof content === "object" && content[0] == 2) addStat(content[1]);
}
function makeup(data) {
var isPure = typeof data === "number";
if (onlyTest) {
statistic(data);
if (!isPure) {
if (data.length == 1 && data[0][0] == 2) data = data[0][1];
else return "";
}
if (!actualPure[data] && !blockCss.includes(wu.changeExt(wu.toDir(cssFile, frameName), ""))) {
console.log("Regard " + cssFile + " as pure import file.");
actualPure[data] = cssFile;
}
return "";
}
let res = [], attach = "";
if (isPure && actualPure[data] != cssFile) {
if (actualPure[data]) return '@import "' + wu.changeExt(wu.toDir(actualPure[data], cssFile), ".wxss") + '";\n';
else {
res.push("/*! Import by _C[" + data + "], whose real path we cannot found. */");
attach = "/*! Import end */";
}
}
let exactData = isPure ? pureData[data] : data;
for (let content of exactData)
if (typeof content === "object") {
switch (content[0]) {
case 0://rpx
res.push(content[1] + "rpx");
break;
case 1://add suffix, ignore it for restoring correct!
break;
case 2://import
res.push(makeup(content[1]));
break;
}
} else res.push(content);
return res.join("") + attach;
}
return () => {
cssFile = this.cssFile;
if (!result[cssFile]) result[cssFile] = "";
result[cssFile] += makeup(data);
};
}
function runVM(name, code) {
let wxAppCode = {}, handle = {cssFile: name};
let vm = new VM({
sandbox: Object.assign(new GwxCfg(), {
__wxAppCode__: wxAppCode,
setCssToHead: cssRebuild.bind(handle),
$gwx(path, global) {
}
})
});
// console.log('do css runVm: ' + name);
vm.run(code);
for (let name in wxAppCode) {
handle.cssFile = path.resolve(saveDir, name);
if (name.endsWith(".wxss")) {
wxAppCode[name]();
}
}
}
function preRun(dir, frameFile, mainCode, files, cb) {
wu.addIO(cb);
runList[path.resolve(dir, "./app.wxss")] = mainCode;
for (let name of files) {
if (name != frameFile) {
wu.get(name, code => {
code = code.replace(/display:-webkit-box;display:-webkit-flex;/gm, '');
code = code.slice(0, code.indexOf("\n"));
if (code.indexOf("setCssToHead(") > -1) {
let lastName = name;
let dirSplit = name.split(nowDir + '/');
if (dirSplit.length > 1) {
lastName = path.resolve(saveDir, dirSplit[1]);
}
runList[lastName] = code.slice(code.indexOf("setCssToHead("));
}
});
}
}
}
function runOnce() {
for (let name in runList) runVM(name, runList[name]);
}
function transformCss(style) {
let ast = csstree.parse(style);
csstree.walk(ast, function (node) {
if (node.type == "Comment") {//Change the comment because the limit of css-tree
node.type = "Raw";
node.value = "\n/*" + node.value + "*/\n";
}
if (node.type == "TypeSelector") {
if (node.name.startsWith("wx-")) node.name = node.name.slice(3);
else if (node.name == "body") node.name = "page";
}
if (node.children) {
const removeType = ["webkit", "moz", "ms", "o"];
let list = {};
node.children.each((son, item) => {
if (son.type == "Declaration") {
if (list[son.property]) {
let a = item, b = list[son.property], x = son, y = b.data, ans = null;
if (x.value.type == 'Raw' && x.value.value.startsWith("progid:DXImageTransform")) {
node.children.remove(a);
ans = b;
} else if (y.value.type == 'Raw' && y.value.value.startsWith("progid:DXImageTransform")) {
node.children.remove(b);
ans = a;
} else {
let xValue = x.value.children && x.value.children.head && x.value.children.head.data.name,
yValue = y.value.children && y.value.children.head && y.value.children.head.data.name;
if (xValue && yValue) for (let type of removeType) if (xValue == `-${type}-${yValue}`) {
node.children.remove(a);
ans = b;
break;
} else if (yValue == `-${type}-${xValue}`) {
node.children.remove(b);
ans = a;
break;
} else {
let mValue = `-${type}-`;
if (xValue.startsWith(mValue)) xValue = xValue.slice(mValue.length);
if (yValue.startsWith(mValue)) yValue = yValue.slice(mValue.length);
}
if (ans === null) ans = b;
}
list[son.property] = ans;
} else list[son.property] = item;
}
});
for (let name in list) if (!name.startsWith('-'))
for (let type of removeType) {
let fullName = `-${type}-${name}`;
if (list[fullName]) {
node.children.remove(list[fullName]);
delete list[fullName];
}
}
}
});
return cssbeautify(csstree.generate(ast), {indent: ' ', autosemicolon: true});
}
wu.scanDirByExt(dir, ".html", files => {
let frameFile = "";
if (fs.existsSync(path.resolve(dir, "page-frame.html")))
frameFile = path.resolve(dir, "page-frame.html");
else if (fs.existsSync(path.resolve(dir, "app-wxss.js")))
frameFile = path.resolve(dir, "app-wxss.js");
else if (fs.existsSync(path.resolve(dir, "page-frame.js")))
frameFile = path.resolve(dir, "page-frame.js");
else throw Error("page-frame-like file is not found in the package by auto.");
wu.get(frameFile, code => {
code = code.replace(/display:-webkit-box;display:-webkit-flex;/gm, '');
let scriptCode = code;
//extract script content from html
if (frameFile.endsWith(".html")) {
try {
const $ = cheerio.load(code);
scriptCode = [].join.apply($('html').find('script').map(function (item) {
return $(this).html();
}, "\n"));
} catch (e) {
//ignore
}
}
let window = {
screen: {
width: 720,
height: 1028,
orientation: {
type: 'vertical'
}
}
};
let navigator = {
userAgent: "iPhone"
};
scriptCode = scriptCode.slice(scriptCode.lastIndexOf('window.__wcc_version__'));
let mainCode = 'window= ' + JSON.stringify(window) +
';\nnavigator=' + JSON.stringify(navigator) +
';\nvar __mainPageFrameReady__ = window.__mainPageFrameReady__ || function(){};var __WXML_GLOBAL__={entrys:{},defines:{},modules:{},ops:[],wxs_nf_init:undefined,total_ops:0};var __vd_version_info__=__vd_version_info__||{}' +
";\n" + scriptCode;
//remove setCssToHead function
let setCssToHeadFuctionStartIndex = mainCode.indexOf("var setCssToHead = function");
let setCssToHeadFunctionParamIndexes = chomp_balanced(mainCode, setCssToHeadFuctionStartIndex, '(', ')');
let setCssToHeadFunctionEndIndexes = chomp_balanced(mainCode, setCssToHeadFunctionParamIndexes.end + 1, '{', '}');
mainCode = mainCode.substr(0, setCssToHeadFuctionStartIndex) + mainCode.substr(setCssToHeadFunctionEndIndexes.end + 1);
//remove var __wxAppCode__ = {};
// let wxAppCodeVarDeclare = "var __wxAppCode__={};";
// let wxAppCodeVarDeclareIndex = mainCode.indexOf(wxAppCodeVarDeclare);
// let wxAppCodeVarDeclareEnd = wxAppCodeVarDeclareIndex + wxAppCodeVarDeclare.length;
// mainCode = mainCode.substr(0, wxAppCodeVarDeclareIndex) + mainCode.substr(wxAppCodeVarDeclareEnd);
code = code.slice(code.lastIndexOf('var setCssToHead = function(file, _xcInvalid'));
code = code.slice(code.lastIndexOf('\nvar _C= ') + 1);
//let oriCode=code;
code = code.slice(0, code.indexOf('\n'));
let vm = new VM({sandbox: {}});
pureData = vm.run(code + "\n_C");
//let mainCode=oriCode.slice(oriCode.indexOf("setCssToHead"),oriCode.lastIndexOf(";var __pageFrameEndTime__"));
console.log("Guess wxss(first turn)...");
preRun(dir, frameFile, mainCode, files, () => {
frameName = frameFile;
onlyTest = true;
runOnce();
onlyTest = false;
console.log("Import count info: %j", importCnt);
for (let id in pureData) if (!actualPure[id]) {
if (!importCnt[id]) importCnt[id] = 0;
if (importCnt[id] <= 1) {
console.log("Cannot find pure import for _C[" + id + "] which is only imported " + importCnt[id] + " times. Let importing become copying.");
} else {
let newFile = path.resolve(dir, "__wuBaseWxss__/" + id + ".wxss");
console.log("Cannot find pure import for _C[" + id + "], force to save it in (" + newFile + ").");
id = Number.parseInt(id);
actualPure[id] = newFile;
cssRebuild.call({cssFile: newFile}, id)();
}
}
console.log("Guess wxss(first turn) done.\nGenerate wxss(second turn)...");
runOnce()
console.log("Generate wxss(second turn) done.\nSave wxss...");
console.log('saveDir: ' + saveDir);
for (let name in result) {
let pathFile = path.resolve(saveDir, wu.changeExt(name, ".wxss"));
wu.save(pathFile, transformCss(result[name]));
}
let delFiles = {};
for (let name of files) delFiles[name] = 8;
delFiles[frameFile] = 4;
cb(delFiles);
});
});
});
}
module.exports = {doWxss: doWxss};
if (require.main === module) {
wu.commandExecute(doWxss, "Restore wxss files.\n\n<dirs...>\n\n<dirs...> restore wxss file from a unpacked directory(Have page-frame.html (or app-wxss.js) and other html file).");
}