You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
2.8 KiB
80 lines
2.8 KiB
4 weeks ago
|
// addapted from the document.currentScript polyfill by Adam Miller
|
||
|
// MIT license
|
||
|
// source: https://github.com/amiller-gh/currentScript-polyfill
|
||
|
|
||
|
// added support for Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1620505
|
||
|
|
||
|
(function (root, factory) {
|
||
|
if (typeof define === 'function' && define.amd) {
|
||
|
define([], factory);
|
||
|
} else if (typeof module === 'object' && module.exports) {
|
||
|
module.exports = factory();
|
||
|
} else {
|
||
|
root.getCurrentScript = factory();
|
||
|
}
|
||
|
}(typeof self !== 'undefined' ? self : this, function () {
|
||
|
function getCurrentScript () {
|
||
|
var descriptor = Object.getOwnPropertyDescriptor(document, 'currentScript')
|
||
|
// for chrome
|
||
|
if (!descriptor && 'currentScript' in document && document.currentScript) {
|
||
|
return document.currentScript
|
||
|
}
|
||
|
|
||
|
// for other browsers with native support for currentScript
|
||
|
if (descriptor && descriptor.get !== getCurrentScript && document.currentScript) {
|
||
|
return document.currentScript
|
||
|
}
|
||
|
|
||
|
// IE 8-10 support script readyState
|
||
|
// IE 11+ & Firefox support stack trace
|
||
|
try {
|
||
|
throw new Error();
|
||
|
}
|
||
|
catch (err) {
|
||
|
// Find the second match for the "at" string to get file src url from stack.
|
||
|
var ieStackRegExp = /.*at [^(]*\((.*):(.+):(.+)\)$/ig,
|
||
|
ffStackRegExp = /@([^@]*):(\d+):(\d+)\s*$/ig,
|
||
|
stackDetails = ieStackRegExp.exec(err.stack) || ffStackRegExp.exec(err.stack),
|
||
|
scriptLocation = (stackDetails && stackDetails[1]) || false,
|
||
|
line = (stackDetails && stackDetails[2]) || false,
|
||
|
currentLocation = document.location.href.replace(document.location.hash, ''),
|
||
|
pageSource,
|
||
|
inlineScriptSourceRegExp,
|
||
|
inlineScriptSource,
|
||
|
scripts = document.getElementsByTagName('script'); // Live NodeList collection
|
||
|
|
||
|
if (scriptLocation === currentLocation) {
|
||
|
pageSource = document.documentElement.outerHTML;
|
||
|
inlineScriptSourceRegExp = new RegExp('(?:[^\\n]+?\\n){0,' + (line - 2) + '}[^<]*<script>([\\d\\D]*?)<\\/script>[\\d\\D]*', 'i');
|
||
|
inlineScriptSource = pageSource.replace(inlineScriptSourceRegExp, '$1').trim();
|
||
|
}
|
||
|
|
||
|
for (var i = 0; i < scripts.length; i++) {
|
||
|
// If ready state is interactive, return the script tag
|
||
|
if (scripts[i].readyState === 'interactive') {
|
||
|
return scripts[i];
|
||
|
}
|
||
|
|
||
|
// If src matches, return the script tag
|
||
|
if (scripts[i].src === scriptLocation) {
|
||
|
return scripts[i];
|
||
|
}
|
||
|
|
||
|
// If inline source matches, return the script tag
|
||
|
if (
|
||
|
scriptLocation === currentLocation &&
|
||
|
scripts[i].innerHTML &&
|
||
|
scripts[i].innerHTML.trim() === inlineScriptSource
|
||
|
) {
|
||
|
return scripts[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// If no match, return null
|
||
|
return null;
|
||
|
}
|
||
|
};
|
||
|
|
||
|
return getCurrentScript
|
||
|
}));
|