Greasemonkey + jQuery: using GM_setValue () in an event callback

I am trying to set data in a long-term storage in a GreaseMonkey script, except that GM_setValue () seems to fail:

$("a#linkid").click(function()
{
    GM_setValue("foo", 123); // doesn't work, but does not generate error
});

GM_setValue("bar", 123); // works properly, value is set
+3
source share
3 answers

I think this is a special Greasemonkey security issue. See 0.7.20080121.0 compatibility . GM does not allow user pages to access GreaseMonkey APIs and what you do there (you register a click handler with jQuery running in a user context). A workaround is also listed on this page.

+9

...

, ...

function gmGet(name) {
    var theValue = GM_getValue(name);
    return theValue;
}

function gmSet(name, valuee) {
    GM_setValue(name, valuee);
}

$("a#linkid").click(function(){
    //setValue
    gmSet("foo", 123);

   //getValue
   gmGet("foo");
});
+2

You can use this solution.

$("a#linkid").click(function()
{
    //setValue
    setTimeout(GM_setValue("foo", 123),0);

   //getValue
   setTimeout(GM_getValue("foo"),0);
});
0
source

Source: https://habr.com/ru/post/1708299/


All Articles