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

我试图在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 

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

我遇到了同样的问题……

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

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

您可以使用此解决方案。

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