UNPKG

vue-socials

Version:

Social media share buttons and counts for Vue.js

36 lines (28 loc) 1.02 kB
/** * Simple implementation of JSONP. * It creates window.callbackRegistry to minimize global window pollution. * It uses callback to prevent adding the promise polyfill. */ function JSONP(url, callback, callbackName) { var script = document.createElement('script'); var cbName = callbackName || 'callback'; if (callback) { if (!window.callbackRegistry) { window.callbackRegistry = {}; } var _key = "cb".concat(String(Math.random()).slice(-6)); script.src = "".concat(url).concat(url.indexOf('?') === -1 ? '?' : '&').concat(cbName, "=callbackRegistry.").concat(_key); script.onerror = function JSONPOnError() { delete window.callbackRegistry[_key]; callback(new Error(), null); }; window.callbackRegistry[_key] = function JSONPCallback(response) { delete window.callbackRegistry[_key]; callback(null, response); }; } else { script.src = url; } document.head.removeChild(document.head.appendChild(script)); } export default JSONP;