Skip to content

Instantly share code, notes, and snippets.

@brettz9
Forked from anonymous/JSONP-global.js
Last active December 9, 2015 17:28
Show Gist options
  • Save brettz9/4303894 to your computer and use it in GitHub Desktop.
Save brettz9/4303894 to your computer and use it in GitHub Desktop.
// Adds one JSONP global (for requireJS module, see JSONP.js)
var JSONP = (function (global) {
// (C) WebReflection Essential - Mit Style
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL)
'use strict';
var id = 0,
ns = 'JSONP',
prefix = '__JSONP__',
document = global.document,
documentElement = document.documentElement;
return function (uri, callback) {
var src = prefix + id++,
script = document.createElement('script'),
JSONPResponse = function () {
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; }
documentElement.removeChild(script);
callback.apply(this, arguments);
};
global[ns][src] = JSONPResponse;
documentElement.insertBefore(
script,
documentElement.lastChild
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src;
};
}(this));
/*globals define */
define(function () {
// (C) WebReflection Essential - Mit Style
// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL)
'use strict';
var id = 0,
global = window,
ns = 'require',
prefix = '__JSONP__',
document = global.document,
documentElement = document.documentElement;
return function (uri, callback) {
var src = prefix + id++,
script = document.createElement('script'),
JSONPResponse = function () {
try { delete global[ns][src]; } catch(e) { global[ns][src] = null; }
documentElement.removeChild(script);
callback.apply(this, arguments);
};
global[ns][src] = JSONPResponse;
documentElement.insertBefore(
script,
documentElement.lastChild
).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src;
};
});
/*globals JSONP */
(function () {'use strict';
function MediaWikiJSONP(opts, argObj, cb) {
if (!(this instanceof MediaWikiJSONP)) {
return new MediaWikiJSONP(opts, argObj, cb);
}
if (typeof opts === 'string') {
this.baseURL = opts;
}
else {
this.baseURL = opts.baseURL;
}
if (argObj) {
this.send(argObj, cb);
}
}
MediaWikiJSONP.prototype.send = function MediaWikiJSONP__send (argObj, cb) {
cb = cb || function () {}; // Are there API calls with side effects?
var uri, arg, args = '';
for (arg in argObj) {
if (argObj.hasOwnProperty(arg)) {
args += '&' + arg + '=' + encodeURIComponent(argObj[arg]);
}
}
uri = this.baseURL + '/w/api.php?format=json' + args;
JSONP(uri, cb);
};
// EXPORTS
this.MediaWikiJSONP = MediaWikiJSONP;
}());
/*globals define */
define(['./JSONP'], function (JSONP) {
'use strict';
function MediaWikiJSONP(opts, argObj, cb) {
if (!(this instanceof MediaWikiJSONP)) {
return new MediaWikiJSONP(opts, argObj, cb);
}
if (typeof opts === 'string') {
this.baseURL = opts;
}
else {
this.baseURL = opts.baseURL;
}
if (argObj) {
this.send(argObj, cb);
}
}
MediaWikiJSONP.prototype.send = function MediaWikiJSONP__send (argObj, cb) {
cb = cb || function () {}; // Are there API calls with side effects?
var uri, arg, args = '';
for (arg in argObj) {
if (argObj.hasOwnProperty(arg)) {
args += '&' + arg + '=' + encodeURIComponent(argObj[arg]);
}
}
uri = this.baseURL + '/w/api.php?format=json' + args;
JSONP(uri, cb);
};
return MediaWikiJSONP;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment