Script is ignored when using await

190 views
Skip to first unread message

Carsten Klotz von Hoff

unread,
Dec 31, 2017, 6:10:27 AM12/31/17
to greasemonkey-dev

var pageTodoList = {
    listName
: 'pageTodoList',
    getList
: function () {
       
var data = await GM.getValue(this.listName);
       
if (data instanceof Array) {
            data
= JSON.parse(data);
       
} else {
            data
= [];
       
}

       
return data;
   
},
    setList
: function (list) {
        await GM
.setValue(this.listName, JSON.stringify(list));
   
}
};

With this code the GM script is completely ignored. If I remove the await the scripts runs, but not as intended in GM 3.x. This is a blocker for me atm and I switched back to 3.17. How to solve in 4.1?

Carsten Klotz von Hoff

unread,
Dec 31, 2017, 6:12:15 AM12/31/17
to greasemonkey-dev
Seems like I'm not the only on with this problem: https://stackoverflow.com/a/47476445

LWChris

unread,
Dec 31, 2017, 6:36:48 AM12/31/17
to greasemo...@googlegroups.com

Await only works in async functions. So you have to make the declaration an async function expression:


setList: async function (list) { ... }


Remember that the calls to setList(foo) now have to become "await setList(foo)", too, which in turn only works within async functions. Basically the async/await pattern "bubbles up" the whole stacktrace.

Same goes for getList, of course.
--
You received this message because you are subscribed to the Google Groups "greasemonkey-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to greasemonkey-d...@googlegroups.com.
To post to this group, send email to greasemo...@googlegroups.com.
Visit this group at https://groups.google.com/group/greasemonkey-dev.
For more options, visit https://groups.google.com/d/optout.

Carsten Klotz von Hoff

unread,
Dec 31, 2017, 11:10:57 AM12/31/17
to greasemonkey-dev
So I have to declare almost every function as async. It seems this BC break was not the best idea. But may be the reason why so much GM scripts do not work with 4.x. A fork that makes GM 3.7 compatible to Firefox Quantum would maybe be a better idea. Or what is about Tampermonkey? Is this an alternative? I have a mixture of own and downloaded scripts. The most have several hundred line of code.

LWChris

unread,
Jan 1, 2018, 7:57:39 AM1/1/18
to greasemo...@googlegroups.com
Hello, answers inline.


Am 31.12.2017 um 17:10 schrieb Carsten Klotz von Hoff:
So I have to declare almost every function as async.

Yes, every function upwards the call chain. It doesn't change anything for you though (except that you have to type the keywords). The await keyword automatically unwraps the Promise object. Don't be afraid, it's easy.

It seems this BC break was not the best idea.

It's not like he had a real choice there. The WebExtension API does not include several functionalities that were used in GM 3.7, so the changes would've been breaking ones anyway.
That being said, this was a rare opportunity to fix many quirks and introduce larger changes, like fixing weird naming or introducing the asnychronous API calls.

Many scripts will most likely need to be modified anyway...


But may be the reason why so much GM scripts do not work with 4.x.

Definitely. Many people haven't cared to update their scripts in advance (using polyfill), in spite of the announcement about the breaking changes as of September 20th, 2017.


A fork that makes GM 3.7 compatible to Firefox Quantum would maybe be a better idea. Or what is about Tampermonkey? Is this an alternative?

Making GM 3.7 work in Quantum is impossible due to the changes in the WebExtensions API. You can always make something that uses the a functional subset of an API (WE) work on the full API (XPCOM/XUL),
but obviously not the other way round if it used functionality that is not in the subset (like GM 3.7 does).

For making GM 4.0 scripts (i. e. ones that use asynchronous calls like await GM.setValue) work on GM 3.7 and Tampermonkey etc., there is a polyfill script. For more information, see

https://www.greasespot.net/2017/09/greasemonkey-4-for-script-authors.html

In a nutshell, the polyfill script adds the GM[dot] promise functions, and makes them internally call the old API wrapped inside a promise object.
That way you can update the scripts to the new standard of using async and await with the GM[dot] instead of GM[underscore] functions, but they continue to work on the other engines.

After all, the best way it COULD HAVE been is:

After being notified in September, immediately update your scripts to
1) use async / await
2) include both grant statements like GM.setValue and GM_setValue
3) use the polyfill script

That means everyone can update their scripts in advance, but keeping them functional in earlier versions like GM 3.7.
The moment GM 4.0 is rolled out, the script continues to work on the new API and the polyfill does nothing (since it only registers callbacks for undefined functions).

But alas, userscripts are exactly THAT, "user"scripts - and users are update procrastinators ("it works now, why change it now?"). ;)

Reply all
Reply to author
Forward
0 new messages