greasemonkeygreasemonkey-4

share data between 2 greasemonkey scripts running on different pages


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 !


Solution

  • 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);
      }
      
    })();