views:

1007

answers:

3

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  A: 

I'm assuming the code is actually being reached?

$("a#linkid").click(function()
{
    alert("Here"); // I'm assuming this works?
    GM_setValue("foo", 123); // doesn't work, but does not generate error
});

This link may be useful. Are you doing all your GM stuff inside of a function that only runs when jQuery is loaded?

I'm actually getting the same problem you report when I do this... Investigating.

John Weldon
Yes, I've confirmed that the code actually executes. This is a brief example snippet.
Luke Dennis
+6  A: 

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.

Matthew Flaschen
Nice. Thanks for this good response. +1
John Weldon
+1  A: 

You can use this solution.

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

   //getValue
   setTimeout(GM_getValue("foo"),0);
});
Rady
Good trick, actually. Though I think you meant /*setValue*/ and /*getValue*/, the code you posted wouldn't actually run. ;)
Luke Dennis