优化浏览器命令功能,新增通过URL、标题和ID获取标签的支持,重构标签激活逻辑以支持模糊匹配。更新相关命令为异步执行,简化配置项

This commit is contained in:
fofolee
2025-01-22 21:30:16 +08:00
parent c3ca34bd2a
commit c1e255dd56
2 changed files with 28 additions and 72 deletions

View File

@@ -200,9 +200,7 @@ const initCDP = async (port) => {
await Promise.all([Page.enable(), Runtime.enable(), DOM.enable()]);
} catch (err) {
console.log(err);
throw new Error(
`请先通过浏览器控制中的“启动浏览器”打开浏览器`
);
throw new Error(`请先通过浏览器控制中的"启动浏览器"打开浏览器`);
}
}
return { Page, Runtime, DOM };
@@ -246,16 +244,17 @@ const getTabs = async () => {
.map((target) => ({
url: target.url,
title: target.title,
id: target.id,
}));
};
const activateTab = async (index) => {
const activateTab = async (type, value) => {
const targets = await CDP.List();
const pages = targets.filter((target) => target.type === "page");
if (index > 0 && index <= pages.length) {
const targetId = pages[index - 1].id;
await CDP.Activate({ id: targetId });
const target = targets.find((target) => target[type].includes(value));
if (!target) {
throw new Error(`未找到目标: ${type} = ${value}`);
}
await CDP.Activate({ id: target.id });
};
const clickElement = async (selector) => {