Run JavaScript: Automatically switch to async mode if await is used

This will allow examples from the documentation to work without the
async option being explicitly chosen.
This commit is contained in:
Dan Stillman 2019-01-22 04:44:05 -05:00
parent 39eb2962a6
commit 8f58560751
2 changed files with 15 additions and 1 deletions

View file

@ -17,7 +17,7 @@
Run as async function
</label>
</div>
<textarea id="code"></textarea>
<textarea id="code" oninput="handleInput()"></textarea>
</div>
<div class="textbox-container">

View file

@ -32,6 +32,20 @@ async function run() {
resultTextbox.value = Zotero.Utilities.varDump(result);
}
function handleInput() {
var checkbox = document.getElementById('run-as-async');
var isAsync = checkbox.checked;
if (isAsync) {
return;
}
var code = document.getElementById('code').value;
// If `await` is used, switch to async mode
if (/^\s*await\s/m.test(code)) {
checkbox.checked = true;
update();
}
}
window.addEventListener('keypress', function (event) {
if (Zotero.isMac) {
if (!event.metaKey) {