GM_xxx in jQuery events #1626

Aymkdn opened this Issue Sep 10, 2012 · 2 comments


None yet

2 participants

Aymkdn commented Sep 10, 2012

Hi guys,

I load jQuery on a page with Greasemonkey, then I use some GM_setValue and GM_getValue, and finally I use the jQuery events.
My code is as simple as this one :

// ==UserScript==
// @name        Test
// @description test
// @include
// @version     1
// @grant       GM_setValue
// @grant       GM_getValue
// @unwrap
// ==/UserScript==

var GM_JQ = document.createElement('script');
GM_JQ.src = '';
GM_JQ.type = 'text/javascript';

// things to do when jQuery is loaded
function letsJQuery() {
  GM_setValue("test","debug"); // save this value
  $('#gbqfq').click(function() {
    // the below code doesn't work, GM_getValue is a function but returns undefined
    // the below code works and returns the correct value
    //setTimeout(function() { alert(GM_getValue("test")); },150);

// Check if jQuery's loaded
function GM_wait() {
  if(typeof unsafeWindow.jQuery == 'undefined') { unsafeWindow.setTimeout(GM_wait,100); }
  else { $ = unsafeWindow.jQuery; jQuery = unsafeWindow.jQuery; letsJQuery(); }

It appears that if we call GM_getValue into a jQuery Event call then GM_getValue returns undefined. We need to call GM_getValue with a setTimeout.
I guess it's a context problem ?


This is known/expected due to limitations of the security sandbox. See:

@arantius arantius closed this Sep 10, 2012
Aymkdn commented Sep 10, 2012

Oh OK I missed that. Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment