Skip to content

Instantly share code, notes, and snippets.

@wallyqs
Created July 20, 2014 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wallyqs/c4bd294dd223385e370b to your computer and use it in GitHub Desktop.
Save wallyqs/c4bd294dd223385e370b to your computer and use it in GitHub Desktop.
Tangle and reload webapp via Guard and Google Chrome Dev tools websocket
require 'org-converge'
require 'uri'
require 'net/http'
require 'eventmachine'
require 'faye/websocket'
require 'json'
ORG_FILE = 'org/app.org'
WEBAPP_URL = 'localhost:8000'
CHROME_DEVTOOLS_URL = 'http://localhost:9222/json'
watch(ORG_FILE) do |m|
OrgConverge::Command.new({
'<org_file>' => ORG_FILE,
'--tangle' => true
}).execute!
begin
json = Net::HTTP.get(URI(CHROME_DEVTOOLS_URL))
chrome = JSON.parse(json)
rescue => e
puts "Could not retrieve debug session info from Google Chrome Dev tools: #{e}"
end
application_tab = chrome.find {|tab| tab['url'] =~ /#{WEBAPP_URL}/}
if application_tab.nil?
puts "Tab for the web app is not open at #{webapp_url}!"
return
end
websocket_debug_url = application_tab['webSocketDebuggerUrl']
# https://github.com/cyrus-and/chrome-remote-interface#chromesendmethod-params-callback
reload_tab_command = {
'id' => rand(1000),
'method' => 'Page.reload'
}.to_json
EM.run do
ws = Faye::WebSocket::Client.new(websocket_debug_url)
ws.onopen = lambda do |event|
puts "Reloading application via debug websocket running at #{ws.url}..."
ws.send(reload_tab_command)
end
ws.onmessage = lambda do |event|
event_info = JSON.parse(event.data)
if event_info['error']
puts "Could not reload application: #{event_info['error']}"
else
puts "Done reloading application!"
end
EM.stop
end
end
end
interactor :off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment