Skip to content
This repository

GM_xxx in jQuery events #1626

Closed
Aymkdn opened this Issue · 2 comments

2 participants

Aymeric arantius
Aymeric

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     https://www.google.fr/
// @version     1
// @grant       GM_setValue
// @grant       GM_getValue
// @unwrap
// ==/UserScript==

var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);

// 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
    alert(GM_getValue("test"));
    // 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(); }
}
GM_wait();

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 ?

arantius
Collaborator

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

arantius arantius closed this
Aymeric

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
Something went wrong with that request. Please try again.