Auto-reload browser when I save changes to html file, in Chrome?

后端 未结 23 1950
半阙折子戏
半阙折子戏 2020-12-12 11:46

I\'m editing an HTML file in Vim and I want the browser to refresh whenever the file underneath changes.

Is there a plugin for Google Chrome that will listen for ch

相关标签:
23条回答
  • 2020-12-12 12:35

    If you have Node installed on your computer, then you can use light-server.

    Setp 1: Install light-server using command npm install -g light-server

    Step 2: While current working directory is the folder containing the static HTML page, start light-server using command npx light-server -s . -p 5000 -w "*.css # reloadcss" -w "*.html # reloadhtml" -w "*.js # reloadjs"

    Step 3: Open the web page in browser at http://localhost:5000


    In Step 2,

    Port can be changed using -p switch

    Files that are being watched can be changed using -w switch

    Server directory can be changed using -s switch

    Documentation for light-server is at https://www.npmjs.com/package/light-server

    0 讨论(0)
  • 2020-12-12 12:36

    Update: Tincr is dead.

    Tincr is a Chrome extension that will refresh the page whenever the file underneath changes.

    0 讨论(0)
  • 2020-12-12 12:37

    If you are on GNU/Linux, you can use a pretty cool browser called Falkon. It's based on the Qt WebEngine. It's just like Firefox or Chromium - except, it auto refreshes the page when a file is updated. The auto refresh doesn't matter much whether you use vim, nano, or atom, vscode, brackets, geany, mousepad etc.

    On Arch Linux, you can install Falkon pretty easily:

    sudo pacman -S falkon
    

    Here's the snap package.

    0 讨论(0)
  • 2020-12-12 12:40

    This works for me (in Ubuntu):

    #!/bin/bash
    #
    # Watches the folder or files passed as arguments to the script and when it
    # detects a change it automatically refreshes the current selected Chrome tab or
    # window.
    #
    # Usage:
    # ./chrome-refresher.sh /folder/to/watch
    
    TIME_FORMAT='%F %H:%M'
    OUTPUT_FORMAT='%T Event(s): %e fired for file: %w. Refreshing.'
    
    while inotifywait --exclude '.+\.swp$' -e modify -q \
        -r --timefmt "${TIME_FORMAT}" --format "${OUTPUT_FORMAT}" "$@"; do
        xdotool search --onlyvisible --class chromium windowactivate --sync key F5 \
        search --onlyvisible --class gnome-terminal windowactivate
    done
    

    You may need to install inotify and xdotool packages (sudo apt-get install inotify-tools xdotool in Ubuntu) and to change args of --class to the actual names of your preferred browser and terminal.

    Start the script as described and just open index.html in a browser. After each save in vim the script will focus your browser's window, refresh it, and then return to the terminal.

    0 讨论(0)
  • 2020-12-12 12:41

    Add this to your HTML

    <script> window.addEventListener('focus', ()=>{document.location = document.location})</script>
    

    While you are editing, your browser page is blurred, by switching back to look at it, the focus event is fired and the page reloads.

    0 讨论(0)
提交回复
热议问题