I write a small userscript and it was first only for one webpage, now that I added more functions I try to let the script work over multiple sites and as far as I understand I have to go from localStorage
to GM.getValue
, but won't get it to work.
I allready learned that the functions is async and I have to use it. So to get this functions to know better, I wrote a lil' testscript and have still no success - so maybe someone might help me.
I get GM.setValue to work, so if I check in Tampermokey the script-tag I see that I have for this script in storage:
{
"WFP_token": "123456789"
}
and the userscript is
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_getValue
// @grant GM_setValue
// ==/UserScript==
(function() {
'use strict';
/**
* Overall script parts are placed here
*/
console.log('[WFP]: init');
function getToken() {
(async () => {
let token = GM.getValue("WFP_token",-1);
if (token != -1 && token != undefined) {
return token;
} else {
window.setTimeout(getToken(),10)
}
})();
}
const WEBHOOK_TOKEN = getToken();
console.log('[WFP]: '+WEBHOOK_TOKEN);
the console output is:
[WFP]: init
[WFP]: undefined
Can somebody help me and point me where I'm wrong and who I should use GM.getValue
?
use await near GM.getValue
, getToken()
and return a promise in setTimeout
else case
(async function() {
'use strict';
/**
* Overall script parts are placed here
*/
console.log('[WFP]: init');
async function getToken(a) {
let token = await GM.getValue("WFP_token", -1);
if (token != -1 && token != undefined) {
return token;
} else {
return new Promise((resolve) => {
window.setTimeout(() => resolve(getToken()), 10);
})
}
}
const WEBHOOK_TOKEN = await getToken();
console.log('[WFP]: ' + WEBHOOK_TOKEN);
})();