Automatically reload html file using browser via terminal command

I'm trying to make some small html web content, and it would be incredibly useful to see current updates to the html file being viewed through the browser whenever I save the file. I know that there are probably IDEs that do this for you, and if you recommend them, please do it, but I'm looking to make a script that just opens the file in the browser, closing the file version it was already open, so I can continue to do all my programs in vim.

I know that I can use the following on Mac OSX to open a file in a new tab of my current browser:

open foo.html 

but if this happens in a time loop or every time I write (: w) in vim, my browser populates new tabs. Is there a way to close the old tab when opening a new tab? Or is there an even better approach to this that I have not considered? It would be very desirable if I could continue to use vim in the terminal.

Thanks in advance.

+6
source share
4 answers

If there is already a tab for foo.html , open foo.html should focus this tab in Safari. For Chrome, you can use something like this:

 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 just assigned โŒ˜R to open "$TM_FILEPATH" -a Safari in the text.html area of โ€‹โ€‹TextMate. I also turned on saving documents when switching to another application, so it basically performs the last three steps of the application update-update-transition cycle.

Other parameters:

+3
source

You can use AppleScript to reload the tab. See Benjie's answer for this question.
Use osascript to call AppleScript from a shell script. You will get something like this:

 osascript -e 'tell application "Google Chrome" to tell the active tab of its first window to reload' 

Alternatively, you can use something like the following to close all previous tabs:

 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 
+10
source

This may be redundant 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 is compiled to static pages locally, it's silly that you cannot dynamically include in jade), and make is really well integrated with vim. You can create rules for compiling, rebooting, clicking on the server, anything. The way I get current updates in the browser is to start the Node or python server to serve the html using an additional javascript observer, depending on what other technologies you use, there are many varieties.

0
source

You can get a browser that can receive commands from the socket, such as uzbl, luakit or dwb.

You can add this function (listening on a socket or even a file) with the extension (add-on, plug-in, regardless of what your favorite browser calls it).

You can insert some javascript on the page that you are editing, which will make it reload some signal that you can control from the CLI.

You can write a script that will kill the browser and open it again. Some browsers will try to load the cached version of the page. To do this, you better use the query string and create a unique URI each time (for example, open "foo.html?$(date +%s)" ).

The possibilities are endless!

0
source

Source: https://habr.com/ru/post/947347/


All Articles