Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GM_xxx in jQuery events #1626

Closed
Aymkdn opened this issue Sep 10, 2012 · 2 comments
Closed

GM_xxx in jQuery events #1626

Aymkdn opened this issue Sep 10, 2012 · 2 comments

Comments

@Aymkdn
Copy link

@Aymkdn 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     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

This comment has been minimized.

Copy link
Collaborator

@arantius arantius commented Sep 10, 2012

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

@arantius arantius closed this Sep 10, 2012
@Aymkdn

This comment has been minimized.

Copy link
Author

@Aymkdn 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
Projects
None yet
2 participants
You can’t perform that action at this time.