|
55 | 55 |
|
56 | 56 | /** |
57 | 57 | * Get current script path |
58 | | - * @return {String} The accessable path of current script |
| 58 | + * @return {String} The path of current script |
59 | 59 | * @example |
60 | | - * get 'dev/' or 'https://www.host.com/test/js/' |
| 60 | + * get 'file:///C:/git/live2d-widget/dev/bundle.js' or 'https://www.host.com/test/js/bundle.js' |
61 | 61 | */ |
62 | 62 |
|
63 | 63 | function getCurrentPath(){ |
64 | 64 |
|
65 | | - let currentScriptPath = null; |
66 | | - |
67 | 65 | try{ |
68 | | - // FF, Chrome |
| 66 | + |
| 67 | + // FF, Chrome, Modern browsers |
69 | 68 | // use their API to get the path of current script |
70 | 69 |
|
71 | 70 | // a.b(); |
72 | | - // console.log('Stage1'); |
73 | | - let currentScriptElement = document.currentScript; |
74 | | - currentScriptPath = currentScriptElement.getAttribute('src').replace(/[^/\\\\]+$/, ''); |
| 71 | + console.log('wpStage1'); |
| 72 | + |
| 73 | + return document.currentScript.src; |
| 74 | + |
| 75 | + if(DOC.currentScript){ // FF 4+ |
| 76 | + return DOC.currentScript.src; |
| 77 | + } |
| 78 | + |
75 | 79 | }catch(e){ |
76 | | - // IE 10 +, Safari and Opera 9 |
77 | 80 |
|
78 | | - // console.log('Stage2'); |
79 | | - console.log(e); |
| 81 | + // document.currentScript doesn't supports |
80 | 82 |
|
81 | | - let stack = e.stack || e.sourceURL || e.stacktrace, |
82 | | - rExtractUri = /(file:\/{3}\w:)(\/[^\/]*){3}\//; |
83 | | - // TODO: Fix wrong RegExp: rExtractUri |
84 | | - let absPath = rExtractUri.exec(stack); |
85 | | - currentScriptPath = absPath[0]; |
| 83 | + console.log('wpStage2'); |
86 | 84 |
|
87 | | - if(currentScriptPath === ''){ |
88 | | - rExtractUri = /(?:http|https|file):\/\/.*?\/.+?.js/; |
89 | | - absPath = rExtractUri.exec(stack); |
90 | | - currentScriptPath = absPath[0]; |
91 | | - } |
| 85 | + // Method 1 |
| 86 | + // https://github.com/mozilla/pdf.js/blob/e081a708c36cb2aacff7889048863723fcf23671/src/shared/compatibility.js#L97 |
| 87 | + // IE, Chrome < 29 |
92 | 88 |
|
93 | | - } |
| 89 | + let scripts = document.getElementsByTagName('script'); |
| 90 | + return scripts[scripts.length - 1].src; |
94 | 91 |
|
95 | | - return currentScriptPath; |
| 92 | +/* |
| 93 | + // Method 2 |
| 94 | + // parse the error stack trace maually |
| 95 | + // https://github.com/workhorsy/uncompress.js/blob/master/js/uncompress.js#L25 |
| 96 | + |
| 97 | + let stack = e.stack; |
| 98 | + let line = null; |
| 99 | + |
| 100 | + // Chrome and IE |
| 101 | + if (stack.indexOf('@') !== -1) { |
| 102 | + line = stack.split('@')[1].split('\n')[0]; |
| 103 | + // Firefox |
| 104 | + } else { |
| 105 | + line = stack.split('(')[1].split(')')[0]; |
| 106 | + } |
| 107 | + line = line.substring(0, line.lastIndexOf('/')) + '/'; |
| 108 | + return line; |
| 109 | +*/ |
| 110 | +/* |
| 111 | + // Method 3 |
| 112 | + // https://www.cnblogs.com/rubylouvre/archive/2013/01/23/2872618.html |
| 113 | + |
| 114 | + let stack = e.stack; |
| 115 | + if(!stack && window.opera){ |
| 116 | + // Opera 9没有e.stack,但有e.Backtrace,但不能直接取得,需要对e对象转字符串进行抽取 |
| 117 | + stack = (String(e).match(/of linked script \S+/g) || []).join(' '); |
| 118 | + } |
| 119 | + if(stack){ |
| 120 | + // e.stack最后一行在所有支持的浏览器大致如下: |
| 121 | +      // chrome23: |
| 122 | +      // @ http://113.93.50.63/data.js:4:1 |
| 123 | +      // firefox17: |
| 124 | +      // @http://113.93.50.63/query.js:4 |
| 125 | +      // opera12: |
| 126 | +      // @http://113.93.50.63/data.js:4 |
| 127 | +      // IE10: |
| 128 | +      // @ Global code (http://113.93.50.63/data.js:4:1) |
| 129 | +     stack = stack.split(/[@ ]/g).pop(); // 取得最后一行,最后一个空格或@之后的部分 |
| 130 | + stack = stack[0] == '(' ? stack.slice(1,-1) : stack; |
| 131 | + return stack.replace(/(:\d+)?:\d+$/i, ''); // 去掉行号与或许存在的出错字符起始位置 |
| 132 | + } |
| 133 | + let nodes = head.getElementsByTagName('script'); // 只在head标签中寻找 |
| 134 | + for(var i = 0, node; node = nodes[i++];){ |
| 135 | + if(node.readyState === 'interactive'){ |
| 136 | + return node.className = node.src; |
| 137 | + } |
| 138 | + } |
| 139 | +*/ |
| 140 | + } |
96 | 141 |
|
97 | 142 | } |
98 | 143 |
|
99 | 144 | // expose the path to the global, |
100 | 145 | // and wp will finish the following work |
101 | | -__webpack_public_path__ = getCurrentPath(); |
| 146 | +__webpack_public_path__ = getCurrentPath().replace(/[^/\\\\]+$/, ''); |
102 | 147 | if (process.env.NODE_ENV === 'development'){ |
103 | 148 | console.log(`wpPP: publicPath: ${__webpack_public_path__}`); |
104 | 149 | } |
|
0 commit comments