bash 通过终端命令使用浏览器自动重新加载 html 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17119184/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
auto-reload html file with a browser via terminal command
提问by StrugglingProgrammer
I am trying to do some small html web stuff, and would find it incredibly useful to be able to see live updates of the html file viewed through a browser whenever I save my file. I know there are probably IDEs out there that do this for you, and if you recommend any, please do, but I am looking to make a script that will just open the file in the browser, while closing the version of the file that was already open, so that I can continue to do all my programming in vim.
我正在尝试做一些小的 html 网络内容,并且会发现每当我保存文件时能够看到通过浏览器查看的 html 文件的实时更新非常有用。我知道那里可能有 IDE 可以为您执行此操作,如果您推荐任何 IDE,请这样做,但我正在寻找一个脚本,该脚本只会在浏览器中打开文件,同时关闭文件的版本已经打开,以便我可以继续在 vim 中进行所有编程。
I know that I can use the following in Mac OSX, to open the file into a new tab of my current browser:
我知道我可以在 Mac OSX 中使用以下命令将文件打开到我当前浏览器的新选项卡中:
open foo.html
but, if I have this occurring on a timed loop, or every time I write (:w) in vim, my browser will fill up with new tabs. Is there a way to close the old tab when opening the new tab? Or is there an even better approach to this that I have not considered? It would be highly preferred if I could continue to use vim in terminal.
但是,如果我在定时循环中发生这种情况,或者每次我在 vim 中编写 (:w) 时,我的浏览器都会填满新标签。有没有办法在打开新标签时关闭旧标签?或者有没有我没有考虑过的更好的方法?如果我可以继续在终端中使用 vim,那将是非常受欢迎的。
Thanks in advance.
提前致谢。
采纳答案by Lri
If there is already a tab for foo.html, open foo.htmlshould focus that tab in Safari. For Chrome, you might use something like this:
如果已经有一个选项卡foo.html,open foo.html则应在 Safari 中聚焦该选项卡。对于 Chrome,您可能会使用以下内容:
set u to "http://t.co/"
tell application "Google Chrome"
repeat with w in windows
set i to 0
repeat with t in tabs of w
set i to i + 1
if URL of t is u then
set active tab index of w to i
set index of w to 1
tell t to reload
activate
return
end if
end repeat
end repeat
open location u
activate
end tell
I have just assigned ?R to open "$TM_FILEPATH" -a Safariin the text.htmlscope in TextMate. I have also enabled saving documents when switching to another application, so it basically does the last three steps of the edit-save-switch application-refresh cycle.
我刚刚在 TextMateopen "$TM_FILEPATH" -a Safari的text.html范围内分配了 ?R 。我还启用了在切换到另一个应用程序时保存文档,因此它基本上完成了编辑-保存-切换应用程序-刷新循环的最后三个步骤。
Other options:
其他选项:
回答by cody
You can use AppleScript to reload the tab. See the Benjie's answer for this question
Use osascriptto call the AppleScript from shell script. You'll get something like this:
您可以使用 AppleScript 重新加载选项卡。请参阅 Benjie对此问题的回答
Use osascriptto call the AppleScript from shell script。你会得到这样的东西:
osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload'
Alternatively you can use something like next to close all previous tabs:
或者,您可以使用类似 next 来关闭所有以前的选项卡:
tell application "Google Chrome"
set windowList to every tab of every window whose URL starts with "http://stackoverflow.com"
repeat with tabList in windowList
repeat with thisTab in tabList
close thisTab
end repeat
end repeat
end tell
回答by Lochlan
It may be overkill for your application, but I use make files for all my HTML projects. I only make static sites for myself, but I use Less and Jade (sometimes php compiled to static pages locally, it is silly that you can't dynamically include with jade) and make is really well integrated with vim. You can make rules for compiling, reloading, pushing to server, whatever. The way I get live updates to the browser is to start a Node or python server to serve the html with an extra javascript 'watcher' there are lots of varieties, depending on what other technologies you are using.
这对您的应用程序来说可能有点矫枉过正,但我对我所有的 HTML 项目都使用了 make 文件。我只为自己制作静态网站,但我使用 Less 和 Jade(有时 php 在本地编译为静态页面,你不能用 jade 动态包含它是愚蠢的)并且 make 与 vim 集成得非常好。您可以制定编译、重新加载、推送到服务器等的规则。我获得浏览器实时更新的方法是启动一个 Node 或 python 服务器,以使用额外的 javascript“观察者”来提供 html 有很多种类,具体取决于您使用的其他技术。
回答by qwertyboy
You can get yourself a browser that can receive commands from a socket, like uzbl, luakit or dwb.
您可以为自己准备一个可以从套接字接收命令的浏览器,例如 uzbl、luakit 或 dwb。
You can add this functionality (listening to a socket, or even a file) with an extension (addon, plugin, whatever your favorite browser calls it).
您可以使用扩展名(插件、插件,无论您喜欢的浏览器如何称呼它)添加此功能(侦听套接字,甚至是文件)。
You can insert some javascript to the page you are editing, that will make it reload upon some signal you can control from the CLI.
您可以在您正在编辑的页面中插入一些 javascript,这将使其根据您可以从 CLI 控制的某些信号重新加载。
You can write a script that will kill the browser and reopen it. Some browsers will try to load a cached version of the page. For these you better use the query string and create a unique URI each time (e.g. open "foo.html?$(date +%s)").
您可以编写一个脚本来杀死浏览器并重新打开它。某些浏览器会尝试加载页面的缓存版本。对于这些,您最好使用查询字符串并每次都创建一个唯一的 URI(例如open "foo.html?$(date +%s)")。
The possibilities are endless!
可能性是无止境!

