各种调整

This commit is contained in:
fofolee
2024-12-27 23:35:09 +08:00
parent cdfb2b502f
commit e8d12b95d4
25 changed files with 2454 additions and 1274 deletions

View File

@@ -0,0 +1,150 @@
// JavaScript 关键字和保留字列表
const reservedWords = [
// ES6+ JavaScript 语言关键字
"break",
"case",
"catch",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"else",
"export",
"extends",
"finally",
"for",
"function",
"if",
"import",
"in",
"instanceof",
"new",
"return",
"super",
"switch",
"this",
"throw",
"try",
"typeof",
"var",
"void",
"while",
"with",
"yield",
"let",
"static",
"await",
"enum",
// 严格模式下的额外保留字
"implements",
"interface",
"package",
"private",
"protected",
"public",
// 历史遗留的保留字(可能在未来版本中使用)
"abstract",
"boolean",
"byte",
"char",
"double",
"final",
"float",
"goto",
"int",
"long",
"native",
"short",
"synchronized",
"throws",
"transient",
"volatile",
// JavaScript 内置的特殊值
"null",
"true",
"false",
"undefined",
"NaN",
"Infinity",
// 常用的全局对象和构造函数
"Array",
"Boolean",
"Date",
"Error",
"Function",
"JSON",
"Math",
"Number",
"Object",
"RegExp",
"String",
"Promise",
"Proxy",
"Map",
"Set",
"Symbol",
"BigInt",
// 浏览器和 Node.js 环境的全局对象
"window",
"document",
"console",
"global",
"process",
"globalThis",
// 特殊的内置标识符
"arguments",
"eval",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toLocaleString",
"toString",
"valueOf",
];
/**
* 检查变量名是否合法
* @param {string} name - 要检查的变量名
* @returns {object} - 包含验证结果和错误信息的对象
*/
export function validateVariableName(name) {
// 检查是否为空
if (!name) {
return {
isValid: false,
error: "变量名不能为空",
};
}
// 检查是否是保留字
if (reservedWords.includes(name)) {
return {
isValid: false,
error: `"${name}" 是 JavaScript 保留字,不能用作变量名`,
};
}
// 检查变量名格式是否合法
const validNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;
if (!validNameRegex.test(name)) {
return {
isValid: false,
error:
"变量名必须以字母、下划线或 $ 开头,只能包含字母、数字、下划线和 $",
};
}
return {
isValid: true,
error: null,
};
}

View File

@@ -109,6 +109,7 @@ export const commandCategories = [
value: "quickcommand.sleep",
label: "添加延时",
desc: "延迟的毫秒数",
inputType: "number",
},
],
},

View File

@@ -10,29 +10,33 @@ export function generateUBrowserCode(configs, selectedActions) {
let code = "utools.ubrowser";
// 基础参数
if (configs.useragent.value) {
code += `\n .useragent('${configs.useragent.value}')`;
}
// if (configs.useragent.value) {
// code += `\n .useragent('${configs.useragent.value}')`;
// }
if (configs.goto.url) {
const gotoOptions = {};
if (configs.goto.headers.Referer) {
gotoOptions.headers = gotoOptions.headers || {};
gotoOptions.headers.Referer = configs.goto.headers.Referer;
}
if (configs.goto.headers.userAgent) {
gotoOptions.headers = gotoOptions.headers || {};
gotoOptions.headers["User-Agent"] = configs.goto.headers.userAgent;
}
if (configs.goto.timeout !== 60000) {
gotoOptions.timeout = configs.goto.timeout;
let gotoOptionsStr = `\n .goto(\n`;
gotoOptionsStr += `${configs.goto.url}`;
if (configs.goto.headers.Referer || configs.goto.headers.userAgent) {
gotoOptionsStr += ",\n{";
if (configs.goto.headers.Referer) {
gotoOptionsStr += `\nReferer: ${configs.goto.headers.Referer}`;
}
if (configs.goto.headers.userAgent) {
gotoOptionsStr += `${
configs.goto.headers.Referer ? "," : ""
}\nuserAgent: ${configs.goto.headers.userAgent}`;
}
gotoOptionsStr += "\n}";
}
code += `\n .goto('${configs.goto.url}'${
Object.keys(gotoOptions).length
? `,\n${JSON.stringify(gotoOptions, null, 2).replace(/\n/g, "\n ")}`
: ""
})`;
if (configs.goto.timeout !== 60000) {
gotoOptionsStr += `,\n${configs.goto.timeout}`;
}
gotoOptionsStr += "\n)";
code += gotoOptionsStr;
}
// 浏览器操作
@@ -43,7 +47,7 @@ export function generateUBrowserCode(configs, selectedActions) {
if (config.type === "time" && config.time) {
code += `\n .wait(${config.time})`;
} else if (config.type === "selector" && config.selector) {
code += `\n .wait('${config.selector}'${
code += `\n .wait(${config.selector}${
config.timeout !== 60000 ? `, ${config.timeout}` : ""
})`;
} else if (config.type === "function" && config.function) {
@@ -66,13 +70,13 @@ export function generateUBrowserCode(configs, selectedActions) {
case "click":
if (config.selector) {
code += `\n .click('${config.selector}')`;
code += `\n .click(${config.selector})`;
}
break;
case "css":
if (config.value) {
code += `\n .css('${config.value}')`;
code += `\n .css(${config.value})`;
}
break;
@@ -81,32 +85,31 @@ export function generateUBrowserCode(configs, selectedActions) {
const modifiers = config.modifiers.length
? `, ${JSON.stringify(config.modifiers)}`
: "";
code += `\n .press('${config.key}'${modifiers})`;
code += `\n .press(${config.key}${modifiers})`;
}
break;
case "paste":
if (config.text) {
code += `\n .paste('${config.text}')`;
code += `\n .paste(${config.text})`;
}
break;
case "screenshot":
if (config.selector || config.savePath) {
const options = {};
if (config.selector) options.selector = config.selector;
if (config.rect.width && config.rect.height) {
options.rect = config.rect;
}
code += `\n .screenshot('${config.savePath}'${
Object.keys(options).length ? `, ${JSON.stringify(options)}` : ""
if (config.selector) {
code += `\n .screenshot(${config.selector}${
config.savePath ? `, '${config.savePath}'` : ""
})`;
} else if (config.rect) {
code += `\n .screenshot(${JSON.stringify(config.rect)}${
config.savePath ? `, ${config.savePath}` : ""
})`;
}
break;
case "pdf":
if (config.savePath) {
code += `\n .pdf('${config.savePath}'${
code += `\n .pdf(${config.savePath}${
config.options ? `, ${JSON.stringify(config.options)}` : ""
})`;
}
@@ -114,39 +117,56 @@ export function generateUBrowserCode(configs, selectedActions) {
case "device":
if (config.type === "preset" && config.deviceName) {
code += `\n .device('${config.deviceName}')`;
code += `\n .device(${config.deviceName})`;
} else if (config.type === "custom") {
const options = {
size: config.size,
};
if (config.useragent) options.useragent = config.useragent;
code += `\n .device(${JSON.stringify(options, null, 2).replace(
/\n/g,
"\n "
)})`;
let deviceOptionsStr = `\n .device(\n{`;
if (config.size) {
deviceOptionsStr += `\nsize: ${JSON.stringify(config.size)}`;
}
if (config.useragent) {
deviceOptionsStr += `${config.size ? "," : ""}\nuserAgent: ${
config.useragent
}`;
}
deviceOptionsStr += "\n}";
code += deviceOptionsStr + "\n)";
}
break;
case "cookies":
if (config.name) {
code += `\n .cookies('${config.name}')`;
code += `\n .cookies(${config.name})`;
} else {
code += `\n .cookies()`;
}
break;
case "setCookies":
if (config.items?.length) {
code += `\n .setCookies(${JSON.stringify(config.items)})`;
let cookiesStr = `\n .setCookies([\n`;
config.items.forEach((item, index) => {
cookiesStr += " {";
if (item.name) cookiesStr += `\n name: ${item.name}`;
if (item.value)
cookiesStr += `${item.name ? "," : ""}\n value: ${
item.value
}}`;
if (index < config.items.length - 1) cookiesStr += ",";
cookiesStr += "\n";
});
cookiesStr += " ])";
code += cookiesStr;
}
break;
case "removeCookies":
if (config.name) {
code += `\n .removeCookies('${config.name}')`;
code += `\n .removeCookies(${config.name})`;
}
break;
case "clearCookies":
code += `\n .clearCookies(${config.url ? `'${config.url}'` : ""})`;
code += `\n .clearCookies(${config.url || ""})`;
break;
case "evaluate":
@@ -168,34 +188,38 @@ export function generateUBrowserCode(configs, selectedActions) {
case "when":
if (config.condition) {
code += `\n .when('${config.condition}')`;
code += `\n .when(${config.condition})`;
}
break;
case "mousedown":
case "mouseup":
if (config.selector) {
code += `\n .${action.value}('${config.selector}')`;
code += `\n .${action.value}(${config.selector})`;
}
break;
case "file":
if (config.selector && config.files?.length) {
code += `\n .file('${config.selector}', ${JSON.stringify(
config.files
)})`;
let filesStr = `\n .file(${config.selector}, [\n`;
config.files.forEach((file, index) => {
filesStr += ` ${file}`;
if (index < config.files.length - 1) filesStr += ",\n";
});
filesStr += "\n ])";
code += filesStr;
}
break;
case "value":
if (config.selector) {
code += `\n .value('${config.selector}', '${config.value}')`;
code += `\n .value(${config.selector}, ${config.value})`;
}
break;
case "check":
if (config.selector) {
code += `\n .check('${config.selector}'${
code += `\n .check(${config.selector}${
config.checked !== undefined ? `, ${config.checked}` : ""
})`;
}
@@ -203,13 +227,13 @@ export function generateUBrowserCode(configs, selectedActions) {
case "focus":
if (config.selector) {
code += `\n .focus('${config.selector}')`;
code += `\n .focus(${config.selector})`;
}
break;
case "scroll":
if (config.type === "element" && config.selector) {
code += `\n .scroll('${config.selector}')`;
code += `\n .scroll(${config.selector})`;
} else if (config.type === "position") {
if (config.x !== undefined && config.y !== undefined) {
code += `\n .scroll(${config.x}, ${config.y})`;
@@ -221,8 +245,8 @@ export function generateUBrowserCode(configs, selectedActions) {
case "download":
if (config.url) {
code += `\n .download('${config.url}'${
config.savePath ? `, '${config.savePath}'` : ""
code += `\n .download(${config.url}${
config.savePath ? `, ${config.savePath}` : ""
})`;
}
break;
@@ -234,7 +258,7 @@ export function generateUBrowserCode(configs, selectedActions) {
case "devTools":
if (config.mode) {
code += `\n .devTools('${config.mode}')`;
code += `\n .devTools(${config.mode})`;
} else {
code += `\n .devTools()`;
}

View File

@@ -1,6 +1,7 @@
// ubrowser 浏览器操作配置
export const ubrowserOperationConfigs = {
wait: {
export const ubrowserOperationConfigs = [
{
value: "wait",
label: "等待",
config: [
{
@@ -56,7 +57,8 @@ export const ubrowserOperationConfigs = {
],
icon: "timer",
},
click: {
{
value: "click",
label: "点击",
config: [
{
@@ -68,7 +70,8 @@ export const ubrowserOperationConfigs = {
],
icon: "mouse",
},
css: {
{
value: "css",
label: "注入CSS",
config: [
{
@@ -80,7 +83,8 @@ export const ubrowserOperationConfigs = {
],
icon: "style",
},
press: {
{
value: "press",
label: "按键",
config: [
{
@@ -106,7 +110,8 @@ export const ubrowserOperationConfigs = {
],
icon: "keyboard",
},
paste: {
{
value: "paste",
label: "粘贴",
config: [
{
@@ -118,7 +123,8 @@ export const ubrowserOperationConfigs = {
],
icon: "content_paste",
},
viewport: {
{
value: "viewport",
label: "视窗",
config: [
{
@@ -140,7 +146,8 @@ export const ubrowserOperationConfigs = {
],
icon: "crop",
},
screenshot: {
{
value: "screenshot",
label: "截图",
config: [
{ key: "selector", label: "元素选择器", icon: "crop", type: "input" },
@@ -180,7 +187,8 @@ export const ubrowserOperationConfigs = {
],
icon: "picture_as_pdf",
},
pdf: {
{
value: "pdf",
label: "导出PDF",
config: [
{
@@ -205,7 +213,8 @@ export const ubrowserOperationConfigs = {
],
icon: "devices",
},
device: {
{
value: "device",
label: "模拟设备",
config: [
{
@@ -245,26 +254,44 @@ export const ubrowserOperationConfigs = {
showValue: "custom",
},
],
icon: "phone_iphone",
},
{
value: "cookies",
label: "获取Cookie",
config: [
{
key: "name",
label: "Cookie名称",
icon: "cookie",
type: "input",
width: 12,
},
],
icon: "cookie",
},
setCookies: {
{
value: "setCookies",
label: "设置Cookie",
config: [{ key: "items", label: "Cookie列表", type: "cookie-list" }],
icon: "cookie",
},
removeCookies: {
{
value: "removeCookies",
label: "删除Cookie",
config: [
{ key: "name", label: "Cookie名称", icon: "cookie", type: "input" },
],
icon: "cookie",
},
clearCookies: {
{
value: "clearCookies",
label: "清空Cookie",
config: [{ key: "url", label: "URL(可选)", icon: "link", type: "input" }],
icon: "cookie",
},
evaluate: {
{
value: "evaluate",
label: "执行代码",
config: [
{
@@ -277,7 +304,8 @@ export const ubrowserOperationConfigs = {
],
icon: "code",
},
when: {
{
value: "when",
label: "条件判断",
config: [
{
@@ -322,12 +350,14 @@ export const ubrowserOperationConfigs = {
],
icon: "rule",
},
end: {
{
value: "end",
label: "结束条件",
config: [],
icon: "stop",
},
mousedown: {
{
value: "mousedown",
label: "按下鼠标",
config: [
{
@@ -339,7 +369,8 @@ export const ubrowserOperationConfigs = {
],
icon: "mouse",
},
mouseup: {
{
value: "mouseup",
label: "释放鼠标",
config: [
{
@@ -351,7 +382,8 @@ export const ubrowserOperationConfigs = {
],
icon: "mouse",
},
file: {
{
value: "file",
label: "上传文件",
config: [
{
@@ -364,7 +396,8 @@ export const ubrowserOperationConfigs = {
],
icon: "upload_file",
},
value: {
{
value: "value",
label: "设置值",
config: [
{
@@ -384,7 +417,8 @@ export const ubrowserOperationConfigs = {
],
icon: "check_box",
},
check: {
{
value: "check",
label: "设置选中",
config: [
{
@@ -404,7 +438,8 @@ export const ubrowserOperationConfigs = {
],
icon: "center_focus_strong",
},
focus: {
{
value: "focus",
label: "聚焦元素",
config: [
{
@@ -416,7 +451,8 @@ export const ubrowserOperationConfigs = {
],
icon: "swap_vert",
},
scroll: {
{
value: "scroll",
label: "滚动",
config: [
{
@@ -461,7 +497,8 @@ export const ubrowserOperationConfigs = {
],
icon: "download",
},
download: {
{
value: "download",
label: "下载",
config: [
{
@@ -481,7 +518,8 @@ export const ubrowserOperationConfigs = {
],
icon: "download",
},
devTools: {
{
value: "devTools",
label: "开发工具",
config: [
{
@@ -499,7 +537,19 @@ export const ubrowserOperationConfigs = {
],
icon: "developer_board",
},
};
{
value: "hide",
label: "隐藏",
config: [],
icon: "visibility_off",
},
{
value: "show",
label: "显示",
config: [],
icon: "visibility",
},
];
// 添加默认运行配置
const defaultUBrowserRunConfigs = {