统一窗口和控件操作的参数和返回值

This commit is contained in:
fofolee
2025-01-15 19:26:07 +08:00
parent a3dc6479f2
commit fcc8f47127
8 changed files with 734 additions and 560 deletions

View File

@@ -2,13 +2,16 @@ const { runCsharpFeature } = require("../../csharp");
/**
* 键盘操作
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @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 {boolean} 是否成功
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const sendKeys = async function (method, window, keys, options = {}) {
const { control, background = false } = options;
@@ -17,23 +20,33 @@ const sendKeys = async function (method, window, keys, options = {}) {
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return {
success: true,
control: JSON.parse(result),
};
}
} catch (err) {
error = err.toString();
}
return true;
return { success: false, error };
};
/**
* 发送文本
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @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 {boolean} 是否成功
* @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;
@@ -43,32 +56,34 @@ const sendText = async function (method, window, text, options = {}) {
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
}
return true;
return { success: false, error };
};
/**
* 鼠标点击
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} action 动作click/doubleClick/rightClick
* @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 {boolean} 是否成功
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const click = async function (
method,
window,
action = "click",
options = {}
) {
const click = async function (method, window, action = "click", options = {}) {
const { control, text, pos, background = false } = options;
const args = ["-type", "mouse", "-action", action];
@@ -79,17 +94,22 @@ const click = async function (
if (pos) args.push("-pos", pos);
args.push("-background", background.toString());
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
}
return true;
return { success: false, error };
};
/**
* 获取窗口控件树
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题句柄、进程名、类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
@@ -103,17 +123,13 @@ const inspectWindow = async function (method, window, options = {}) {
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
args.push("-background", background.toString());
const result = await runCsharpFeature("sendmessage", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
try {
return JSON.parse(result);
const result = await runCsharpFeature("sendmessage", args);
if (result) return JSON.parse(result);
} catch (error) {
console.error("解析控件树失败:", error);
return null;
console.log(error);
}
return [];
};
module.exports = {