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

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,11 +2,10 @@ 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 {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
* @returns {object[]} 元素列表
*/
const listElements = async function (method, window, options = {}) {
@@ -17,28 +16,29 @@ const listElements = async function (method, window, options = {}) {
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
if (scope) args.push("-scope", scope);
const result = await runCsharpFeature("automation", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
try {
return JSON.parse(result);
} catch (error) {
console.error("解析元素列表失败:", error);
return null;
const result = await runCsharpFeature("automation", args);
console.log(result);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
}
return [];
};
/**
* 点击元素
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} by 查找方式name/class/type/automationid
* @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 {string} options.pattern 点击模式:"invoke"|"toggle"
* @param {boolean} options.background 是否后台操作
* @returns {boolean} 是否成功
* @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;
@@ -48,24 +48,31 @@ const clickElement = async function (method, window, by, value, options = {}) {
if (method !== "active" && window) args.push("-window", window);
if (pattern) args.push("-pattern", pattern);
if (background) args.push("-background");
const result = await runCsharpFeature("automation", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) {
return { success: true, element: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
}
return true;
return { success: true, error };
};
/**
* 设置元素值
* @param {string} method 窗口类型title/handle/active
* @param {string} window 窗口标题/句柄
* @param {string} by 查找方式name/class/type/automationid
* @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 {boolean} 是否成功
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const setElementValue = async function (
method,
@@ -90,19 +97,21 @@ const setElementValue = async function (
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
const result = await runCsharpFeature("automation", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return { success: true, element: 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} by 查找方式name/class/type/automationid
* @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 是否后台操作
@@ -122,38 +131,32 @@ const getElementValue = async function (
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
const result = await runCsharpFeature("automation", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
let error;
try {
return JSON.parse(result);
} catch (error) {
console.error("解析元素值失败:", error);
return null;
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 超时时间(秒)
* @param {boolean} options.background 是否后台操作
* @returns {object} 元素信息
*/
const inspectElement = async function () {
const args = ["-type", "inspect"];
const result = await runCsharpFeature("automation", args);
if (result && result.startsWith("Error:")) {
throw new Error(result.substring(7));
}
try {
return JSON.parse(result);
} catch (error) {
console.error("解析元素信息失败:", error);
return null;
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
}
return [];
};
module.exports = {

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 = {

View File

@@ -1,10 +1,12 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 窗口置顶
* @param {string} method 查找方式title/handle/active
* @param {string} value 查找值handle时为数字
* @param {boolean} isTopMost 是否置顶
* 窗口置顶,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被置顶的窗口信息
*/
async function setTopMost(method, value, isTopMost) {
const args = [
@@ -17,14 +19,29 @@ async function setTopMost(method, value, isTopMost) {
"-value",
isTopMost.toString(),
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 设置窗口透明度,只操作第一个找到的窗口
* @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 = [
@@ -37,17 +54,29 @@ async function setOpacity(method, value, opacity) {
"-value",
opacity.toString(),
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 设置窗口位置和大小,只操作第一个找到的窗口
* @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 = [
@@ -60,14 +89,26 @@ async function setWindowRect(method, value, x, y, width, height) {
"-value",
`${x},${y},${width},${height}`,
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* @param {string} state 状态normal/maximize/minimize
* 设置窗口状态,只操作第一个找到的窗口
* @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 = [
@@ -80,34 +121,70 @@ async function setWindowState(method, value, state) {
"-value",
state,
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 关闭窗口,只操作第一个找到的窗口
* @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];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 设置窗口焦点,只操作第一个找到的窗口
* @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];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 设置窗口边框,只操作第一个找到的窗口
* @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 = [
@@ -120,14 +197,26 @@ async function setBorder(method, value, hasBorder) {
"-value",
hasBorder.toString(),
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* 设置窗口点击穿透,只操作第一个找到的窗口
* @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 = [
@@ -140,43 +229,33 @@ async function setClickThrough(method, value, isTransparent) {
"-value",
isTransparent.toString(),
];
await runCsharpFeature("window", args);
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/active
* @param {string} value 查找值handle时为数字
* @returns {Object} 窗口信息
* 获取窗口信息,返回所有匹配的窗口信息
* @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];
const result = await runCsharpFeature("window", args);
try {
return JSON.parse(result);
} catch (error) {
return {};
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) return JSON.parse(windowInfo);
} catch (err) {
console.log(err);
}
}
/**
* 设置窗口可见性
* @param {string} method 查找方式title/handle/active
* @param {string} value 查找值handle时为数字
* @param {boolean} visible 是否可见
*/
async function setVisible(method, value, visible) {
const args = [
"-type",
"visible",
"-method",
method,
"-window",
value,
"-value",
visible.toString(),
];
await runCsharpFeature("window", args);
return [];
}
module.exports = {
@@ -184,7 +263,6 @@ module.exports = {
setOpacity,
setWindowRect,
setWindowState,
setVisible,
closeWindow,
setFocus,
setBorder,