window自动化分类:1.新增选择窗口(inspect),支持高亮指定元素,并查看元素的名称、类型、句柄、xpath等各项属性及所在窗口各项属性 2.新增界面自动化,支持通过xpath、name、id等条件在对指定元素执行点击、设置值、模拟输入、高亮等操作

This commit is contained in:
fofolee 2025-01-17 18:51:11 +08:00
parent 61d711765b
commit 10b67c919a
11 changed files with 2203 additions and 2110 deletions

View File

@ -7,5 +7,8 @@
"eslint.validate": ["javascript", "javascriptreact", "typescript", "vue"],
"[vue]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -113,15 +113,13 @@ const buildCsharpFeature = async (feature) => {
fs.copyFile(srcCsPath, destCsPath, (err) => {
if (err) return reject(err.toString());
child_process.exec(
`${cscPath} /nologo ${references}/out:"${exePath}" "${destCsPath}"`,
{ encoding: null },
(err, stdout) => {
if (err) return reject(iconv.decode(stdout, "gbk"));
else resolve(iconv.decode(stdout, "gbk"));
fs.unlink(destCsPath, () => {});
}
);
const command = `${cscPath} /nologo ${references}/out:"${exePath}" "${destCsPath}"`;
console.log(command);
child_process.exec(command, { encoding: null }, (err, stdout) => {
if (err) return reject(iconv.decode(stdout, "gbk"));
else resolve(iconv.decode(stdout, "gbk"));
fs.unlink(destCsPath, () => {});
});
});
});
};
@ -181,6 +179,11 @@ const runCsharpFeature = async (feature, args = [], options = {}) => {
encoding: null,
},
(err, stdout, stderr) => {
console.log({
err,
stdout: iconv.decode(stdout, "gbk"),
stderr: iconv.decode(stderr, "gbk"),
});
if (err || Buffer.byteLength(stderr) > 0)
reject(iconv.decode(stderr || stdout, "gbk"));
else reslove(iconv.decode(stdout, "gbk"));

View File

@ -62,16 +62,20 @@ public class AutomationTool
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
private const uint WM_KEYDOWN = 0x0100;
private const uint WM_KEYUP = 0x0101;
private const uint WM_KEYPRESS = 0x0102;
private const uint WM_CHAR = 0x0102;
private const uint WM_SETTEXT = 0x000C;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
private const uint WM_RBUTTONDOWN = 0x0204;
private const uint WM_RBUTTONUP = 0x0205;
private const uint WM_LBUTTONDBLCLK = 0x0203;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_CHAR = 0x0102;
private const int WM_SETTEXT = 0x000C;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
private const int WM_LBUTTONDBLCLK = 0x0203;
// 添加按键状态标志
private const int KEYEVENTF_KEYDOWN = 0x0;
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
@ -131,6 +135,11 @@ public class AutomationTool
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
private const uint MAPVK_VK_TO_VSC = 0x00;
private const uint MAPVK_VSC_TO_VK = 0x01;
private const uint MAPVK_VK_TO_CHAR = 0x02;
private const uint MAPVK_VSC_TO_VK_EX = 0x03;
#endregion
public static void Main(string[] args)
@ -460,20 +469,54 @@ public class AutomationTool
// 按下修饰键
foreach (byte modifier in modifierKeys)
{
PostMessage(hWnd, WM_KEYDOWN, modifier, 0);
// 获取扫描码
uint scanCode = MapVirtualKey((uint)modifier, MAPVK_VK_TO_VSC);
// 构造 lParam
int lParamDown = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0x1 << 24); // extended key for modifiers
PostMessage(hWnd, WM_KEYDOWN, modifier, lParamDown);
Thread.Sleep(10); // 短暂延迟确保修饰键被正确识别
}
// 发送主键字符
// 发送主键
if (mainKey > 0)
{
PostMessage(hWnd, WM_CHAR, mainKey, 0);
// 获取主键的扫描码
uint scanCode = MapVirtualKey((uint)mainKey, MAPVK_VK_TO_VSC);
// 构造主键的 lParam
int lParamDown = 0x00000001 | // repeat count = 1
((int)scanCode << 16); // scan code
int lParamUp = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0xC0 << 24); // key up + previous key state
// 发送按键按下
PostMessage(hWnd, WM_KEYDOWN, mainKey, lParamDown);
Thread.Sleep(10); // 短暂延迟
// 发送按键释放
PostMessage(hWnd, WM_KEYUP, mainKey, lParamUp);
Thread.Sleep(10); // 短暂延迟
}
// 释放修饰键
// 释放修饰键(反序释放)
for (int i = modifierKeys.Count - 1; i >= 0; i--)
{
byte modifier = modifierKeys[i];
PostMessage(hWnd, WM_KEYUP, modifier, 0);
uint scanCode = MapVirtualKey((uint)modifier, MAPVK_VK_TO_VSC);
// 构造释放修饰键的 lParam
int lParamUp = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0xC1 << 24); // extended key + key up + previous key state
PostMessage(hWnd, WM_KEYUP, modifier, lParamUp);
Thread.Sleep(10); // 短暂延迟
}
// 如果有多个按键组合,等待一下
@ -484,58 +527,6 @@ public class AutomationTool
}
}
private static bool IsSpecialKey(byte vKey)
{
switch (vKey)
{
// 修饰键
case 0xA0: // VK_LSHIFT
case 0xA2: // VK_LCONTROL
case 0xA4: // VK_LMENU
case 0x5B: // VK_LWIN
// 控制键
case 0x08: // VK_BACK
case 0x09: // VK_TAB
case 0x0D: // VK_RETURN
case 0x1B: // VK_ESCAPE
case 0x20: // VK_SPACE
case 0x2E: // VK_DELETE
// 方向键
case 0x25: // VK_LEFT
case 0x26: // VK_UP
case 0x27: // VK_RIGHT
case 0x28: // VK_DOWN
// 导航键
case 0x24: // VK_HOME
case 0x23: // VK_END
case 0x21: // VK_PRIOR (PageUp)
case 0x22: // VK_NEXT (PageDown)
case 0x2D: // VK_INSERT
// 功能键
case 0x70: // VK_F1
case 0x71: // VK_F2
case 0x72: // VK_F3
case 0x73: // VK_F4
case 0x74: // VK_F5
case 0x75: // VK_F6
case 0x76: // VK_F7
case 0x77: // VK_F8
case 0x78: // VK_F9
case 0x79: // VK_F10
case 0x7A: // VK_F11
case 0x7B: // VK_F12
// 其他常用键
case 0x14: // VK_CAPITAL
case 0x90: // VK_NUMLOCK
case 0x91: // VK_SCROLL
case 0x2C: // VK_SNAPSHOT
case 0x13: // VK_PAUSE
return true;
default:
return false;
}
}
private static void SendText(IntPtr hWnd, string text)
{
StringBuilder sb = new StringBuilder(text);
@ -554,56 +545,74 @@ public class AutomationTool
return null;
}
private static bool HasArgument(string[] args, string key)
{
return Array.Exists(args, arg => arg.Equals(key, StringComparison.OrdinalIgnoreCase));
}
private static byte GetVirtualKeyCode(string key)
{
switch (key.ToLower())
switch (key.ToUpper())
{
case "ctrl":
case "^": return 0xA2; // VK_LCONTROL
case "alt": return 0xA4; // VK_LMENU
case "shift": return 0xA0; // VK_LSHIFT
case "win": return 0x5B; // VK_LWIN
case "enter": return 0x0D;
case "tab": return 0x09;
case "esc": return 0x1B;
case "space": return 0x20;
case "backspace": return 0x08;
case "delete": return 0x2E;
// 方向键
case "left": return 0x25; // VK_LEFT
case "up": return 0x26; // VK_UP
case "right": return 0x27; // VK_RIGHT
case "down": return 0x28; // VK_DOWN
// 导航键
case "home": return 0x24; // VK_HOME
case "end": return 0x23; // VK_END
case "pageup": return 0x21; // VK_PRIOR
case "pagedown": return 0x22; // VK_NEXT
case "insert": return 0x2D; // VK_INSERT
// 功能键
case "f1": return 0x70; // VK_F1
case "f2": return 0x71;
case "f3": return 0x72;
case "f4": return 0x73;
case "f5": return 0x74;
case "f6": return 0x75;
case "f7": return 0x76;
case "f8": return 0x77;
case "f9": return 0x78;
case "f10": return 0x79;
case "f11": return 0x7A;
case "f12": return 0x7B;
// 其他常用键
case "capslock": return 0x14; // VK_CAPITAL
case "numlock": return 0x90; // VK_NUMLOCK
case "scrolllock": return 0x91; // VK_SCROLL
case "printscreen": return 0x2C; // VK_SNAPSHOT
case "pause": return 0x13; // VK_PAUSE
// 修饰键
case "CTRL":
case "^": return 0x11; // VK_CONTROL
case "ALT":
case "%": return 0x12; // VK_MENU
case "SHIFT": return 0x10; // VK_SHIFT
// 特殊按键
case "{BACKSPACE}":
case "{BS}":
case "{BKSP}": return 0x08; // VK_BACK
case "{BREAK}": return 0x03; // VK_CANCEL
case "{CAPSLOCK}": return 0x14; // VK_CAPITAL
case "{DELETE}":
case "{DEL}": return 0x2E; // VK_DELETE
case "{DOWN}": return 0x28; // VK_DOWN
case "{END}": return 0x23; // VK_END
case "{ENTER}":
case "{RETURN}": return 0x0D; // VK_RETURN
case "{ESC}": return 0x1B; // VK_ESCAPE
case "{HELP}": return 0x2F; // VK_HELP
case "{HOME}": return 0x24; // VK_HOME
case "{INSERT}":
case "{INS}": return 0x2D; // VK_INSERT
case "{LEFT}": return 0x25; // VK_LEFT
case "{NUMLOCK}": return 0x90; // VK_NUMLOCK
case "{PGDN}": return 0x22; // VK_NEXT
case "{PGUP}": return 0x21; // VK_PRIOR
case "{PRTSC}": return 0x2C; // VK_SNAPSHOT
case "{RIGHT}": return 0x27; // VK_RIGHT
case "{SCROLLLOCK}": return 0x91; // VK_SCROLL
case "{TAB}": return 0x09; // VK_TAB
case "{UP}": return 0x26; // VK_UP
// 功能键 F1-F16
case "{F1}": return 0x70;
case "{F2}": return 0x71;
case "{F3}": return 0x72;
case "{F4}": return 0x73;
case "{F5}": return 0x74;
case "{F6}": return 0x75;
case "{F7}": return 0x76;
case "{F8}": return 0x77;
case "{F9}": return 0x78;
case "{F10}": return 0x79;
case "{F11}": return 0x7A;
case "{F12}": return 0x7B;
// 数字键盘
case "{ADD}": return 0x6B; // VK_ADD
case "{SUBTRACT}": return 0x6D; // VK_SUBTRACT
case "{MULTIPLY}": return 0x6A; // VK_MULTIPLY
case "{DIVIDE}": return 0x6F; // VK_DIVIDE
case "{NUMPAD0}": return 0x60; // VK_NUMPAD0
case "{NUMPAD1}": return 0x61;
case "{NUMPAD2}": return 0x62;
case "{NUMPAD3}": return 0x63;
case "{NUMPAD4}": return 0x64;
case "{NUMPAD5}": return 0x65;
case "{NUMPAD6}": return 0x66;
case "{NUMPAD7}": return 0x67;
case "{NUMPAD8}": return 0x68;
case "{NUMPAD9}": return 0x69;
default:
if (key.Length == 1)
{

View File

@ -316,13 +316,20 @@ public class WindowManager
GetWindowText(hwnd, title, title.Capacity);
GetClassName(hwnd, className, className.Capacity);
// 获取窗口位置和大小
RECT rect = new RECT();
GetWindowRect(hwnd, out rect);
// 获取进程信息
uint processId = 0;
GetWindowThreadProcessId(hwnd, out processId);
string processName = "";
string processPath = "";
try
{
var process = Process.GetProcessById((int)processId);
processName = process.ProcessName;
processPath = process.MainModule.FileName;
}
catch { }
@ -331,7 +338,12 @@ public class WindowManager
{ "handle", hwnd.ToInt64() },
{ "title", title.ToString() },
{ "class", className.ToString() },
{ "process", processName }
{ "x", rect.Left },
{ "y", rect.Top },
{ "width", rect.Right - rect.Left },
{ "height", rect.Bottom - rect.Top },
{ "processName", processName },
{ "processPath", processPath }
};
}

View File

@ -1,168 +1,144 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 列出所有元素
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @returns {object[]} 元素列表
* 执行自动化操作
* @param {string} type - 操作类型, 可选值: "inspect"|"click"|"setvalue"|"getvalue"|"select"|"expand"|"scroll"|"wait"|"focus"|"highlight"
* @param {Object} params - 参数对象
* @param {string} params.by - 查找方式"xpath"|"id"|"name"|"condition"
* @param {string} params.searchValue - 搜索值
* @param {string} params.window - 窗口标题句柄进程名类名
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const listElements = async function (method, window, options = {}) {
const { filter, scope } = options;
const args = ["-type", "list"];
async function runAutomation(
type,
window = "",
by = "xpath",
searchValue = "",
params = {}
) {
const args = [];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
if (scope) args.push("-scope", scope);
try {
const result = await runCsharpFeature("automation", args);
console.log(result);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
if (window) {
args.push("-window", window);
}
return [];
};
/**
* 点击元素
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} by 查找方式"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {string} options.pattern 点击模式"invoke"|"toggle"
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const clickElement = async function (method, window, by, value, options = {}) {
const { pattern = "invoke", background = false } = options;
const args = ["-type", "click", "-by", by, "-value", value];
args.push("-type", type);
// 通用参数处理
if (type !== "inspect") {
switch (by) {
case "xpath":
args.push("-xpath", searchValue);
break;
case "id":
args.push("-id", searchValue);
break;
case "name":
args.push("-name", searchValue);
break;
case "condition":
args.push("-condition", searchValue);
break;
}
}
// 特定命令的参数处理
switch (type) {
case "inspect":
if (params) {
args.push("-position");
if (params.x && params.y) {
args.push(`${params.x},${params.y}`);
}
}
break;
case "setvalue":
if (params.newValue !== undefined) {
args.push("-value", params.newValue);
}
if (params.sendenter) {
args.push("-sendenter");
}
break;
case "select":
if (params.item) {
args.push("-item", params.item);
}
break;
case "expand":
if (params.expand !== undefined) {
args.push("-expand", params.expand);
}
break;
case "scroll":
if (params.direction) {
args.push("-direction", params.direction);
}
if (params.amount !== undefined) {
args.push("-amount", params.amount);
}
break;
case "wait":
if (params.condition) {
args.push("-condition", params.condition);
}
if (params.timeout !== undefined) {
args.push("-timeout", params.timeout);
}
break;
case "highlight":
if (params.duration !== undefined) {
args.push("-duration", params.duration);
}
break;
case "sendkeys":
if (params.keys) {
args.push("-keys", params.keys);
}
break;
}
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (pattern) args.push("-pattern", pattern);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) {
return { success: true, element: JSON.parse(result) };
const resultStr = result.toString().trim();
if (type === "inspect") return JSON.parse(resultStr);
if (resultStr === "true") return { success: true };
try {
return { success: true, data: JSON.parse(resultStr) };
} catch (err) {
return { success: true, data: resultStr };
}
}
} catch (err) {
error = err.toString();
error = err.toString().trim();
}
return { success: true, error };
};
/**
* 设置元素值
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} by 查找方式"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {string} newValue 要设置的值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const setElementValue = async function (
method,
window,
by,
value,
newValue,
options = {}
) {
const { background = false } = options;
const args = [
"-type",
"setvalue",
"-by",
by,
"-value",
value,
"-newvalue",
newValue,
];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return { success: true, element: JSON.parse(result) };
} catch (err) {
error = err.toString();
}
if (type === "inspect") return { error };
return { success: false, error };
};
/**
* 获取元素值
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} by 查找方式"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {object} 元素值
*/
const getElementValue = async function (
method,
window,
by,
value,
options = {}
) {
const { background = false } = options;
const args = ["-type", "getvalue", "-by", by, "-value", value];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
error = err.toString();
}
return { success: false, error };
};
/**
* 检查元素
* @param {object} options 选项
* @param {number} options.timeout 超时时间()
* @returns {object} 元素信息
*/
const inspectElement = async function () {
const args = ["-type", "inspect"];
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
}
return [];
};
}
module.exports = {
listElements,
clickElement,
setElementValue,
getElementValue,
inspectElement,
inspect: () => runAutomation("inspect"),
inspectPosition: (position) =>
runAutomation("inspect", null, null, null, position || {}),
click: (...args) => runAutomation("click", ...args),
setvalue: (...args) => runAutomation("setvalue", ...args),
getvalue: (...args) => runAutomation("getvalue", ...args),
select: (...args) => runAutomation("select", ...args),
expand: (...args) => runAutomation("expand", ...args),
scroll: (...args) => runAutomation("scroll", ...args),
wait: (...args) => runAutomation("wait", ...args),
focus: (...args) => runAutomation("focus", ...args),
highlight: (...args) => runAutomation("highlight", ...args),
sendkeys: (...args) => runAutomation("sendkeys", ...args),
};

View File

@ -1,140 +1,114 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 键盘操作
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} keys 键盘按键
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
* 执行消息发送操作
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"inspect"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题句柄进程名类名
* @param {string} params.action - 动作类型 "keys""text""click"
* @param {string} params.value - 操作的值如按键文本等
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const sendKeys = async function (method, window, keys, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "keys", "-value", keys];
async function runSendMessage(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, action, value, options = {} } = params;
const { control, text, pos, filter, background = false } = options;
// 通用参数
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return {
success: true,
control: JSON.parse(result),
};
}
} catch (err) {
error = err.toString();
if (method !== "active" && window) {
args.push("-window", window);
}
return { success: false, error };
};
/**
* 发送文本
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} text 文本
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const sendText = async function (method, window, text, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "text", "-value", text];
// 特定命令的参数处理
switch (type) {
case "keyboard":
args.push("-action", action);
if (value) {
args.push("-value", value);
}
if (control) {
args.push("-control", control);
}
break;
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
case "mouse":
args.push("-action", action);
if (control) {
args.push("-control", control);
}
if (text) {
args.push("-text", text);
}
if (pos) {
args.push("-pos", pos);
}
break;
case "inspect":
if (filter) {
args.push("-filter", filter);
}
break;
}
return { success: false, error };
};
/**
* 鼠标点击
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {string} action 动作"click"|"doubleClick"|"rightClick"
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {string} options.text 控件文本
* @param {string} options.pos 点击位置x,y
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const click = async function (method, window, action = "click", options = {}) {
const { control, text, pos, background = false } = options;
const args = ["-type", "mouse", "-action", action];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
if (text) args.push("-text", text);
if (pos) args.push("-pos", pos);
// 后台操作参数
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
const jsonResult = JSON.parse(result);
if (type === "inspect") {
return jsonResult;
}
return { success: true, control: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
}
if (type === "inspect") return [];
return { success: false, error };
};
/**
* 获取窗口控件树
* @param {string} method 窗口类型"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄进程名类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
* @returns {object} 控件树
*/
const inspectWindow = async function (method, window, options = {}) {
const { filter, background = false } = options;
const args = ["-type", "inspect"];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
args.push("-background", background.toString());
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) return JSON.parse(result);
} catch (error) {
console.log(error);
}
return [];
};
}
module.exports = {
sendKeys,
sendText,
click,
inspectWindow,
sendKeys: (method, window, keys, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "keys",
value: keys,
options,
}),
sendText: (method, window, text, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "text",
value: text,
options,
}),
click: (method, window, action = "click", options = {}) =>
runSendMessage("mouse", {
method,
window,
action,
options,
}),
inspectWindow: (method, window, options = {}) =>
runSendMessage("inspect", {
method,
window,
options,
}),
};

View File

@ -1,271 +1,98 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 窗口置顶只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被置顶的窗口信息
* 执行窗口操作
* @param {string} type - 操作类型, 可选值: "topmost"|"opacity"|"rect"|"state"|"close"|"focus"|"border"|"clickthrough"|"info"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题句柄进程名类名
* @param {*} params.value - 操作的值不同操作类型对应不同的值类型
* @returns {Promise<Object>} - 操作结果
*/
async function setTopMost(method, value, isTopMost) {
const args = [
"-type",
"topmost",
"-method",
method,
"-window",
value,
"-value",
isTopMost.toString(),
];
async function runWindow(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, value } = params;
// 通用参数
args.push("-method", method);
if (window) {
args.push("-window", window);
}
// 特定命令的参数处理
switch (type) {
case "topmost":
case "border":
case "clickthrough":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "opacity":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "rect":
if (value && typeof value === "object") {
const { x = 0, y = 0, width = 0, height = 0 } = value;
args.push("-value", `${x},${y},${width},${height}`);
}
break;
case "state":
if (value) {
args.push("-value", value);
}
break;
}
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return {
success: true,
window: JSON.parse(windowInfo),
};
const result = await runCsharpFeature("window", args);
if (result) {
const jsonResult = JSON.parse(result);
if (type === "info") {
return jsonResult;
}
return { success: true, window: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
console.log(error);
}
return { success: false, error };
}
/**
* 设置窗口透明度只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {number} opacity 透明度 0-100
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置透明度的窗口信息
*/
async function setOpacity(method, value, opacity) {
const args = [
"-type",
"opacity",
"-method",
method,
"-window",
value,
"-value",
opacity.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
if (type === "info") return [];
return { success: false, error };
}
/**
* 设置窗口位置和大小只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {number} x X坐标
* @param {number} y Y坐标
* @param {number} width 宽度
* @param {number} height 高度
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置位置和大小的窗口信息
*/
async function setWindowRect(method, value, x, y, width, height) {
const args = [
"-type",
"rect",
"-method",
method,
"-window",
value,
"-value",
`${x},${y},${width},${height}`,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口状态只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {string} state 状态"normal"|"maximize"|"minimize"
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置状态的窗口信息
*/
async function setWindowState(method, value, state) {
const args = [
"-type",
"state",
"-method",
method,
"-window",
value,
"-value",
state,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 关闭窗口只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被关闭的窗口信息
*/
async function closeWindow(method, value) {
const args = ["-type", "close", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口焦点只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置焦点的窗口信息
*/
async function setFocus(method, value) {
const args = ["-type", "focus", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口边框只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {boolean} hasBorder 是否显示边框
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置边框的窗口信息
*/
async function setBorder(method, value, hasBorder) {
const args = [
"-type",
"border",
"-method",
method,
"-window",
value,
"-value",
hasBorder.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口点击穿透只操作第一个找到的窗口
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @param {boolean} isTransparent 是否点击穿透
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置点击穿透的窗口信息
*/
async function setClickThrough(method, value, isTransparent) {
const args = [
"-type",
"clickthrough",
"-method",
method,
"-window",
value,
"-value",
isTransparent.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 获取窗口信息返回所有匹配的窗口信息
* @param {string} method 查找方式"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题句柄进程名类名
* @returns {Object} 所有匹配的窗口信息
*/
async function getWindowInfo(method, value) {
const args = ["-type", "info", "-method", method, "-window", value];
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) return JSON.parse(windowInfo);
} catch (err) {
console.log(err);
}
return [];
}
module.exports = {
setTopMost,
setOpacity,
setWindowRect,
setWindowState,
closeWindow,
setFocus,
setBorder,
setClickThrough,
getWindowInfo,
setTopMost: (method, window, isTopMost) =>
runWindow("topmost", { method, window, value: isTopMost }),
setOpacity: (method, window, opacity) =>
runWindow("opacity", { method, window, value: opacity }),
setWindowRect: (method, window, x, y, width, height) =>
runWindow("rect", { method, window, value: { x, y, width, height } }),
setWindowState: (method, window, state) =>
runWindow("state", { method, window, value: state }),
closeWindow: (method, window) => runWindow("close", { method, window }),
setFocus: (method, window) => runWindow("focus", { method, window }),
setBorder: (method, window, hasBorder) =>
runWindow("border", { method, window, value: hasBorder }),
setClickThrough: (method, window, isTransparent) =>
runWindow("clickthrough", { method, window, value: isTransparent }),
getWindowInfo: (method, window) => runWindow("info", { method, window }),
};

View File

@ -329,7 +329,9 @@ export default defineComponent({
if (this.options.multiSelect) {
this.toggleSelectItem(option);
} else {
const value = this.getItemValue(option);
const value = this.options.appendItem
? `${this.inputValue}${this.getItemValue(option)}`
: this.getItemValue(option);
this.$emit("update:modelValue", newVarInputVal("str", value));
}
},

View File

@ -1,3 +1,5 @@
import { newVarInputVal } from "js/composer/varInputValManager.js";
const controlClass = [
// 基础控件
{ value: "Button", label: "按钮 (Button)" },
@ -45,6 +47,53 @@ const controlClass = [
{ value: "GroupBox", label: "分组框 (GroupBox)" },
];
const sendKeys = [
// 特殊按键
{ value: "{ENTER}", label: "回车键 (Enter)" },
{ value: "{BACKSPACE}", label: "退格键 (Backspace)" },
{ value: "{BREAK}", label: "Break键" },
{ value: "{CAPSLOCK}", label: "大写锁定 (Caps Lock)" },
{ value: "{DELETE}", label: "删除键 (Delete)" },
{ value: "{END}", label: "End键" },
{ value: "{ESC}", label: "ESC键" },
{ value: "{HELP}", label: "帮助键" },
{ value: "{HOME}", label: "Home键" },
{ value: "{INSERT}", label: "插入键 (Insert)" },
{ value: "{INS}", label: "插入键 (Ins)" },
{ value: "{NUMLOCK}", label: "数字锁定键" },
{ value: "{PGDN}", label: "下一页 (Page Down)" },
{ value: "{PGUP}", label: "上一页 (Page Up)" },
{ value: "{PRTSC}", label: "打印屏幕键" },
{ value: "{SCROLLLOCK}", label: "滚动锁定键" },
{ value: "{TAB}", label: "Tab键" },
{ value: "{DOWN}", label: "向下键" },
{ value: "{LEFT}", label: "向左键" },
{ value: "{RIGHT}", label: "向右键" },
{ value: "{UP}", label: "向上键" },
// 功能键
...new Array(12).fill(0).map((_, index) => ({
value: `{F${index + 1}}`,
label: `F${index + 1}`,
})),
// 数字键盘
...new Array(10).fill(0).map((_, index) => ({
value: `{NUMPAD${index}}`,
label: `小键盘 ${index}`,
})),
{ value: "{ADD}", label: "小键盘加号" },
{ value: "{SUBTRACT}", label: "小键盘减号" },
{ value: "{MULTIPLY}", label: "小键盘乘号" },
{ value: "{DIVIDE}", label: "小键盘除号" },
];
const modifierKeys = [
// 修饰键组合示例
{ value: "^", label: "Ctrl" },
{ value: "%", label: "Alt" },
{ value: "+", label: "Shift" },
{ value: "^c", label: "Ctrl+C" },
];
const registryPaths = [
// 系统设置
{
@ -130,25 +179,100 @@ const searchWindowConfig = [
},
];
const searchElementConfig = [
{
label: "窗口句柄",
component: "VariableInput",
icon: "window",
width: 12,
placeholder: "留空则使用当前活动窗口",
defaultValue: newVarInputVal("str", ""),
},
{
label: "元素查找方式",
component: "q-select",
icon: "search",
width: 4,
options: [
{ label: "XPath", value: "xpath" },
{ label: "AutomationId", value: "id" },
{ label: "Name", value: "name" },
{ label: "组合条件", value: "condition" },
],
defaultValue: "xpath",
},
{
label: "查找值",
component: "VariableInput",
icon: "account_tree",
width: 8,
placeholder: "XPath: /Pane[3]/Edit[2], 组合条件: name=按钮&type=Button",
},
];
export const windowsCommands = {
label: "Win自动化",
icon: "window",
defaultOpened: false,
commands: [
// 获取窗口
{
value: "quickcomposer.windows.window.getWindowInfo",
label: "搜索/选择窗口",
desc: "搜索/选择窗口",
icon: "window",
isAsync: true,
config: [],
subCommands: [
{
config: searchWindowConfig,
value: "quickcomposer.windows.window.getWindowInfo",
label: "搜索窗口",
icon: "search",
outputVariable: "windowInfo",
saveOutput: true,
},
{
value: "quickcomposer.windows.automation.inspect",
label: "手动选择窗口",
icon: "my_location",
},
{
value: "quickcomposer.windows.automation.inspectPosition",
label: "从坐标选择窗口",
icon: "location_on",
config: [
{
component: "OptionEditor",
options: {
x: {
label: "X坐标",
component: "NumberInput",
icon: "arrow_right",
placeholder: "留空使用当前鼠标位置",
width: 6,
},
y: {
label: "Y坐标",
component: "NumberInput",
icon: "arrow_drop_down",
placeholder: "留空使用当前鼠标位置",
width: 6,
},
},
},
],
},
],
},
// 窗口
{
value: "quickcomposer.windows.window.setTopMost",
label: "窗口控制",
desc: "Windows窗口操作",
icon: "window",
config: searchWindowConfig,
subCommands: [
{
value: "quickcomposer.windows.window.getWindowInfo",
label: "窗口信息",
icon: "info",
outputVariable: "windowInfo",
saveOutput: true,
},
{
value: "quickcomposer.windows.window.setTopMost",
label: "窗口置顶",
@ -293,9 +417,220 @@ export const windowsCommands = {
],
isAsync: true,
},
// automation
{
value: "quickcomposer.windows.automation.click",
label: "界面自动化",
desc: "Windows界面自动化操作",
icon: "smart_button",
isAsync: true,
config: searchElementConfig,
subCommands: [
{
value: "quickcomposer.windows.automation.click",
label: "点击元素",
icon: "mouse",
},
{
value: "quickcomposer.windows.automation.sendkeys",
label: "模拟键盘输入",
icon: "keyboard",
config: [
{
component: "OptionEditor",
options: {
keys: {
label: "输入内容",
component: "VariableInput",
icon: "keyboard",
width: 12,
placeholder:
"模拟键盘输入支持按键、组合键、中文ab中文^a{BACKSPACE}",
options: {
items: [...modifierKeys, ...sendKeys],
appendItem: true,
},
},
},
},
],
},
{
value: "quickcomposer.windows.automation.setvalue",
label: "设置值",
icon: "edit",
config: [
{
component: "OptionEditor",
options: {
newValue: {
label: "新值",
component: "VariableInput",
icon: "edit",
width: 9,
placeholder: "要设置的新值",
},
sendenter: {
label: "发送回车",
component: "CheckButton",
icon: "keyboard",
width: 3,
},
},
},
],
},
{
value: "quickcomposer.windows.automation.getvalue",
label: "获取值",
icon: "content_paste",
outputVariable: "elementValue",
},
{
value: "quickcomposer.windows.automation.select",
label: "选择项目",
icon: "list",
config: [
{
component: "OptionEditor",
options: {
item: {
label: "选择项",
component: "VariableInput",
icon: "check_box",
width: 12,
placeholder: "要选择的项目名称",
},
},
},
],
},
{
value: "quickcomposer.windows.automation.expand",
label: "展开/折叠",
icon: "unfold_more",
config: [
{
component: "OptionEditor",
options: {
expand: {
label: "操作",
component: "ButtonGroup",
icon: "unfold_more",
width: 12,
options: [
{ label: "展开", value: "true" },
{ label: "折叠", value: "false" },
],
},
},
defaultValue: {
expand: "true",
},
},
],
},
{
value: "quickcomposer.windows.automation.scroll",
label: "滚动",
icon: "swap_vert",
config: [
{
component: "OptionEditor",
options: {
direction: {
label: "方向",
component: "ButtonGroup",
icon: "swap_vert",
width: 6,
options: [
{ label: "垂直", value: "vertical" },
{ label: "水平", value: "horizontal" },
],
},
amount: {
label: "位置",
component: "NumberInput",
icon: "straighten",
width: 6,
min: 0,
max: 100,
step: 10,
},
},
defaultValue: {
direction: "vertical",
amount: 0,
},
},
],
},
{
value: "quickcomposer.windows.automation.wait",
label: "等待元素",
icon: "hourglass_empty",
config: [
{
component: "OptionEditor",
options: {
condition: {
label: "条件",
component: "VariableInput",
icon: "filter_alt",
width: 8,
placeholder: "name=xx;type=Button",
},
timeout: {
label: "超时(秒)",
component: "NumberInput",
icon: "timer",
width: 4,
min: 1,
max: 3600,
step: 10,
},
},
defaultValue: {
timeout: 30,
},
},
],
},
{
value: "quickcomposer.windows.automation.focus",
label: "设置焦点",
icon: "center_focus_strong",
},
{
value: "quickcomposer.windows.automation.highlight",
label: "高亮显示",
icon: "highlight",
config: [
{
component: "OptionEditor",
options: {
duration: {
label: "持续时间(秒)",
component: "NumberInput",
icon: "timer",
width: 12,
min: 1,
max: 60,
step: 5,
},
},
defaultValue: {
duration: 2,
},
},
],
},
],
},
// sendmessage
{
value: "quickcomposer.windows.sendmessage.inspectWindow",
label: "界面自动化(sendmessage)",
label: "界面自动化(后台)",
desc: "Windows界面自动化操作",
icon: "smart_button",
isAsync: true,
@ -446,7 +781,11 @@ export const windowsCommands = {
component: "VariableInput",
icon: "keyboard",
width: 12,
placeholder: "按键组合多个逗号隔开ctrl+a,a,b",
placeholder: "多个逗号隔开a,b,{ENTER},不支持组合键",
options: {
items: sendKeys,
appendItem: true,
},
},
{
key: "options",
@ -478,6 +817,7 @@ export const windowsCommands = {
},
],
},
// 监控
{
value: "quickcomposer.windows.monitor.watchClipboard",
label: "剪贴板/文件监控",
@ -543,6 +883,7 @@ export const windowsCommands = {
},
],
},
// 进程
{
value: "quickcomposer.windows.process.listProcesses",
label: "进程管理",
@ -610,6 +951,7 @@ export const windowsCommands = {
},
],
},
// 注册表
{
value: "quickcomposer.windows.registry.listKeys",
label: "注册表管理",
@ -748,6 +1090,7 @@ export const windowsCommands = {
},
],
},
// 服务
{
value: "quickcomposer.windows.service.listServices",
label: "服务管理",
@ -794,6 +1137,7 @@ export const windowsCommands = {
},
],
},
// 软件
{
value: "quickcomposer.windows.software.listSoftware",
label: "软件管理",
@ -826,6 +1170,7 @@ export const windowsCommands = {
},
],
},
// 系统工具
{
value: "quickcomposer.windows.utils.setWallpaper",
label: "系统工具",
@ -1061,175 +1406,5 @@ export const windowsCommands = {
},
],
},
{
value: "quickcomposer.windows.automation.inspectElement",
label: "UI自动化",
desc: "Windows界面自动化操作",
icon: "smart_button",
isAsync: true,
config: [],
subCommands: [
{
value: "quickcomposer.windows.automation.inspectElement",
label: "检查元素",
icon: "search",
},
{
value: "quickcomposer.windows.automation.listElements",
label: "列出元素",
icon: "list",
config: [
...searchWindowConfig,
{
component: "OptionEditor",
icon: "settings",
width: 12,
options: {
scope: {
label: "范围",
component: "q-select",
icon: "account_tree",
width: 3,
options: [
{ label: "子元素", value: "children" },
{ label: "所有后代", value: "descendants" },
{ label: "整个子树", value: "subtree" },
],
},
filter: {
label: "过滤条件",
component: "VariableInput",
icon: "filter_alt",
width: 9,
placeholder: "可选,按名称/类名/ControlType/AutomationId过滤",
},
},
defaultValue: {
scope: "children",
},
},
],
},
{
value: "quickcomposer.windows.automation.clickElement",
label: "点击元素",
icon: "mouse",
config: [
...searchWindowConfig,
{
key: "by",
label: "元素查找方式",
component: "q-select",
icon: "search",
width: 3,
options: [
{ label: "名称", value: "name" },
{ label: "类名", value: "class" },
{ label: "类型", value: "type" },
{ label: "AutomationId", value: "automationid" },
],
defaultValue: "name",
},
{
key: "value",
label: "查找值",
component: "VariableInput",
icon: "text_fields",
width: 9,
placeholder: "要点击的元素值",
},
{
key: "pattern",
label: "点击模式",
component: "ButtonGroup",
icon: "touch_app",
width: 12,
options: [
{ label: "普通点击", value: "invoke" },
{ label: "切换状态", value: "toggle" },
],
defaultValue: "invoke",
},
{
key: "background",
label: "后台操作",
component: "CheckButton",
icon: "back_hand",
width: 12,
},
],
},
{
value: "quickcomposer.windows.automation.setElementValue",
label: "设置值",
icon: "edit",
config: [
...searchWindowConfig,
{
key: "by",
label: "查找方式",
component: "q-select",
icon: "search",
width: 4,
options: [
{ label: "名称", value: "name" },
{ label: "类名", value: "class" },
{ label: "类型", value: "type" },
{ label: "AutomationId", value: "automationid" },
],
defaultValue: "name",
},
{
key: "value",
label: "查找值",
component: "VariableInput",
icon: "text_fields",
width: 8,
placeholder: "要设置值的元素",
},
{
key: "newValue",
label: "新值",
component: "VariableInput",
icon: "edit",
width: 12,
placeholder: "要设置的新值",
},
],
},
{
value: "quickcomposer.windows.automation.getElementValue",
label: "获取值",
icon: "content_paste",
outputVariable: "elementValue",
saveOutput: true,
config: [
...searchWindowConfig,
{
key: "by",
label: "查找方式",
component: "q-select",
icon: "search",
width: 4,
options: [
{ label: "名称", value: "name" },
{ label: "类名", value: "class" },
{ label: "类型", value: "type" },
{ label: "AutomationId", value: "automationid" },
],
defaultValue: "name",
},
{
key: "value",
label: "查找值",
component: "VariableInput",
icon: "text_fields",
width: 8,
placeholder: "要获取值的元素",
},
],
},
],
},
],
};

View File

@ -211,6 +211,20 @@ const isPathMatched = (path, patterns) => {
return includeMatch && !excludeMatch;
};
/**
* 递归获取完整的成员访问路径
* @param {Object} node 节点
* @returns {string} 完整的成员访问路径
*/
const getMemberPath = (node) => {
if (node.type === "Identifier") {
return node.name;
} else if (node.type === "MemberExpression") {
return `${getMemberPath(node.object)}.${node.property.name}`;
}
return "";
};
/**
* 解析函数调用字符串返回函数名和参数
* @param {string} functionStr 要解析的函数字符串
@ -267,15 +281,6 @@ export const parseFunction = (functionStr, options = {}) => {
// 处理函数名,支持成员方法调用
let name;
if (callExpression.callee.type === "MemberExpression") {
// 递归获取完整的成员访问路径
const getMemberPath = (node) => {
if (node.type === "Identifier") {
return node.name;
} else if (node.type === "MemberExpression") {
return `${getMemberPath(node.object)}.${node.property.name}`;
}
return "";
};
name = getMemberPath(callExpression.callee);
} else {
name = callExpression.callee.name;
@ -289,7 +294,6 @@ export const parseFunction = (functionStr, options = {}) => {
currentPath &&
options.variableFormatPaths?.length > 0 &&
isPathMatched(currentPath, options.variableFormatPaths);
switch (node.type) {
case "StringLiteral":
// 字符串字面量总是带引号的
@ -349,7 +353,10 @@ export const parseFunction = (functionStr, options = {}) => {
return processNode(node.value, currentPath);
case "MemberExpression":
// 处理成员表达式
return getMemberPath(node);
const memberPath = functionStr.slice(node.start, node.end);
return shouldUseVariableFormat
? newVarInputVal("var", memberPath)
: getMemberPath(node);
default:
console.warn("Unhandled node type:", node.type);
return null;