🏠 Home 

Discussions » Development

Native GM_xmlhttpRequest support in jQuery Ajax Requests - Working! (need testers)

§
Posted: 21-01-2015

Native GM_xmlhttpRequest support in jQuery Ajax Requests - Working! (need testers)

I recently made a post about a javascript library I am developing called jMod

While working on it, I remembered how much I absolutely HATE not being able to reliably use jQuery for cross-origin requests. Instead, I am usually forced to use GM_xmlhttpRequest which just makes me sad.

So I added a new feature to jMod:

jMod.jQueryExtensions.addCrossDomainSupport(jQuery);

Calling this will replace the default jQuery data transmission method with a custom one using GM_xmlhttpRequest. This affects all ajax requests made by jQuery!

Look at this example

jMod.jQueryExtensions.addCrossDomainSupport($);
// Test $.ajax()
console.log('Test $.ajax("http://google.com")');
$.ajax({
url: 'http://google.com',
contentType: 'text/plain',
type: 'GET',
dataType: 'html'
})
.done(function() {
console.log("$.ajax() success");
})
.fail(function() {
console.log("$.ajax() error");
});
// Test $(element).load()
console.log('Test $(element).load("http://google.com #hplogo")');
var tmpDiv = document.createElement('div');
tmpDiv.id = 'tmpDiv';
document.body.appendChild(tmpDiv);
$('#tmpDiv').load('http://google.com #hplogo', function(responseText, textStatus, jqXHR){
console.log('$(element).load() ' + textStatus);
});

The second test loads the remote page's content into the new div element without having to set any special flags. You dont have to worry about cross-origin errors ever again! You hear that Same-origin policy?! I DO WHAT I WANT!!

The r###lting log can be at the bottom of this post (Firefox and Chrome).

Right now I just need some help testing the new feature. I've edited so many of my browser's permission settings, I cant be sure this will work for other users (but I am pretty positive it will). So can any of ya'll help me test it? I will even write the script for you :D

// ==UserScript==
// @name             jMod Ajax Test Script
// @description    Test script
// @author           jgjake2
// @include          http://test.com/change/me
// @require          http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @require          http://myuserjs.org/API/0.0.17/jMod.js
// @version          0.1
// @grant            unsafeWindow
// @grant            GM_info
// @grant            GM_log
// @grant            GM_addStyle
// @grant            GM_getMetadata
// @grant            GM_xmlhttpRequest
// @grant            GM_registerMenuCommand
// @grant            GM_getValue
// @grant            GM_setValue
// @grant            GM_listValues
// @grant            GM_deleteValue
// @grant            GM_getResourceText
// @grant            GM_getResourceURL
// @unwrap
// @noframes
// @run-at document-start
// @jMod             {"debug": true, "API": {"log": {"debug": true}}}
// ==/UserScript==
$(document).ready(function() {
jMod.jQueryExtensions.addCrossDomainSupport($);
function test_jQueryFunctions(){
jMod.jQueryExtensions.addCrossDomainSupport($);
// Test $.ajax()
console.log('Test $.ajax("http://google.com")');
$.ajax({
url: 'http://google.com',
contentType: 'text/plain',
type: 'GET',
dataType: 'html'
})
.done(function() {
console.log("$.ajax() success");
})
.fail(function() {
console.log("$.ajax() error");
});
// Test $(element).load()
console.log('Test $(element).load("http://google.com #hplogo")');
var tmpDiv = document.createElement('div');
tmpDiv.id = 'tmpDiv';
document.body.appendChild(tmpDiv);
$('#tmpDiv').load('http://google.com #hplogo', function(responseText, textStatus, jqXHR){
console.log('$(element).load() ' + textStatus);
});
}
test_jQueryFunctions();
});
Firebug R###lt
Firebug R###lt
Google Chrome R###lt
Google Chrome R###lt
woxxomMod
§
Posted: 21-01-2015

Haven't tested it but here's a suggestion: you can add new user profiles into Chrome and Firefox to test the script in a default environment.

§
Posted: 04-08-2017
Edited: 04-08-2017

Very Good. But is too big, Can I use one part of them ?

Post reply

Sign in to post a reply.