|
7 | 7 |
|
8 | 8 | /** |
9 | 9 | * Get current script path |
10 | | - * @return {String} The accessable path of current script |
| 10 | + * @return {String} The path of current script |
11 | 11 | * @example |
12 | | - * get 'dev/' or 'https://www.host.com/test/js/' |
| 12 | + * get 'file:///C:/git/live2d-widget/dev/bundle.js' or 'https://www.host.com/test/js/bundle.js' |
13 | 13 | */ |
14 | 14 |
|
15 | 15 | function getCurrentPath(){ |
16 | 16 |
|
17 | | - let currentScriptPath = null; |
18 | | - |
19 | 17 | try{ |
20 | | - // FF, Chrome |
| 18 | + |
| 19 | + // FF, Chrome, Modern browsers |
21 | 20 | // use their API to get the path of current script |
22 | 21 |
|
23 | 22 | // a.b(); |
24 | | - // console.log('Stage1'); |
25 | | - let currentScriptElement = document.currentScript; |
26 | | - currentScriptPath = currentScriptElement.getAttribute('src').replace(/[^/\\\\]+$/, ''); |
| 23 | + console.log('wpStage1'); |
| 24 | + |
| 25 | + return document.currentScript.src; |
| 26 | + |
| 27 | + if(DOC.currentScript){ // FF 4+ |
| 28 | + return DOC.currentScript.src; |
| 29 | + } |
| 30 | + |
27 | 31 | }catch(e){ |
28 | | - // IE 10 +, Safari and Opera 9 |
29 | 32 |
|
30 | | - // console.log('Stage2'); |
31 | | - console.log(e); |
| 33 | + // document.currentScript doesn't supports |
| 34 | + |
| 35 | + console.log('wpStage2'); |
32 | 36 |
|
33 | | - let stack = e.stack || e.sourceURL || e.stacktrace, |
34 | | - rExtractUri = /(file:\/{3}\w:)(\/[^\/]*){3}\//; |
35 | | - // TODO: Fix wrong RegExp: rExtractUri |
36 | | - let absPath = rExtractUri.exec(stack); |
37 | | - currentScriptPath = absPath[0]; |
| 37 | + // Method 1 |
| 38 | + // https://github.com/mozilla/pdf.js/blob/e081a708c36cb2aacff7889048863723fcf23671/src/shared/compatibility.js#L97 |
| 39 | + // IE, Chrome < 29 |
38 | 40 |
|
39 | | - if(currentScriptPath === ''){ |
40 | | - rExtractUri = /(?:http|https|file):\/\/.*?\/.+?.js/; |
41 | | - absPath = rExtractUri.exec(stack); |
42 | | - currentScriptPath = absPath[0]; |
| 41 | + let scripts = document.getElementsByTagName('script'); |
| 42 | + return scripts[scripts.length - 1].src; |
| 43 | + |
| 44 | +/* |
| 45 | + // Method 2 |
| 46 | + // parse the error stack trace maually |
| 47 | + // https://github.com/workhorsy/uncompress.js/blob/master/js/uncompress.js#L25 |
| 48 | +
|
| 49 | + let stack = e.stack; |
| 50 | + let line = null; |
| 51 | +
|
| 52 | + // Chrome and IE |
| 53 | + if (stack.indexOf('@') !== -1) { |
| 54 | + line = stack.split('@')[1].split('\n')[0]; |
| 55 | + // Firefox |
| 56 | + } else { |
| 57 | + line = stack.split('(')[1].split(')')[0]; |
43 | 58 | } |
| 59 | + line = line.substring(0, line.lastIndexOf('/')) + '/'; |
| 60 | + return line; |
| 61 | +*/ |
| 62 | +/* |
| 63 | + // Method 3 |
| 64 | + // https://www.cnblogs.com/rubylouvre/archive/2013/01/23/2872618.html |
44 | 65 |
|
| 66 | + let stack = e.stack; |
| 67 | + if(!stack && window.opera){ |
| 68 | + // Opera 9没有e.stack,但有e.Backtrace,但不能直接取得,需要对e对象转字符串进行抽取 |
| 69 | + stack = (String(e).match(/of linked script \S+/g) || []).join(' '); |
| 70 | + } |
| 71 | + if(stack){ |
| 72 | + // e.stack最后一行在所有支持的浏览器大致如下: |
| 73 | + // chrome23: |
| 74 | + // @ http://113.93.50.63/data.js:4:1 |
| 75 | + // firefox17: |
| 76 | + // @http://113.93.50.63/query.js:4 |
| 77 | + // opera12: |
| 78 | + // @http://113.93.50.63/data.js:4 |
| 79 | + // IE10: |
| 80 | + // @ Global code (http://113.93.50.63/data.js:4:1) |
| 81 | + stack = stack.split(/[@ ]/g).pop(); // 取得最后一行,最后一个空格或@之后的部分 |
| 82 | + stack = stack[0] == '(' ? stack.slice(1,-1) : stack; |
| 83 | + return stack.replace(/(:\d+)?:\d+$/i, ''); // 去掉行号与或许存在的出错字符起始位置 |
| 84 | + } |
| 85 | + let nodes = head.getElementsByTagName('script'); // 只在head标签中寻找 |
| 86 | + for(var i = 0, node; node = nodes[i++];){ |
| 87 | + if(node.readyState === 'interactive'){ |
| 88 | + return node.className = node.src; |
| 89 | + } |
| 90 | + } |
| 91 | +*/ |
45 | 92 | } |
46 | 93 |
|
47 | | - return currentScriptPath; |
48 | | - |
49 | 94 | } |
50 | 95 |
|
51 | 96 | // expose the path to the global, |
52 | 97 | // and wp will finish the following work |
53 | | -__webpack_public_path__ = getCurrentPath(); |
| 98 | +__webpack_public_path__ = getCurrentPath().replace(/[^/\\\\]+$/, ''); |
54 | 99 | if (process.env.NODE_ENV === 'development'){ |
55 | 100 | console.log(`wpPP: publicPath: ${__webpack_public_path__}`); |
56 | 101 | } |
|
0 commit comments