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

*爱你&永不变心* 提交于 2019-12-10 19:47:30

问题


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

$("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

回答1:


I think this is a specific Greasemonkey security issue. Please see 0.7.20080121.0 compatibility. GM does not allow user pages to call GreaseMonkey APIs, and that's what you're doing there (you're registering a click handler with JQuery running in the user context). A workaround is also given on that page.




回答2:


I had the same kind of problem...

Previous solution was not working for me and I found solution like this...

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");
});



回答3:


You can use this solution.

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

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


来源:https://stackoverflow.com/questions/860829/greasemonkey-jquery-using-gm-setvalue-within-an-event-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!