index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // addapted from the document.currentScript polyfill by Adam Miller
  2. // MIT license
  3. // source: https://github.com/amiller-gh/currentScript-polyfill
  4. // added support for Firefox https://bugzilla.mozilla.org/show_bug.cgi?id=1620505
  5. (function (root, factory) {
  6. if (typeof define === 'function' && define.amd) {
  7. define([], factory);
  8. } else if (typeof module === 'object' && module.exports) {
  9. module.exports = factory();
  10. } else {
  11. root.getCurrentScript = factory();
  12. }
  13. }(typeof self !== 'undefined' ? self : this, function () {
  14. function getCurrentScript () {
  15. var descriptor = Object.getOwnPropertyDescriptor(document, 'currentScript')
  16. // for chrome
  17. if (!descriptor && 'currentScript' in document && document.currentScript) {
  18. return document.currentScript
  19. }
  20. // for other browsers with native support for currentScript
  21. if (descriptor && descriptor.get !== getCurrentScript && document.currentScript) {
  22. return document.currentScript
  23. }
  24. // IE 8-10 support script readyState
  25. // IE 11+ & Firefox support stack trace
  26. try {
  27. throw new Error();
  28. }
  29. catch (err) {
  30. // Find the second match for the "at" string to get file src url from stack.
  31. var ieStackRegExp = /.*at [^(]*\((.*):(.+):(.+)\)$/ig,
  32. ffStackRegExp = /@([^@]*):(\d+):(\d+)\s*$/ig,
  33. stackDetails = ieStackRegExp.exec(err.stack) || ffStackRegExp.exec(err.stack),
  34. scriptLocation = (stackDetails && stackDetails[1]) || false,
  35. line = (stackDetails && stackDetails[2]) || false,
  36. currentLocation = document.location.href.replace(document.location.hash, ''),
  37. pageSource,
  38. inlineScriptSourceRegExp,
  39. inlineScriptSource,
  40. scripts = document.getElementsByTagName('script'); // Live NodeList collection
  41. if (scriptLocation === currentLocation) {
  42. pageSource = document.documentElement.outerHTML;
  43. inlineScriptSourceRegExp = new RegExp('(?:[^\\n]+?\\n){0,' + (line - 2) + '}[^<]*<script>([\\d\\D]*?)<\\/script>[\\d\\D]*', 'i');
  44. inlineScriptSource = pageSource.replace(inlineScriptSourceRegExp, '$1').trim();
  45. }
  46. for (var i = 0; i < scripts.length; i++) {
  47. // If ready state is interactive, return the script tag
  48. if (scripts[i].readyState === 'interactive') {
  49. return scripts[i];
  50. }
  51. // If src matches, return the script tag
  52. if (scripts[i].src === scriptLocation) {
  53. return scripts[i];
  54. }
  55. // If inline source matches, return the script tag
  56. if (
  57. scriptLocation === currentLocation &&
  58. scripts[i].innerHTML &&
  59. scripts[i].innerHTML.trim() === inlineScriptSource
  60. ) {
  61. return scripts[i];
  62. }
  63. }
  64. // If no match, return null
  65. return null;
  66. }
  67. };
  68. return getCurrentScript
  69. }));