有没有办法使用 console.print 语句在 devtools 控制台中将 link 打印到 javascript 源文件和行号?
Is there a way to print a link to a javascript source file and line number in the devtools console with a console.print statement?
我有一些 console.log 打印是在特定 javascript 文件中触发的。该文件知道其他文件,并在 console.log 打印中引用这些文件名。我想知道是否有一种方法可以在相同的 console.log 打印输出中将某种 link 打印到该源文件中,希望的行为是单击 link 将打开源代码在开发工具中查看该文件?
在回答这个问题时,没有直接的方法可以做到这一点。
但是,@Emissary 在下面的评论中指出,访问 link 文件的解决方法是利用 Error 对象的能力。
console.log(new Error().stack)
通过尝试@miir 的解决方案发现您可以:
1) Link 到一个文件只要你把完整路径
console.log("http://localhost:8000/myapp/path/to/file.js")
console.log(document.currentScript.src); // if executed from within a script
2) Link 手动到一个文件+行+字符
const line = "83";
const character = "2";
console.log(document.currentScript.src + ":" + line + ":" + character);
3) Link 到文件 + 行 + 字符动态
const level = 1; // 1 is last stack trace, you can go up the calling functions
console.log(new Error().stack.split("\n")[level].match(/http[^\)]+/)[0]);
没有什么不能从接受的答案中找出来的,但也许它可以帮助新人。
我有一些 console.log 打印是在特定 javascript 文件中触发的。该文件知道其他文件,并在 console.log 打印中引用这些文件名。我想知道是否有一种方法可以在相同的 console.log 打印输出中将某种 link 打印到该源文件中,希望的行为是单击 link 将打开源代码在开发工具中查看该文件?
在回答这个问题时,没有直接的方法可以做到这一点。
但是,@Emissary 在下面的评论中指出,访问 link 文件的解决方法是利用 Error 对象的能力。
console.log(new Error().stack)
通过尝试@miir 的解决方案发现您可以:
1) Link 到一个文件只要你把完整路径
console.log("http://localhost:8000/myapp/path/to/file.js")
console.log(document.currentScript.src); // if executed from within a script
2) Link 手动到一个文件+行+字符
const line = "83";
const character = "2";
console.log(document.currentScript.src + ":" + line + ":" + character);
3) Link 到文件 + 行 + 字符动态
const level = 1; // 1 is last stack trace, you can go up the calling functions
console.log(new Error().stack.split("\n")[level].match(/http[^\)]+/)[0]);
没有什么不能从接受的答案中找出来的,但也许它可以帮助新人。