Created
July 16, 2014 07:48
-
-
Save Fasani/66bdfbc6ce7e3b360e9d to your computer and use it in GitHub Desktop.
Browser Auto Refresh Script - Ruby Watcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file has been edited for my own usage and shared here, credits to Brett Terpstra. | |
#!/usr/bin/env ruby | |
# watch.rb by Brett Terpstra, 2011 <http://brettterpstra.com> | |
# with credit to Carlo Zottmann <https://github.com/carlo/haml-sass-file-watcher> | |
# ============================= | |
# Code from: http://brettterpstra.com/2011/03/07/watch-for-file-changes-and-refresh-your-browser-automatically/ | |
# To install use: chmod a+x watcher.rb | |
# Run with: ruby watcher.rb <folder/to/watch> <keyword> | |
# eg: ruby watcher.rb ~/Projects lvh.me | |
trap("SIGINT") { exit } | |
if ARGV.length < 2 | |
puts "Usage: #{$0} watch_folder keyword" | |
puts "Example: #{$0} . mywebproject" | |
exit | |
end | |
dev_extension = 'dev' | |
filetypes = ['css','html','htm','php','rb','erb','less','js', 'haml'] | |
watch_folder = ARGV[0] | |
keyword = ARGV[1] | |
puts "Watching '#{watch_folder}' and subfolders for changes in project files..." | |
while true do | |
files = [] | |
filetypes.each {|type| | |
files += Dir.glob( File.join( watch_folder, "**", "*.#{type}" ) ) | |
} | |
new_hash = files.collect {|f| [ f, File.stat(f).mtime.to_i ] } | |
hash ||= new_hash | |
diff_hash = new_hash - hash | |
unless diff_hash.empty? | |
hash = new_hash | |
diff_hash.each do |df| | |
puts "Detected a change in '#{df[0]}', refreshing Google Chrome..." | |
%x{osascript<<ENDGAME | |
tell application "Google Chrome" | |
set windowList to every window | |
repeat with aWindow in windowList | |
set tabList to every tab of aWindow | |
repeat with atab in tabList | |
if (URL of atab contains "#{keyword}") then | |
tell atab to reload | |
end if | |
end repeat | |
end repeat | |
end tell | |
(* | |
### Start commented code | |
### This is support for other browsers | |
tell application "Safari" | |
set windowList to every window | |
repeat with aWindow in windowList | |
set tabList to every tab of aWindow | |
repeat with atab in tabList | |
if (URL of atab contains "#{keyword}") then | |
tell atab to do javascript "window.location.reload()" | |
end if | |
end repeat | |
end repeat | |
end tell | |
tell app "Firefox" to activate | |
tell app "System Events" | |
keystroke "r" using command down | |
end tell | |
### End commented code | |
*) | |
} | |
end | |
end | |
sleep 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment