I don't understand GM.setValue and GM.getValue

2,503 views
Skip to first unread message

Stephan Hradek

unread,
Feb 17, 2018, 1:28:38 AM2/17/18
to greasemonkey-users
I'm trying https://www.greasespot.net/2017/09/greasemonkey-4-for-script-authors.html

The issues I face are:
  1. It only seems to works inside the async function. Is this intended?
  2. It doesn't seem to work cross-domains, which I thought was one of the purposes of GM_setValue, Was this dropped for GM.setValue?

I'm using FF 52.0.1 (64Bit) on MacOS 10.13.3



Stephan Hradek

unread,
Feb 17, 2018, 8:36:29 AM2/17/18
to greasemonkey-users


Am Samstag, 17. Februar 2018 07:28:38 UTC+1 schrieb Stephan Hradek:
2. It doesn't seem to work cross-domains, which I thought was one of the purposes of GM_setValue, Was this dropped for GM.setValue?

I think I understood. The values are per-script. So if I want to share a value across sites, I have to use the same script.

Stephan Hradek

unread,
Feb 19, 2018, 2:22:45 AM2/19/18
to greasemonkey-users
Hi!

I think, I understood it now. Maybe someone can confirm or correct?


Am Samstag, 17. Februar 2018 07:28:38 UTC+1 schrieb Stephan Hradek:
I'm trying https://www.greasespot.net/2017/09/greasemonkey-4-for-script-authors.html

The issues I face are:
It only seems to works inside the async function. Is this intended?

Seems this is correct.

So the "boilerplate" layout of a script would be something like
// ==UserScript==
// @name     …
// @grant    GM.getValue
// @grant    GM.setValue
// ==/UserScript==

// Maybe some initialization code here

(async function() {
  let value
= await GM.getValue('myvariable' /*, a default value */);
 
// do the stuff the script needs to do

})();

// usually nothing more here


It doesn't seem to work cross-domains, which I thought was one of the purposes of GM_setValue, Was this dropped for GM.setValue?

 As noted in a previous post I did not understand that persisted variables are per-script. So if you need a variable for more sites, make the script work on all of them.

So a boilerplate for that could be

// ==UserScript==
// @name     …
// @grant    GM.getValue
// @grant    GM.setValue
// @include  https://site1
// @include  https://site2
// @include  https://site3
// ==/UserScript==

// Maybe some initialization code here
var SITE= document.location.host;

(async function() {
  let value
= await GM.getValue('myvariable' /*, a default value */);
  switch(SITE) {
    case "site1":
      // call function for site1
      break;
    case "site2":
      // call function for site2
      break;
    case "site1":
      // call function for site1
      break;
    case "site3":
      // call function for site3
      break;

    default:
      // …
      break
  }

})();

function for_site1() {
}

function for_site2() {
}

function for_site3() {
}

// usually nothing more here

Reply all
Reply to author
Forward
0 new messages