perf: 减少侵入式修改

This commit is contained in:
msojocs 2025-07-05 20:05:46 +08:00
parent 54ce4f4565
commit a3e75a647f
3 changed files with 184 additions and 171 deletions

183
res/scripts/hackrequire.js Normal file
View File

@ -0,0 +1,183 @@
/* patch wechat devtools begin */
(() => {
try {
const originalSpawn = require("child_process").spawn;
require("child_process").spawn = function (command, args, options) {
if (command.includes("wcc.exe")) {
command = command.replace("code/package.nw", "package.nw");
command = command.replace("wcc.exe", "wcc");
} else if (command.includes("wcsc.exe")) {
command = command.replace("code/package.nw", "package.nw");
command = command.replace("wcsc.exe", "wcsc");
}
return originalSpawn.apply(this, [command, args, options]);
};
const originalResolve = require("path").resolve;
require("path").resolve = function (...paths) {
if (paths.length === 2 && paths[1].includes("code/package.nw")) {
paths[1] = paths[1].replace("code/package.nw", "package.nw");
}
return originalResolve.apply(this, paths);
};
if (typeof nw === "undefined") {
return;
}
let log = function (content) {
process.stderr.write(content + "\n");
};
let originMenuItem = nw.MenuItem;
nw.MenuItem = function MenuItem(options) {
options = Object.assign({}, options);
delete options.shortcutName;
delete options.shouldEnabled;
if (options.label && (typeof options.label === "string")) {
if (options.label.indexOf("[") !== -1) {
let rest = options.label.split("[").slice(1).join("[").trim();
if (rest[rest.length - 1] === "]") {
rest = rest.slice(0, -1).split("+").map((x) => {
if (!x) { return "+" }
switch (x) {
case "↓": { return "Down"; }
case "↑": { return "Up"; }
case "PAGE↓": { return "PageDown"; }
case "PAGE↑": { return "PageUp"; }
case "←": { return "Left"; }
case "→": { return "Right"; }
default: { return x; }
}
});
if (rest.length > 1) {
options.key = rest[rest.length - 1];
options.modifiers = rest.slice(0, -1).join("+");
} else {
options.key = rest[0];
}
}
options.label = options.label.split("[")[0];
}
if (options.label.indexOf("(&") !== -1) {
options.label = options.label.split("(&")[0];
}
options.label = options.label.replace("&", "").trim();
switch (options.label) {
case "Go to Declaration": { options.label = "转到声明"; break; }
case "Go to References": { options.label = "转到引用"; break; }
case "Find All References": { options.label = "查找所有引用"; break; }
case "Find All Implementations": { options.label = "查找所有实现"; break; }
}
}
return new originMenuItem(options);
};
let originAppend = nw.Menu.prototype.append;
nw.Menu.prototype.append = function (item) {
if (item.parentMenu) {
item.parentMenu.remove(item);
}
item.parentMenu = this;
if ((this.items.length > 0) &&
(this.items[this.items.length - 1].type === "separator") &&
(item.type === "separator")) {
originInsert.call(this, item, this.items.length);
return;
}
if ((this.items.length === 0) && (item.type === "separator")) {
originInsert.call(this, item, this.items.length);
return;
}
return originAppend.call(this, item);
};
let originInsert = nw.Menu.prototype.insert;
nw.Menu.prototype.insert = function (item, index) {
if (item.parentMenu) {
item.parentMenu.remove(item);
}
item.parentMenu = this;
return originInsert.call(this, item, index);
};
const originalOpen = nw.Window.open
nw.Window.open = function (url, options, callback) {
console.warn('[wechat-devtools] nw.Window.open is called, url:', url, 'options:', options);
let cb = callback
if (options.title === '版本更新提示') {
options.inject_js_start = 'js/unpack/hackrequire/index.js';
cb = (...args) => {
const keys = [
"shareData",
"windowMap",
"isSimple",
"masterProxyPort",
"proxyPort",
"masterH2ProxyPort",
"h2ProxyPort"
];
for(let k of keys)
args[0].window.global[k] = global[k];
callback(...args)
}
}
else if (options.title === '云开发控制台') {
cb = (...args) => {
const keys = [
"shareData",
"windowMap",
"isSimple",
"masterProxyPort",
"proxyPort",
"masterH2ProxyPort",
"h2ProxyPort",
'tokenData',
];
for(let k of keys)
args[0].window.global[k] = global[k];
callback(...args)
}
}
else if (options.title.includes('微信开发者工具')) {
cb = (...args) => {
const keys = [
"shareData",
"windowMap",
"isSimple",
"masterProxyPort",
"proxyPort",
"masterH2ProxyPort",
"h2ProxyPort",
'tokenData',
];
for(let k of keys)
args[0].window.global[k] = global[k];
callback(...args)
}
}
return originalOpen.apply(this, [url, options, cb])
}
const originalExec = require('child_process').exec;
require('child_process').exec = function (command, options, callback) {
if (command.includes('open -a Terminal')) {
command = 'gnome-terminal'
}
return originalExec.apply(this, [command, options, callback])
}
} catch (error) {
process.stderr.write(error.message);
process.stderr.write(error.stack);
}
})();
/* patch wechat devtools end */

View File

@ -37,17 +37,6 @@ node "$unpack_script" "$package_dir/core.wxvpkg" "$tmp_dir/core.wxvpkg"
# |_| \_\_____|_| |_____/_/ \_\____|_____| \____\___/|_| \_\_____|
#
# find
open_find_result=$( grep -lr "this.props.onWindowOpenFail());if" "$tmp_dir/core.wxvpkg" )
echo "云开发控制台启动点: $open_find_result"
if [[ ! -z $open_find_result ]];then
# replace
new_cb_handle="this.props.onWindowOpenFail());Object.keys(window).forEach(key=>{if(!e.window[key]){try{e.window[key]=window[key];}catch(e){console.error(e);}}});"
sed -i "s/this.props.onWindowOpenFail());/$new_cb_handle/g" $open_find_result
else
warn "云开发控制台启动点未找到"
fi
token_find_result=$( grep -lr "constructor(){this._sessionToken=\"\",this._tokenMap={}}" "$tmp_dir/core.wxvpkg" )
echo "WebSocket token存储对象位置: $token_find_result"
if [[ ! -z $token_find_result ]];then
@ -57,41 +46,8 @@ else
warn "WebSocket token存储对象位置未找到"
fi
# open -a Terminal "`pwd`" --> gnome-terminal
notice "fix terminal"
find_result=$( grep -lr 'open -a Terminal "`pwd`"' "$tmp_dir/core.wxvpkg" )
echo "Terminal启动位置: $find_result"
if [[ ! -z $find_result ]];then
new_str="gnome-terminal"
sed -i "s#open -a Terminal \"\`pwd\`\"#$new_str#g" "$find_result"
else
warn "Terminal启动位置未找到"
fi
# wcc、wcsc处理设置WINE=fasle环境变量生效
if [[ "$WINE" != 'true' ]];then
# "wcc.exe":!0,"wcsc.exe":!0
find_result=$( grep -lr 'wcc-exec' "$tmp_dir/core.wxvpkg" )
echo "wcc: $find_result"
if [[ ! -z $find_result ]];then
# new_str='{"wcc.bin":!0,"wcsc.bin":!0,wcc:!0,wcsc:!0}'
# sed -i "s#{wcc:!0,wcsc:!0}#$new_str#g" "$find_result"
# new_str='"linux"===process.platform'
# sed -i "s#\"darwin\"===process.platform#$new_str#g" "$find_result"
# return_exp_wcc=$(cat $find_result | grep -P 'return [a-z]+\("wcc"\)' -o) # return ?("wcc")
# return_exp_wcc_replace="${return_exp_wcc//wcc/wcc.bin}" # return ?("wcc.bin")
# return_exp_wcc_replace="${return_exp_wcc//return /${return_exp_wcc_replace},}" # return ?("wcc.bin")
# return_exp_wcsc=$(cat $find_result | grep -P 'return [a-z]+\("wcsc"\)' -o) # return ?("wcsc")
# return_exp_wcsc_replace="${return_exp_wcc_replace//wcc/wcsc}"
sed -i "s#wcc\\.exe#wcc#g" "$find_result"
sed -i "s#wcsc\\.exe#wcsc#g" "$find_result"
sed -i "s#code/package.nw#package.nw#g" "$find_result"
else
warn "wcc位置未找到"
fi
# 处理报错时控制台显示的环境
find_result=$( grep -lr '(env:' "$tmp_dir/core.wxvpkg" )
echo "env: $find_result"

View File

@ -23,133 +23,7 @@ if [ `grep -c "patch wechat devtools begin" $target_file` -ne '0' ];then
fi
tmp_file=$(mktemp)
cat > "$tmp_file" <<EOF
/* patch wechat devtools begin */
/* nw-menu.js */
(() => {
try {
if (typeof nw === "undefined") {
return;
}
let log = function (content) {
process.stderr.write(content + "\n");
};
let originMenuItem = nw.MenuItem;
nw.MenuItem = function MenuItem(options) {
options = Object.assign({}, options);
delete options.shortcutName;
delete options.shouldEnabled;
if (options.label && (typeof options.label === "string")) {
if (options.label.indexOf("[") !== -1) {
let rest = options.label.split("[").slice(1).join("[").trim();
if (rest[rest.length - 1] === "]") {
rest = rest.slice(0, -1).split("+").map((x) => {
if (!x) { return "+" }
switch (x) {
case "↓": { return "Down"; }
case "↑": { return "Up"; }
case "PAGE↓": { return "PageDown"; }
case "PAGE↑": { return "PageUp"; }
case "←": { return "Left"; }
case "→": { return "Right"; }
default: { return x; }
}
});
if (rest.length > 1) {
options.key = rest[rest.length - 1];
options.modifiers = rest.slice(0, -1).join("+");
} else {
options.key = rest[0];
}
}
options.label = options.label.split("[")[0];
}
if (options.label.indexOf("(&") !== -1) {
options.label = options.label.split("(&")[0];
}
options.label = options.label.replace("&", "").trim();
switch (options.label) {
case "Go to Declaration": { options.label = "转到声明"; break; }
case "Go to References": { options.label = "转到引用"; break; }
case "Find All References": { options.label = "查找所有引用"; break; }
case "Find All Implementations": { options.label = "查找所有实现"; break; }
}
}
return new originMenuItem(options);
};
let originAppend = nw.Menu.prototype.append;
nw.Menu.prototype.append = function (item) {
if (item.parentMenu) {
item.parentMenu.remove(item);
}
item.parentMenu = this;
if ((this.items.length > 0) &&
(this.items[this.items.length - 1].type === "separator") &&
(item.type === "separator")) {
originInsert.call(this, item, this.items.length);
return;
}
if ((this.items.length === 0) && (item.type === "separator")) {
originInsert.call(this, item, this.items.length);
return;
}
return originAppend.call(this, item);
};
let originInsert = nw.Menu.prototype.insert;
nw.Menu.prototype.insert = function (item, index) {
if (item.parentMenu) {
item.parentMenu.remove(item);
}
item.parentMenu = this;
return originInsert.call(this, item, index);
};
const originalOpen = nw.Window.open
nw.Window.open = function (url, options, callback) {
console.warn('[wechat-devtools] nw.Window.open is called, url:', url, 'options:', options);
let cb = callback
if (options.title === '版本更新提示') {
options.inject_js_start = 'js/unpack/hackrequire/index.js';
cb = (...args) => {
const keys = [
"shareData",
"windowMap",
"isSimple",
"masterProxyPort",
"proxyPort",
"masterH2ProxyPort",
"h2ProxyPort"
];
for(let k of keys)
args[0].window.global[k] = global[k];
callback(...args)
}
}
return originalOpen.apply(this, [url, options, cb])
}
} catch (error) {
process.stderr.write(error.message);
process.stderr.write(error.stack);
}
})();
/* patch wechat devtools end */
EOF
cat "$root_dir/res/scripts/hackrequire.js" > "$tmp_file"
cat "$target_file" >> "$tmp_file"
cat "$tmp_file" > "$target_file"