[英]How I can write a function that will display the path in the Browser Console
[英]How can I open the editor when I click browser or nodejs console file path?
例如,在我的網站上,我使用console.log
打印絕對文件路徑,如下所示console.log('/user/myname/work/projectName/index.ts')
我想在單擊時在編輯器中打開文件chrome 中 devtool 中的消息。
如果我在 nodejs 中登錄它,我該如何做同樣的事情?
node index.js
console.log(__dirname)
當我單擊終端中的消息時,我想使用當前編輯器可能是 vscode 或 webtorm 等打開 __dirname 文件。
我能怎么做?
謝謝你的幫助!
在大多數情況下(大多數終端應用程序已經很長時間開箱即用地支持此功能)您需要做的就是在文件路徑之前添加正確的協議,以便記錄的路徑成為文件 URI .
更多關於文件 URI 的信息: https://en.wikipedia.org/wiki/File_URI_scheme
在您的情況下,您可以將file://
添加到路徑之前,它應該開箱即用,例如:
file:///Users/someuser/somefile.txt
請注意,我在示例中使用了標准的 Unix/MacOS 路徑名。 前兩個斜杠描述了協議( file://
),下一個來自文件系統中的根路徑,這在例如 Windows 環境中看起來會有所不同。
因此,如果您想使用__dirname
(請注意,您仍然可以向記錄的消息中添加其他文本,文件 URI 只是其中的一部分):
console.log(`I will be opening file://${__dirname}`);
這是由於終端仿真器中超鏈接的支持而起作用的。 如果您想更深入地研究該主題,請遵循此 GIST: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.