greasemonkey and jquery get not working

1.3k Views Asked by At

I am using jQuery with GM 1.5 and have found I am unable to use .get

I require http://code.jquery.com/jquery.js

and my code is just this

this.$ = this.jQuery = jQuery.noConflict(true);

$(document).ready(function () {
    $.get('index.php',function () {
        console.log('yay');
        console.log($(this).html());

    });
});

I am sure I have been able to do this in previous versions, is this something to do with the sandbox changes that were made?

1

There are 1 best solutions below

4
On BEST ANSWER

The code in the question works fine. Verified with Greasemonkey 1.5 and Firefox 16 and 17 on Windows XP and Windows 7.

Re:

Argh, now im being told GM_setValue doesnt exist. I didn't think it was a choice between GM_ functions and jQuery functionality


  1. You don't have to choose. Don't inject jQuery (or most other libraries) use @require. Then, with the proper @grant directives, you can use GM_ functions easily.

  2. There is no point in code like this.$ = this.jQuery = jQuery.noConflict(true); unless you use @grant none -- which would shut off GM_ functions.

  3. $(document).ready() is not needed in a Greasemonkey script unless you use @run-at document-start.


So, use code like this:

// ==UserScript==
// @name     YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_getValue
// @grant    GM_setValue
// @grant    etc., etc.
// ==/UserScript==

$.get ('index.php', function () {
    console.log ('yay');
    console.log ($(this).html () );
} );