Diskuse » Development

Converting GM_setValue/GM_getValue to GM.setValue/GM.getValue

§
Posted: 09. 06. 2023

Hello,

I need help converting GM_setValue/GM_getValue to GM.setValue/GM.getValue.

I see that it requires async/await which I don't have experience with yet.

This is the program I need help with: Newspaper.

Thank you

async/await is not a must, but it lets you handle it easier like what you did.

in traditional design, apart from events, the program flow is a -> b -> c -> d

function a(){
// do something
}

function b(){
// do something
}

function c(){
// do something
}

function d(){
// do something
}

a()
b()
c()
d()

So to execute c and d, you must have finish everything in a and b

Here I change the b into async function

async function b(){

}

here b() will become a Promise object.

let resultA = a(); // say 123 let resultB = b(); // promise let resultC = c(); // say 'abc' let resultD = d(); // say undefined

the promise result means that it is async execution. You can just go ahead to execute c and d without waiting b

so you can use .then to do the callback after b completed.

resultB.then(actualResultB=>{
console.log('b is completed', actualResultB)
})

inside async function, you can use await. in normal function, you cannot. when you use await, the execution will jump out for a while and do something else. when that promise is ready (complete), it will go back to that async function (just like .then but you just write like normal function)

async function b(){
let value1 = await GM.getValue('value1',10);
let value2 = await GM.getValue('value2',5);
console.log(value1, value2)
return value1+value2;
}

this is equal to

function b(){
let value1 = GM_getValue('value1',10);
let value2 = GM_getValue('value2',5);
console.log(value1, value2)
return value1+value2;
}

You can totally ignore async await. Just use GM.getValue / setValue with .then


// ...

function b(){

let promise1 = GM.getValue('value1',10);
let promise2 = GM.getValue('value2',5);

let promiseAll = Promise.all([promise1, promise2]);
promiseAll.then(arr=>{

let [value1, value2] = arr;
console.log(value1, value2)


})



}

// ...

a()
b()
c()
d()

You can also make your function b to return a Promise object

// ...

function b(){

return new Promise(resolve=>{

let promise1 = GM.getValue('value1',10);
let promise2 = GM.getValue('value2',5);

let promiseAll = Promise.all([promise1, promise2]);
promiseAll.then(arr=>{

let [value1, value2] = arr;
console.log(value1, value2)

resolve(value1+value2)


})

})



}

// ...

a()
let resultB = b()
c()
d()


resultB.then(actualResultB=>{

console.log('b is completed', actualResultB)

})

§
Posted: 09. 06. 2023
Edited: 09. 06. 2023

For the most simple way to use GM.getValue, you might just write

let waitingResult = Promise.all([
  GM.getValue('value1',10),
  GM.getValue('value2',5)
]);

waitingResult.then(values=>{
  let [value1, value2] = values;
  console.log(value1,value2);

  GM.setValue('value3',value1+value2).then(()=>{
    console.log('value3 is set');
  });

});

or

let waitingResult = Promise.all([
  GM.getValue('value1',10),
  GM.getValue('value2',5)
]);

waitingResult.then(values=>{
  let [value1, value2] = values;
  console.log(value1,value2);

  return GM.setValue('value3',value1+value2);

}).then(()=>{
    console.log('value3 is set');
  });

§
Posted: 09. 06. 2023

Thank you very much!

I'm checking which code to integrate.

NotYouMod
§
Posted: 09. 06. 2023

Just use async directive in IIFE to be able to use await.

(async function() {
  await GM.setValue('hello', 'world')

  const value = await GM.getValue('hello')

  console.log('Value of item in storage with key "hello" equals to:', value)
})()
§
Posted: 09. 06. 2023

Gentlemen, what do you think of this diff?

If you think of a better way, please do let me know.

Thank you kindly for your help!

Post reply

Sign in to post a reply.