feat: add await detection and friendly prompt (v0.32)
- Detect await usage in sync context - Auto-wrap with async function - Return friendly prompt suggesting to avoid await - Provide alternative solution using global variables
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
// ==UserScript==
|
||||
// @name ljq_web_driver
|
||||
// @namespace http://tampermonkey.net/
|
||||
// @version 0.31
|
||||
// @version 0.32
|
||||
// @description Execute JS via ljq_web_driver
|
||||
// @require https://code.jquery.com/jquery-3.6.0.min.js
|
||||
// @author You
|
||||
@@ -310,6 +310,9 @@
|
||||
} catch (e) {
|
||||
if (isIllegalReturnError(e)) {
|
||||
result = (new Function(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;
|
||||
}
|
||||
}
|
||||
@@ -336,6 +339,13 @@
|
||||
);
|
||||
}
|
||||
|
||||
function isAwaitError(e) {
|
||||
return e instanceof SyntaxError && (
|
||||
/await is only valid in async/i.test(e.message) || // Chrome
|
||||
/await.*async/i.test(e.message) // Firefox等
|
||||
);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
ws = new WebSocket(wsUrl);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user