|
5 | 5 | function getCurrentScriptSource() { |
6 | 6 | // `document.currentScript` is the most accurate way to get the current running script, |
7 | 7 | // but is not supported in all browsers (most notably, IE). |
8 | | - if (document.currentScript) { |
| 8 | + if ('currentScript' in document) { |
| 9 | + // In some cases, `document.currentScript` would be `null` even if the browser supports it: |
| 10 | + // e.g. asynchronous chunks on Firefox. |
| 11 | + // We should not fallback to the list-approach as it would not be safe. |
| 12 | + if (document.currentScript == null) return; |
9 | 13 | return document.currentScript.getAttribute('src'); |
10 | 14 | } |
11 | | - |
12 | | - // Fallback to getting all scripts running in the document. |
13 | | - const scriptElements = document.scripts || []; |
14 | | - const scriptElementsWithSrc = Array.prototype.filter.call(scriptElements, function (elem) { |
15 | | - return elem.getAttribute('src'); |
16 | | - }); |
17 | | - if (scriptElementsWithSrc.length) { |
| 15 | + // Fallback to getting all scripts running in the document, |
| 16 | + // and finding the last one injected. |
| 17 | + else { |
| 18 | + const scriptElementsWithSrc = Array.prototype.filter.call( |
| 19 | + document.scripts || [], |
| 20 | + function (elem) { |
| 21 | + return elem.getAttribute('src'); |
| 22 | + } |
| 23 | + ); |
| 24 | + if (!scriptElementsWithSrc.length) return; |
18 | 25 | const currentScript = scriptElementsWithSrc[scriptElementsWithSrc.length - 1]; |
19 | 26 | return currentScript.getAttribute('src'); |
20 | 27 | } |
|
0 commit comments