bbo
Version:
bbo is a utility library of zero dependencies for javascript.
68 lines (58 loc) • 1.67 kB
JavaScript
import noop from './noop.js';
import './get_tag.js';
import isFunction from './is_function.js';
import randomKey from './random_key.js';
/* eslint-disable */
/**
* Options:
* - param {String} qs parameter (`callback`)
* - prefix {String} qs parameter (`bbo`)
* - name {String} qs parameter (`prefix` + incr)
* - timeout {Number} how long after a timeout error is emitted (`60000`)
* @param {String} url
* @param {Object|Function} optional options / callback
* @param {Function} optional callback
*/
function jsonp(url, opts, fn) {
if (isFunction(opts)) {
fn = opts;
opts = {};
}
if (!opts) opts = {};
var prefix = opts.prefix || 'bbo';
var id = opts.name || prefix + randomKey(10);
var param = opts.param || 'callback';
var timeout = null != opts.timeout ? opts.timeout : 60000;
var enc = encodeURIComponent;
var target = document.getElementsByTagName('script')[0] || document.head;
var script;
var timer;
if (timeout) {
timer = setTimeout(function () {
cleanup();
if (fn) fn(new Error('Timeout'));
}, timeout);
}
function cleanup() {
if (script.parentNode) script.parentNode.removeChild(script);
window[id] = noop();
if (timer) clearTimeout(timer);
}
function cancel() {
if (window[id]) {
cleanup();
}
}
window[id] = function (data) {
cleanup();
if (fn) fn(data, null);
};
console.log(url);
url += (~url.indexOf('?') ? '&' : '?') + param + '=' + enc(id);
url = url.replace('?&', '?');
script = document.createElement('script');
script.src = url;
target.parentNode.insertBefore(script, target);
return cancel;
}
export default jsonp;