feat(web_driver): support await in executeCode via AsyncFunction fallback
This commit is contained in:
@@ -241,13 +241,13 @@
|
|||||||
url: location.href,
|
url: location.href,
|
||||||
sessionId: sid
|
sessionId: sid
|
||||||
}),
|
}),
|
||||||
onload: function(resp) {
|
onload: async function(resp) {
|
||||||
if (resp.status === 200) {
|
if (resp.status === 200) {
|
||||||
let data = JSON.parse(resp.responseText);
|
let data = JSON.parse(resp.responseText);
|
||||||
console.log(log_prefix + '接收到数据:', data);
|
console.log(log_prefix + '接收到数据:', data);
|
||||||
if (data.id === "" && data.ret === "use ws") return;
|
if (data.id === "" && data.ret === "use ws") return;
|
||||||
if (data.id === "") return setTimeout(connecthttp, 100);
|
if (data.id === "") return setTimeout(connecthttp, 100);
|
||||||
const response = executeCode(data);
|
const response = await executeCode(data);
|
||||||
|
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
handleError(data.id, response.error, '执行代码');
|
handleError(data.id, response.error, '执行代码');
|
||||||
@@ -283,7 +283,7 @@
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function executeCode(data) {
|
async function executeCode(data) {
|
||||||
let id = data.id || 'unknown'; // 获取 ID
|
let id = data.id || 'unknown'; // 获取 ID
|
||||||
let result;
|
let result;
|
||||||
|
|
||||||
@@ -301,34 +301,28 @@
|
|||||||
const jsCode = data.code.trim();
|
const jsCode = data.code.trim();
|
||||||
const lines = jsCode.split(/\r?\n/).filter(l => l.trim());
|
const lines = jsCode.split(/\r?\n/).filter(l => l.trim());
|
||||||
const lastLine = lines.length > 0 ? lines[lines.length - 1].trim() : '';
|
const lastLine = lines.length > 0 ? lines[lines.length - 1].trim() : '';
|
||||||
|
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
|
||||||
|
|
||||||
if (lastLine.startsWith('return')) {
|
if (lastLine.startsWith('return')) {
|
||||||
result = (new Function(jsCode))();
|
result = await (new AsyncFunction(jsCode))();
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
result = eval(jsCode);
|
result = eval(jsCode);
|
||||||
|
if (result instanceof Promise) result = await result;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (isIllegalReturnError(e)) {
|
if (isIllegalReturnError(e) || isAwaitError(e)) {
|
||||||
result = (new Function(jsCode))();
|
result = await (new AsyncFunction(jsCode))();
|
||||||
} else if (isAwaitError(e)) {
|
|
||||||
result = (async function() { return eval(jsCode); })();
|
|
||||||
result = 'Promise is running, cannot get return value. Suggest avoiding await next time, or use global variables (e.g., window.myVar) to store async results.';
|
|
||||||
} else throw e;
|
} else throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const processedResult = smartProcessResult(result);
|
const processedResult = smartProcessResult(result);
|
||||||
if (result instanceof Promise) {
|
|
||||||
result.finally(() => window.open = _open);
|
|
||||||
return { result: processedResult };
|
|
||||||
}
|
|
||||||
return { result: processedResult };
|
return { result: processedResult };
|
||||||
} catch (execError) {
|
} catch (execError) {
|
||||||
return { error: execError };
|
return { error: execError };
|
||||||
} finally {
|
} finally {
|
||||||
if (!(result instanceof Promise)) {
|
|
||||||
setTimeout(() => window.open = _open, 100);
|
setTimeout(() => window.open = _open, 100);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
function isIllegalReturnError(e) {
|
function isIllegalReturnError(e) {
|
||||||
return e instanceof SyntaxError && (
|
return e instanceof SyntaxError && (
|
||||||
@@ -375,7 +369,7 @@
|
|||||||
try {
|
try {
|
||||||
let data = JSON.parse(e.data);
|
let data = JSON.parse(e.data);
|
||||||
ws.send(JSON.stringify({type: 'ack',id: data.id}));
|
ws.send(JSON.stringify({type: 'ack',id: data.id}));
|
||||||
const response = executeCode(data);
|
const response = await executeCode(data);
|
||||||
|
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
handleError(data.id, response.error, '执行代码');
|
handleError(data.id, response.error, '执行代码');
|
||||||
|
|||||||
Reference in New Issue
Block a user