UNPKG

current-executing-script

Version:

Get the currently executing script, regardless of its source/trigger/synchronicity. Similar to HTML5's `document.currentScript` but arguably much more useful!

9 lines 2.17 kB
/*! * document.currentScript * Polyfill for `document.currentScript`. * Copyright (c) 2015 James M. Greene * Licensed MIT * http://jsfiddle.net/JamesMGreene/9DFc9/ * v0.1.7 */ !function(){function a(a){if("string"==typeof a&&a)for(var b=0,c=i.length;c>b;b++)if(i[b].src===a)return i[b];return null}function b(){for(var a=null,b=0,c=i.length;c>b;b++)if(!i[b].src){if(a)return null;a=i[b]}return a}function c(){var a=0;return"undefined"!=typeof e&&e&&"number"==typeof e.skipStackDepth&&(a=e.skipStackDepth),a}function d(a,b){var e,f,g,h="number"==typeof b;return b=h?b:c(),"string"==typeof a&&a&&(h?f=a.match(/(data:text\/javascript(?:;[^,]+)?,.+?|(?:|blob:)(?:http[s]?|file):\/\/[\/]?.+?\/[^:\)]*?)(?::\d+)(?::\d+)?/):(f=a.match(/^(?:|[^:@]*@|.+\)@(?=data:text\/javascript|blob|http[s]?|file)|.+?\s+(?: at |@)(?:[^:\(]+ )*[\(]?)(data:text\/javascript(?:;[^,]+)?,.+?|(?:|blob:)(?:http[s]?|file):\/\/[\/]?.+?\/[^:\)]*?)(?::\d+)(?::\d+)?/),f&&f[1]||(f=a.match(/\)@(data:text\/javascript(?:;[^,]+)?,.+?|(?:|blob:)(?:http[s]?|file):\/\/[\/]?.+?\/[^:\)]*?)(?::\d+)(?::\d+)?/))),f&&f[1]&&(b>0?(g=a.slice(a.indexOf(f[0])+f[0].length),e=d(g,b-1)):e=f[1])),e}function e(){if(0===i.length)return null;if(1===i.length)return i[0];if("readyState"in i[0])for(var c=i.length;c--;)if("interactive"===i[c].readyState)return i[c];if("loading"===document.readyState)return i[i.length-1];var e,j=new Error;if(f&&(e=j.stack),!e&&g)try{throw j}catch(k){e=k.stack}if(e){var l=d(e),m=a(l);return m||l!==h||(m=b()),m}return null}var f=!1,g=!1;!function(){try{var a=new Error;throw f="string"==typeof a.stack&&!!a.stack,a}catch(b){g="string"==typeof b.stack&&!!b.stack}}();var h=window.location.href,i=document.getElementsByTagName("script");e.skipStackDepth=1;var j=!("currentScript"in document),k=document.__defineGetter__,l="function"==typeof Object.defineProperty&&function(){var a;try{Object.defineProperty(document,"_xyz",{get:function(){return"blah"},configurable:!0}),a="blah"===document._xyz,delete document._xyz}catch(b){a=!1}return a}();document._currentScript=e,j&&(l?Object.defineProperty(document,"currentScript",{get:e}):k&&document.__defineGetter__("currentScript",e))}();