Compare commits

..

3 Commits

Author SHA1 Message Date
msojocs
5b68973fc4 perf: 优化处理逻辑 2025-07-19 16:24:05 +08:00
msojocs
080ee2b6df feat: plasma支持 2025-07-17 21:50:59 +08:00
msojocs
465af9b79a refactor: 使用更简洁的主题监听方式 2025-07-17 20:28:41 +08:00
4 changed files with 257 additions and 108 deletions

View File

@ -1,3 +1,6 @@
# 1.06.2504010-3 / 2025-07-
- refactor: 重构主题监听处理方案
# 1.06.2504010-2 / 2025-07-15
- fix: cli使用异常。#147

View File

@ -1,24 +1,30 @@
/* 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);
};
{
// 修正编译器路径
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;
}
@ -111,69 +117,227 @@
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)
{
// 修正新窗口数据丢失的问题
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 === '云开发控制台') {
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)
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])
}
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'
{
// 修正打开外部Terminal的功能
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])
}
}
{
// 修正 暗色/亮色 自动跟随系统
const {spawn, execSync} = require('child_process')
let isDark = (function () {
try {
const { DESKTOP_SESSION } = process.env;
console.log(DESKTOP_SESSION);
let theme = "";
switch (DESKTOP_SESSION) {
case "deepin":
theme = execSync(
`gsettings get com.deepin.dde.appearance gtk-theme`
);
break;
case "gnome":
case "gnome-classic":
theme = execSync(
`gsettings get org.gnome.desktop.interface ${this.gnomeScheme}`
);
break;
case "plasma":
theme = execSync(
`gsettings get org.gnome.desktop.interface color-scheme`,
);
break;
default:
console.warn(
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
);
break;
}
console.log(theme.toString());
return theme.toString().toLowerCase().includes("dark");
} catch (error) {
console.error("尝试获取主题信息失败,使用默认暗色");
return true;
}
})()
const originalMatchMedia = window.matchMedia
window.matchMedia = function (...args) {
console.warn('----------------> matchMedia:', ...args)
const mm = originalMatchMedia.apply(this, args)
Object.defineProperty(mm, 'matches', {
get(){
return isDark
}
})
return mm
}
class CheckDark {
// 监听gsettings monitor org.gnome.desktop.interface gtk-theme
monitorTheme(callback) {
try {
if (this.callback) {
this.callback = callback
return;
}
this.callback = callback
let monitor = null;
const { DESKTOP_SESSION } = process.env;
switch (DESKTOP_SESSION) {
case "deepin":
monitor = spawn("gsettings", [
"monitor",
"com.deepin.dde.appearance",
"gtk-theme",
]);
break;
case "gnome":
case "gnome-classic":
monitor = spawn("gsettings", [
"monitor",
"org.gnome.desktop.interface",
this.gnomeScheme,
]);
break;
case "plasma":
monitor = spawn("gsettings", [
"monitor",
"org.gnome.desktop.interface",
'color-scheme',
]);
break;
default:
console.warn(
`NOT SUPPORTED !!! DESKTOP_SESSION: ${DESKTOP_SESSION}`
);
break;
}
monitor &&
monitor.on("error", (err) => {
console.error("monitorTheme", err);
});
monitor &&
monitor.stdout.on("data", (chunk) => {
const data = chunk.toString();
console.warn('Theme changed:', data)
isDark = data.toLowerCase().includes("dark");
this.callback(isDark)
});
process.on("SIGTERM", (signal) => {
monitor.kill(signal);
});
} catch (err) {
console.error("尝试监听主题失败!", err);
}
}
get gnomeScheme() {
try {
// 判断 Gnome-Shell 版本 from @icepie
const gnomeVersion = execSync(`gnome-shell --version`)
.toString()
.replace(/[\r\n]/g, "")
.split(" ");
const gnomeVersionNum =
gnomeVersion.length == 3 ? Number(gnomeVersion[2]) : 0;
return gnomeVersionNum >= 42 ? "color-scheme" : "gtk-theme";
} catch (err) {
console.error("检查gnome版本失败, 使用gtk-theme", err);
return "gtk-theme";
}
}
}
const checkDark = new CheckDark()
const original = MediaQueryList.prototype.addEventListener
MediaQueryList.prototype.addEventListener = function (...args) {
console.warn('----------> MediaQueryList.addEventListener:', ...args)
checkDark.monitorTheme((isDark) => {
args[1]({
matches: isDark
})
})
return original.apply(this, args)
}
}
{
// 修复云开发控制台
const originalBind = Function.prototype.bind
Function.prototype.bind = function(...args) {
if (args[0]?._tokenMap) {
console.warn('---------set tokenData')
if (window.tokenData) {
args[0]._sessionToken = window.tokenData._sessionToken
args[0]._tokenMap = window.tokenData._tokenMap
}
else
window.tokenData = args[0]
}
return originalBind.apply(this, args)
}
return originalExec.apply(this, [command, options, callback])
}
} catch (error) {
process.stderr.write(error.message);

View File

@ -3,7 +3,7 @@ const { execSync, spawn } = require("child_process");
class CheckDark {
// 监听gsettings monitor org.gnome.desktop.interface gtk-theme
monitorTheme() {
monitorTheme(callback) {
try {
let monitor = null;
const { DESKTOP_SESSION } = process.env;
@ -37,11 +37,8 @@ class CheckDark {
monitor.stdout.on("data", (chunk) => {
// TODO: 防抖动包装
const data = chunk.toString();
const t = data.toLowerCase().includes("dark");
console.log(data);
console.log("dark", t);
// (this._theme = t ? i.Dark : i.Light),
// this._onDidThemeChange.fire(this._theme);
const isDark = data.toLowerCase().includes("dark");
callback(isDark)
});
process.on("SIGTERM", (signal) => {
monitor.kill(signal);
@ -83,7 +80,7 @@ class CheckDark {
}
get gnomeScheme() {
try {
// 判断 Gnome-Shell 版本
// 判断 Gnome-Shell 版本 from @icepie
const gnomeVersion = execSync(`gnome-shell --version`)
.toString()
.replace(/[\r\n]/g, "")
@ -98,7 +95,9 @@ class CheckDark {
}
}
const cd = new CheckDark();
cd.monitorTheme();
cd.monitorTheme((isDark) => {
console.info('is dark:', isDark)
});
console.log(cd.isDark);
function original() {

View File

@ -37,14 +37,14 @@ node "$unpack_script" "$package_dir/core.wxvpkg" "$tmp_dir/core.wxvpkg"
# |_| \_\_____|_| |_____/_/ \_\____|_____| \____\___/|_| \_\_____|
#
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
new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}"
sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result"
else
warn "WebSocket token存储对象位置未找到"
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
# new_constructor="constructor(){if(window.tokenData){/*有就直接用*/this._sessionToken=window.tokenData._sessionToken;this._tokenMap=window.tokenData._tokenMap;}else{/*没有就新建*/this._sessionToken=\"\",this._tokenMap={};window.tokenData=this;/*新建完要给中间人*/}}"
# sed -i "s#constructor(){this._sessionToken=\"\",this._tokenMap={}}#$new_constructor#g" "$token_find_result"
# else
# warn "WebSocket token存储对象位置未找到"
# fi
# wcc、wcsc处理设置WINE=fasle环境变量生效
if [[ "$WINE" != 'true' ]];then
@ -64,23 +64,6 @@ if [[ "$WINE" != 'true' ]];then
echo $timeStamp > "${package_dir}/.build_time"
fi
# fix theme
notice "fix theme"
find_result=$( grep -lr "OSThemeController=" "$tmp_dir/core.wxvpkg" )
echo "theme: $find_result"
if [[ -n $find_result ]];then
# require of child_process
sed -i 's/"use strict";O/"use strict";const {execSync,spawn}=require("child_process");O/' $find_result
# replace listener to monitor
sed -i 's/this.registerListeners()/this.monitorTheme()/' $find_result
# replace check func
sed -i 's/mediaQuery.matches/isDark/' $find_result
# add functions
sed -i 's#}getDefaultTheme#}get isDark(){try{const{DESKTOP_SESSION}=process.env;console.log(DESKTOP_SESSION);let theme="";switch(DESKTOP_SESSION){case"deepin":theme=execSync(`gsettings get com.deepin.dde.appearance gtk-theme`);break;case"gnome":case"gnome-classic":theme=execSync(`gsettings get org.gnome.desktop.interface ${this.gnomeScheme}`);break;default:break}return theme.includes("dark");}catch(err){console.error("尝试获取主题信息失败,使用默认暗色",err);return true;}}get gnomeScheme(){try{const gnomeVersion=execSync(`gnome-shell --version`).toString().replace(/[\\r\\n]/g,"").split(" ");const gnomeVersionNum=gnomeVersion.length==3?Number(gnomeVersion[2]):0;return gnomeVersionNum>=42?"color-scheme":"gtk-theme";}catch(err){console.error("检查gnome版本失败, 使用gtk-theme", err);return "gtk-theme";}}monitorTheme(){try{let monitor=null;const{DESKTOP_SESSION}=process.env;switch(DESKTOP_SESSION){case"deepin":monitor=spawn("gsettings",["monitor","com.deepin.dde.appearance","gtk-theme",]);break;case"gnome":case"gnome-classic":monitor=spawn("gsettings",["monitor","org.gnome.desktop.interface",this.gnomeScheme,]);break;default:console.warn(`NOT SUPPORTED!!!DESKTOP_SESSION:${DESKTOP_SESSION}`);break}monitor\&\&monitor.on("error",(err)=>{console.error("monitorTheme",err)});monitor\&\&monitor.stdout.on("data",e.debounce((chunk)=>{const data=chunk.toString();const t=data.toLowerCase().includes("dark");(this._theme=t?i.Dark:i.Light),this._onDidThemeChange.fire(this._theme)},400));process.on("SIGTERM",(signal)=>{monitor.kill(signal);});}catch(err){console.error("尝试监听主题失败!", err);}}getDefaultTheme#' $find_result
else
warn "theme位置未找到"
fi
# pack 路径 到 文件
notice "pack"
node "$pack_script" "$tmp_dir/core.wxvpkg" "$package_dir/core.wxvpkg"