0

with Firefox 68 and Greasemonkey 4.9, i want to set a value from a script on a webpage and get that same value from another script on another webpage. it seems not working. How can i do that ? Here is what i tried :

script 1

// ==UserScript==
// @name     My EDIT
// @version  1
// @match http://one.local/stuff/edit*
// @grant       GM.setValue
// @grant       GM.getValue
// ==/UserScript==

(async () => {
  let count = await GM.getValue('count', 0);
  await GM.setValue('count', count + 1);
  console.log(count);
})();

script 2

// ==UserScript==
// @name     My VIEW
// @version  1
// @match http://www.two.local/view
// @grant       GM.getValue
// ==/UserScript==

(async () => {
  let count = await GM.getValue('count', 0);
  console.log(count);
})();

even if values are incremented when i visite http://one.local/stuff/edit many times, i can't get those when visiting http://www.two.local/view (it remains 0 !

2
  • 1
    You have two different scripts but storage is per script. Join them into one script and specify both sites in @match, then use location.hostname to decide what part of code to run.
    – wOxxOm
    Jan 27, 2020 at 7:09
  • actually, that worked fine :)
    – izus
    Jan 27, 2020 at 8:51

1 Answer 1

2

Any good script manger should NOT allow script A access script B storage as it would be a serious security breach.

You can combine the scripts into one script, that runs on both pages. That way the storage would be shared.

Simple example:

// ==UserScript==
// @name            Combined
// @version         1
// @match           http://one.local/stuff/edit*
// @match           http://www.two.local/view
// @grant           GM.setValue
// @grant           GM.getValue
// ==/UserScript==

(async() => {
  
  // code for one.local  
  if (location.hostname === 'one.local') {  
    const count = await GM.getValue('count', 0);
    await GM.setValue('count', count + 1);
    console.log(count);
  }
  // code for www.two.local
  else if (location.hostname === 'www.two.local') {
    const count = await GM.getValue('count', 0);
    console.log(count);
  }
  
})();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.