2

I have a GM script which inserts a link on to the page, and then adds an event listener for when its clicked.

This then runs a function which contains among other things some jQuery.get calls. These calls however do not seem to fire unless I use the unsafeWindow version of jQuery.

function runMyFunc() {
    console.log('myFunc is called');

    $.get('index.php',function () {
        console.log('jQuery.get worked');
    });
}

$("#someHook").before('<a id="myLink">Link</a>');

$('#myLink').click(runMyFunc);

The above will output 'myFunc is called' to console, but will not do anything with the .get

I'm using FF17 and GM1.5, jQUery from http://code.jquery.com/jquery.js

Is there a nicer way of getting this to work than using unsafeWindow? I had this working before GM 1.0 and have a lot of $.get which I would need to change in my scripts, not all of which are run from the same scenario

Dan
  • 1,343
  • 2
  • 9
  • 12

1 Answers1

1

Your code works fine for me. Why/How do you think $.get is not working?

Remember that you will never see the 'jQuery.get worked' message if there is a server error (404 etc.) with index.php. Did you check the Firebug Net panel, or Wireshark, etc. to see if the AJAX call was made?

Anyway, you can see that code working, plus some error handling if you install this Greasemonkey script:

// ==UserScript==
// @name     _delme9h762
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @include  http://fiddle.jshell.net/ZqhRH/*
// @require  http://code.jquery.com/jquery.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/

function runMyFunc() {
    console.log('myFunc is called');

    $.get('index.php', function () {
        console.log ('jQuery.get worked');
    } )
    .error ( function (respObj) {
        console.log ("Error! ", respObj.status, respObj.statusText);
    } )
    .complete ( function (respObj) {
        console.log ("AJAX Complete. Status: ", respObj.status);
    } )
    ;
}

$("#someHook").before('<a id="myLink">Link</a>');

$('#myLink').click(runMyFunc);


And then visit fiddle.jshell.net/ZqhRH/1/show/.

Note, the console will show:

myFunc is called
Error! 404 NOT FOUND
AJAX Complete. Status: 404

On the jsFiddle site, because index.php does not exist there, but $.get() is otherwise working perfectly.

Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • When I test $.get without using the link event I see the 'jQuery.get worked' message. The script above fails at jsfiddle (no index.php found) On the site I am trying to run the code I get: Error! 0 TypeError: xhr is undefined jquery...user.js (line 21) AJAX Complete. Status: 0 – Dan Nov 24 '12 at 09:41
  • bleh, I dont like this comment box, keeps thinking my returns are submits. I see the GET request in firebug only for the non-link scenario tests – Dan Nov 24 '12 at 09:47
  • Verify: The answer script did not "fail" at jsFiddle. It made the requested get (unlike what the question reports), and properly returned a 404 -- merely because the specified page doesn't exist. The `$.get()` worked as intended. – Brock Adams Nov 24 '12 at 09:54
  • As for getting error 0 on your site. See [this question](http://stackoverflow.com/q/1023867/331508), but something (not shown in the question), that your script, and/or the server, and/or the target page are doing is the problem. **Link to the target page** (or at the very least, make a proper SSCE that demonstrates the problem). – Brock Adams Nov 24 '12 at 09:59
  • Yeah, looks to be the same thing. I added their error function suggestion: if(xmlHttpRequest.readyState == 0 || xmlHttpRequest.status == 0) { console.log('premature ajaxulation'); return; } // it's not really an error else console.log ("Error! ", textStatus, errorThrown); I also stuck your test page up on my own site and it tested fine when I changed the get target. So the target page (which i dont own) must be up to something dodgy. – Dan Nov 24 '12 at 10:10
  • I have since tested on a 'mobile' version of the same site. My code works fine. The normal version has a load a lot of their ajax code - so I assume there is something in their code that interferes with mine. – Dan Nov 24 '12 at 10:15