UNPKG

bbo

Version:

bbo is a utility library of zero dependencies for javascript.

70 lines (59 loc) 1.7 kB
'use strict'; var noop = require('./noop.js'); require('./get_tag.js'); var is_function = require('./is_function.js'); var random_key = require('./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 (is_function(opts)) { fn = opts; opts = {}; } if (!opts) opts = {}; var prefix = opts.prefix || 'bbo'; var id = opts.name || prefix + random_key(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; } module.exports = jsonp;