1

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?

Dan
  • 1,343
  • 2
  • 9
  • 12
  • Works fine on GM 1.5 and FF 16 or FF17, on Windows (XP and 7). Something not in the question is the problem. How is it "Not working"? – Brock Adams Nov 22 '12 at 11:15
  • You are right! I still had some other libraries being required. I have doTimeout, json2 and the timeago libraries. I imagine its the timeago library causing the issue as it's the one I added last on my project. – Dan Nov 23 '12 at 06:55
  • Weirdly it started to work when I removed them, but as I added each one to test which could be stopping get from working... it still worked. However in my (big) project get still isnt working - the same test as above doesn't give me a 'yay'. And other functionality relying on get doesnt work either. Guess I now have to do a similar thing for my project which has 15 requires :( – Dan Nov 23 '12 at 07:01
  • Ok. It was none of the files. It was @grant – Dan Nov 23 '12 at 07:13
  • I'm not sure what the specific grant would be, I was trying to list all the things I wanted to use instead of using @grant none. Fail. – Dan Nov 23 '12 at 07:14
  • Argh, now im being told GM_setValue doesnt exist. I didn't think it was a choice between GM_ functions and jQuery functionality :/ – Dan Nov 23 '12 at 07:23

1 Answers1

1

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 () );
} );
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • So the $(document).ready() was the problem! I was sure when I first started that I found several articles saying using .ready was the best way of ensuring your code ran when the page had finished loading. Goes to show how bad some of the 'knowledge' out their on the internet is. THanks Brock! – Dan Nov 23 '12 at 22:11
  • Heh, still not working in the bit I need it to though. I have a link I insert into the page, I then use addEventListener document.getElementById('inserted-link').addEventListener("click",myFunction,true); but get will not work in myFunction. Everything else does - GM_set/get etc. – Dan Nov 23 '12 at 22:53
  • None of that was in this question! Mark this question answered and then ask a new question with the full/correct information. Best to link to your target page, too. – Brock Adams Nov 24 '12 at 01:00
  • Yes, my original question was because I couldn't get my test to work either. I've now narrowed it down and asked this in: http://stackoverflow.com/questions/13537432/how-to-get-jquery-ajax-calls-to-work-from-greasemonkey-page-events – Dan Nov 24 '12 at 01:19