mirror of
https://github.com/fofolee/uTools-quickcommand.git
synced 2025-12-15 07:05:21 +08:00
重构浏览器自动化后端代码结构
This commit is contained in:
@@ -1,5 +1,33 @@
|
||||
const { executeScript } = require("./browser");
|
||||
const fs = require("fs");
|
||||
const { initCDP, cleanupCDP } = require("./cdp");
|
||||
const { searchTarget } = require("./tabs");
|
||||
|
||||
const executeScript = async (tab, script, args = {}) => {
|
||||
const target = await searchTarget(tab);
|
||||
try {
|
||||
const { Runtime } = await initCDP(target.id);
|
||||
const argNames = Object.keys(args);
|
||||
const argValues = Object.values(args).map((v) => JSON.stringify(v));
|
||||
|
||||
const wrappedScript = `
|
||||
(async function(${argNames.join(", ")}) {
|
||||
${script}
|
||||
})(${argValues.join(", ")})
|
||||
`;
|
||||
|
||||
const { result } = await Runtime.evaluate({
|
||||
expression: wrappedScript,
|
||||
returnByValue: true,
|
||||
awaitPromise: true,
|
||||
});
|
||||
|
||||
await cleanupCDP(target.id);
|
||||
return result.value;
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
throw new Error("执行脚本失败");
|
||||
}
|
||||
};
|
||||
|
||||
const clickElement = async (tab, selector) => {
|
||||
return await executeScript(
|
||||
@@ -178,6 +206,7 @@ const injectLocalScript = async (tab, filePath) => {
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
executeScript,
|
||||
clickElement,
|
||||
inputText,
|
||||
submitForm,
|
||||
|
||||
Reference in New Issue
Block a user