Why?
There exist other user script engines which have already done the work of bridging the gap. Greasemonkey has elected to move exclusively towards a more performant asynchronous model. Eventually in the future, such scripts will be faster.What?
The Greasespot Wiki has been updated to explain Greasemonkey 4 in detail. Here's a quick summary.First, there is only an embedded editor. Browser Extensions have no access to the file system, so you can no longer author user scripts in your familiar text editor.
There is only one object provided to user scripts now, named GM. It has several properties. One of them is info – the equivalent of the old GM_info. There are also several methods of this object: getResourceUrl, deleteValue/getValue/listValues/setValue, xmlHttpRequest.
To use these methods you still need @grant, and use the new name, e.g.:
// @grant GM.setValueThe new form has a dot, where the old form has an underscore. You may specify both @grants, if you'd like to be compatible with Greasemonkey 4 and other user script engines at the same time. As of today, there is no support for: GM_log (use console.log), GM_addStyle, GM_registerMenuCommand, nor GM_getResourceText.
In general these methods work like their old counterparts, but their return values are Promises. The async and await keywords make asynchronous promises easy to work with. For example:
// ==UserScript==Here the GM.getValue() method actually returns a promise, but the await keyword transparently converts that to its resolved value, allowing us to write code just as if the value was directly returned – with neither callbacks nor promise resolution. See the documentation on async and await.
// @name GM set/get demo
// @grant GM.getValue
// @grant GM.setValue
// ==/UserScript==
(async function() {
console.log('Starting the get/set demo ...');
let i = await GM.getValue('i', 0);
console.log(`This time, i was ${i}.`);
GM.setValue('i', i+1);
})();
If you'd like your script to be compatible with Greasemonkey 4 and also Greasemonkey 3 (or other user script engines), we have provided a polyfill, which makes new-style API calls work on top of older engines. To use it: 1) keep your old-API @grant line, 2) add a new-API @grant line, 4) require the polyfill script, 4) switch your code to use new-API style (and probably async/await). So the above example might look like:
// ==UserScript==With the exact same new-API style code as above. Such a script can be installed in either Greasemonkey 4 or Greasemonkey 3 (or TamperMonkey, or ViolentMonkey, etc.) and work as intended.
// @name GM set/get demo
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js// @grant GM.getValue
// @grant GM_getValue// @grant GM.setValue
// @grant GM_setValue
// ==/UserScript==
...
4 comments:
Will GM_openInTab be supported in version 4? I use this feature to allow my script to open pages in new tabs in the background.
For those of us who support older browsers (ie pre-FF52), it'd be really nice if GM.getResourceText were supported, as eval(GM.getResourceText(...)) would be an easy way to load Greasemonkey-4 versions of scripts.
GM.getResourceText: https://github.com/greasemonkey/greasemonkey/issues/2548
> As of today, there is no support for: GM_log (use console.log),
> GM_addStyle, GM_registerMenuCommand, nor GM_getResourceText.
"As of today", does that means not planned/possible - or just not implemented yet?
Regarding GM_registerMenuCommand, doesn't it sound like it is possible to implement it (or something very similar) with WebExtensions menus API?
Post a Comment