Greasemonkey + jQuery:在事件回调中使用GM_setValue()

时间:2009-05-13 22:54:59

标签: jquery events callback greasemonkey storage

我正在尝试使用GreaseMonkey脚本在长期存储中设置数据,但GM_setValue()似乎无声地失败:

$("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 个答案:

答案 0 :(得分:9)

我认为这是特定的Greasemonkey安全问题。请参阅0.7.20080121.0 compatibility。 GM不允许用户页面调用GreaseMonkey API,而这正是您在那里所做的(您正在注册一个在用户上下文中运行的JQuery的点击处理程序)。该页面也提供了一种解决方法。

答案 1 :(得分:2)

我遇到了同样的问题...

以前的解决方案对我不起作用,我找到了这样的解决方案......

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 :(得分:0)

您可以使用此解决方案。

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

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