mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-06-29 12:22:44 +08:00
扩展浏览器标签页管理功能:实现创建新标签页和关闭标签页的命令,支持通过URL、标题和ID进行标签页操作
This commit is contained in:
parent
df709d9c72
commit
26360e9643
@ -225,7 +225,7 @@ const executeScript = async (script, args = {}) => {
|
|||||||
const argValues = Object.values(args).map((v) => JSON.stringify(v));
|
const argValues = Object.values(args).map((v) => JSON.stringify(v));
|
||||||
|
|
||||||
const wrappedScript = `
|
const wrappedScript = `
|
||||||
(function(${argNames.join(", ")}) {
|
(async function(${argNames.join(", ")}) {
|
||||||
${script}
|
${script}
|
||||||
})(${argValues.join(", ")})
|
})(${argValues.join(", ")})
|
||||||
`;
|
`;
|
||||||
|
76
plugin/lib/quickcomposer/browser/tabs.js
Normal file
76
plugin/lib/quickcomposer/browser/tabs.js
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
const CDP = require("chrome-remote-interface");
|
||||||
|
const { executeScript } = require("./browser");
|
||||||
|
|
||||||
|
let client = null;
|
||||||
|
let Page = null;
|
||||||
|
let Runtime = null;
|
||||||
|
let Target = null;
|
||||||
|
|
||||||
|
const initCDP = async (port) => {
|
||||||
|
if (!client) {
|
||||||
|
try {
|
||||||
|
client = await CDP({ port });
|
||||||
|
({ Page, Runtime, Target } = client);
|
||||||
|
await Promise.all([Page.enable(), Runtime.enable()]);
|
||||||
|
} catch (err) {
|
||||||
|
console.log(err);
|
||||||
|
throw new Error(`请先通过浏览器控制中的"启动浏览器"打开浏览器`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return { Page, Runtime, Target };
|
||||||
|
};
|
||||||
|
|
||||||
|
// 获取所有标签页
|
||||||
|
const getTabs = async () => {
|
||||||
|
const targets = await CDP.List();
|
||||||
|
return targets
|
||||||
|
.filter((target) => target.type === "page")
|
||||||
|
.map((target) => ({
|
||||||
|
url: target.url,
|
||||||
|
title: target.title,
|
||||||
|
id: target.id,
|
||||||
|
}));
|
||||||
|
};
|
||||||
|
|
||||||
|
const searchTarget = async (searchProperty, searchValue) => {
|
||||||
|
const targets = await CDP.List();
|
||||||
|
const target = targets.find((target) =>
|
||||||
|
target[searchProperty].includes(searchValue)
|
||||||
|
);
|
||||||
|
if (!target) {
|
||||||
|
throw new Error(`未找到目标: ${searchProperty} = ${searchValue}`);
|
||||||
|
}
|
||||||
|
return target;
|
||||||
|
};
|
||||||
|
|
||||||
|
// 激活指定标签页
|
||||||
|
const activateTab = async (searchProperty, searchValue) => {
|
||||||
|
const target = await searchTarget(searchProperty, searchValue);
|
||||||
|
await CDP.Activate({ id: target.id });
|
||||||
|
};
|
||||||
|
|
||||||
|
// 创建新标签页
|
||||||
|
const createNewTab = async (url = "about:blank") => {
|
||||||
|
const { Target } = await initCDP();
|
||||||
|
const { targetId } = await Target.createTarget({ url });
|
||||||
|
const { targetInfo } = await Target.getTargetInfo({ targetId });
|
||||||
|
return {
|
||||||
|
url: targetInfo.url,
|
||||||
|
title: targetInfo.title,
|
||||||
|
id: targetId,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
// 关闭标签页
|
||||||
|
const closeTab = async (searchProperty, searchValue) => {
|
||||||
|
const target = await searchTarget(searchProperty, searchValue);
|
||||||
|
await CDP.Close({ id: target.id });
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getTabs,
|
||||||
|
activateTab,
|
||||||
|
createNewTab,
|
||||||
|
closeTab,
|
||||||
|
};
|
@ -137,6 +137,45 @@ export const browserCommands = {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
value: "quickcomposer.browser.createNewTab",
|
||||||
|
label: "创建新标签页",
|
||||||
|
icon: "tab",
|
||||||
|
config: [
|
||||||
|
{
|
||||||
|
label: "网址",
|
||||||
|
component: "VariableInput",
|
||||||
|
icon: "link",
|
||||||
|
width: 12,
|
||||||
|
placeholder: "留空则打开about:blank",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
value: "quickcomposer.browser.closeTab",
|
||||||
|
label: "关闭标签页",
|
||||||
|
icon: "tab",
|
||||||
|
config: [
|
||||||
|
{
|
||||||
|
component: "QSelect",
|
||||||
|
icon: "tab",
|
||||||
|
width: 3,
|
||||||
|
options: [
|
||||||
|
{ label: "通过URL", value: "url" },
|
||||||
|
{ label: "通过标题", value: "title" },
|
||||||
|
{ label: "通过ID", value: "id" },
|
||||||
|
],
|
||||||
|
defaultValue: "url",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
label: "搜索条件",
|
||||||
|
component: "VariableInput",
|
||||||
|
icon: "tab",
|
||||||
|
width: 9,
|
||||||
|
placeholder: "支持模糊匹配",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user