getCurrentScriptSource.js 658 B

12345678910111213141516171819202122
  1. 'use strict';
  2. function getCurrentScriptSource() {
  3. // `document.currentScript` is the most accurate way to find the current script,
  4. // but is not supported in all browsers.
  5. if (document.currentScript) {
  6. return document.currentScript.getAttribute('src');
  7. } // Fall back to getting all scripts in the document.
  8. var scriptElements = document.scripts || [];
  9. var currentScript = scriptElements[scriptElements.length - 1];
  10. if (currentScript) {
  11. return currentScript.getAttribute('src');
  12. } // Fail as there was no script to use.
  13. throw new Error('[WDS] Failed to get current script source.');
  14. }
  15. module.exports = getCurrentScriptSource;