Last active
February 14, 2017 21:07
-
-
Save jiyu3/ef6bb7b48c750bb9b71f285cf312747f to your computer and use it in GitHub Desktop.
javascriptで現在実行中のファイルパス(__FILE__)及び行番号(__LINE__)を取得する方法 ref: http://qiita.com/jiyu/items/6be41ca3a7b2e91b0335
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(window, '__STACK__', { | |
get: function(){ | |
let origin = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack){ return stack; }; | |
let err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
let stack = err.stack; | |
Error.prepareStackTrace = origin; | |
return stack; | |
} | |
}); | |
Object.defineProperty(window, '__FILE__', { | |
get: function(){ | |
let filename = __STACK__[1].getFileName().replace(location.origin, "").replace(window.location.search, ""); | |
if(!filename) filename = "/"; | |
return filename; | |
} | |
}); | |
Object.defineProperty(window, '__LINE__', { | |
get: function(){ | |
return __STACK__[1].getLineNumber(); | |
} | |
}); | |
console.log("__FILE__", __FILE__); | |
console.log("__LINE__", __LINE__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Object.defineProperty(window, '__STACK__', { | |
get: function(){ | |
var orig = Error.prepareStackTrace; | |
Error.prepareStackTrace = function(_, stack){ return stack; }; | |
var err = new Error; | |
Error.captureStackTrace(err, arguments.callee); | |
var stack = err.stack; | |
Error.prepareStackTrace = orig; | |
return stack; | |
} | |
}); | |
Object.defineProperty(window, '__FILE__', { | |
get: function(){ | |
let filename = __STACK__[1].getFileName().replace(location.origin, "").replace(window.location.search, ""); | |
if(!filename) filename = "/"; | |
return filename; | |
} | |
}); | |
Object.defineProperty(window, '__LINE__', { | |
get: function(){ | |
return __STACK__[1].getLineNumber(); | |
} | |
}); | |
Object.defineProperty(window, '__HERE__', { | |
get: function(){ | |
let filename = __STACK__[1].getFileName().replace(location.origin, "").replace(window.location.search, ""); | |
if(!filename) filename = "/"; | |
return filename + ":" + __STACK__[1].getLineNumber(); | |
} | |
}); | |
console.log("__FILE__", __FILE__); | |
console.log("__LINE__", __LINE__); | |
console.log("__HERE__", __HERE__); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajaxSetup({ | |
dataFilter: function(data) { | |
let filename = this.url.replace(location.origin, "").replace(/\?.*/, ""); | |
if(this.dataType === "script") return `__XHR_FILE__ = "${filename}";\n` + data; | |
else if(this.dataType === "html") return `<script class="init_script">__XHR_FILE__ = "${filename}";<\/script>` + data; | |
else return data; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment