This is a brainstorming question, looking for ideas.
Is there any method to convert a sync function in an external script to behave like async.
Consider the situation of userscript & userscript manager.
The userscript uses a sync GM_getValue
function and userscript developers are reluctant to update the code to async GM.getValue
API.
The script manger that is processing the script, can not support sync method.
Is there any way to handle the code in an async way?
Example:
function run() {
const a = GM_getValue('one');
}
- Is there a possibility to halt the function by script manager until async response is available?
- Is there a way to parse the script and convert the relevant functions to async/await? (The Regex is error prone)
- Is there a way to override the function and replace it with an async version?
e.g.
async function run() {
const a = await GM_getValue('one');
}
- Any other ideas?
Update example subsequent to comments
// ==UserScript==
// @name My Script
// @match http://www.example.org/*
// @grant GM_getValue
// ==/UserScript==
function run() {
const b = GM_getValue("one");
if (b && b > a) {
// do somthig
}
else {
// do something else
}
return b;
}
const a = 5;
const c = run();
const d = GM_getValue("two");
const e = parseInt(d);
if (d > a) {
// do somthig
}
else {
// do something else
}
function sum(a, b) {
return a + b;
}
const f = sum(2, 5);