As others have pointed out, jQuery load()
(and most other jQuery AJAX) does not work cross domain. You say that's not a factor here, but it will be in the future or for others.
The more immediate problem with that code:
var $f = jQuery().load("http://google.com");
alert($f.text());
Is that load()
is not synchronous. That first line of code starts a process but execution immediately drops to the alert()
statement -- long before the load()
process finishes.
Get around both problems by using GM_xmlhttpRequest().
So your code would become:
var pageText;
GM_xmlhttpRequest ( {
method: 'GET',
url: 'http://google.com',
onload: function (responseDetails) {
pageText = responseDetails.responseText;
//-- This will alert the page contents.
alert (pageText);
}
} );
//--- This will alert nothing.
alert (pageText);
Just be sure to process the text inside the onload
handler.
If you really must have the code wait for the loaded page (not recommended), you can use the synchronous
flag, like so:
var pageText;
GM_xmlhttpRequest ( {
method: 'GET',
url: 'http://google.com',
onload: function (responseDetails) {
pageText = responseDetails.responseText;
//-- This will alert the page contents.
alert (pageText);
},
synchronous: true
} );
/*--- This will also alert the text, but the page/GM script can appear
to "freeze" for a bit.
*/
synchronous: true
alert (pageText);