Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chrome - use tab's current host rather than 127.0.0.1 #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/global-chrome.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@ ToggleCommand =
chrome.browserAction.setTitle { tabId, title: status.buttonToolTip }
chrome.browserAction.setIcon { tabId, path: status.buttonIcon }

getHost = (url) ->
matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i)
domain = matches && matches[1]
domain.split(':')[0]

chrome.browserAction.onClicked.addListener (tab) ->
LiveReloadGlobal.toggle(tab.id)
host = getHost(tab.url)
LiveReloadGlobal.toggle(tab.id, host)
ToggleCommand.update(tab.id)

chrome.tabs.onSelectionChanged.addListener (tabId, selectInfo) ->
Expand All @@ -32,7 +37,8 @@ chrome.extension.onRequest.addListener ([eventName, data], sender, sendResponse)
# console.log "#{eventName}(#{JSON.stringify(data)})"
switch eventName
when 'status'
LiveReloadGlobal.updateStatus(sender.tab.id, data)
host = getHost(sender.tab.url)
LiveReloadGlobal.updateStatus(sender.tab.id, data, host)
ToggleCommand.update(sender.tab.id)
else
LiveReloadGlobal.received(eventName, data)
33 changes: 18 additions & 15 deletions src/global.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ Status =


class TabState
constructor: (@tab) ->
constructor: (@tab, @host) ->
@enabled = no
@active = no

enable: ->
@send 'enable', { @useFallback, scriptURI: @bundledScriptURI(), host: LiveReloadGlobal.host, port: LiveReloadGlobal.port }
enable: (host) ->
@host = @host || LiveReloadGlobal.host
@send 'enable', { @useFallback, scriptURI: @bundledScriptURI(), host: @host, port: LiveReloadGlobal.port }

disable: ->
@send 'disable'
Expand Down Expand Up @@ -99,19 +100,19 @@ LiveReloadGlobal =
return
return

findState: (tab, create=no) ->
findState: (tab, create=no, host=no) ->
for tabState in @_tabs
return tabState if tabState.tab is tab
if create
state = new TabState(tab)
state = new TabState(tab, host)
@_tabs.push state
state
else
null

toggle: (tab) ->
toggle: (tab, host) ->
if @isAvailable(tab)
state = @findState(tab, yes)
state = @findState(tab, yes, host)
if state.enabled
state.disable()
unless @areAnyTabsEnabled()
Expand All @@ -121,29 +122,31 @@ LiveReloadGlobal =
state.useFallback = @useFallback
state.enable()
else
@beforeEnablingFirst (err) =>
@beforeEnablingFirst((err) =>
if err
switch err
when 'cannot-connect' then state.alert(CannotConnectAlert)
when 'cannot-download' then state.alert("Cannot download livereload.js")
else
state.useFallback = @useFallback
state.enable()
state.enable(host)
host)

tabStatus: (tab) ->
unless @isAvailable(tab)
return Status.unavailable
@findState(tab)?.status() || Status.disabled

updateStatus: (tab, status) ->
@findState(tab, yes).updateStatus(status)
updateStatus: (tab, status, host) ->
@findState(tab, yes, host).updateStatus(status)

areAnyTabsEnabled: ->
return yes for tabState in @_tabs when tabState.enabled
no

beforeEnablingFirst: (callback) ->
beforeEnablingFirst: (callback, host = no) ->
@useFallback = no
host = host || @host

# probe using web sockets
callbackCalled = no
Expand All @@ -153,8 +156,8 @@ LiveReloadGlobal =
ws.close()
timeout = setTimeout(failOnTimeout, 1000)

console.log "Connecting to ws://#{@host}:#{@port}/livereload..."
ws = new TheWebSocket("ws://#{@host}:#{@port}/livereload")
console.log "Connecting to ws://#{host}:#{@port}/livereload..."
ws = new TheWebSocket("ws://#{host}:#{@port}/livereload")
ws.onerror = =>
console.log "Web socket error."
callback('cannot-connect') unless callbackCalled
Expand Down Expand Up @@ -186,7 +189,7 @@ LiveReloadGlobal =
xhr.onerror = (event) =>
callback('cannot-download') unless callbackCalled
callbackCalled = yes
xhr.open("GET", "http://#{@host}:#{@port}/livereload.js", true)
xhr.open("GET", "http://#{host}:#{@port}/livereload.js", true)
xhr.send(null)


Expand Down