From 49dfa9b7e278c5ec2f101b6e2de8fdb590ee38db Mon Sep 17 00:00:00 2001 From: Greg Allen Date: Wed, 13 Mar 2013 20:03:15 -0700 Subject: [PATCH 1/2] chrome - use tab's current host rather than 127.0.0.1 --- src/global-chrome.coffee | 5 ++++- src/global.coffee | 21 ++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/global-chrome.coffee b/src/global-chrome.coffee index a8c1dbd..50944d0 100644 --- a/src/global-chrome.coffee +++ b/src/global-chrome.coffee @@ -18,7 +18,10 @@ ToggleCommand = chrome.browserAction.onClicked.addListener (tab) -> - LiveReloadGlobal.toggle(tab.id) + matches = tab.url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i) + domain = matches && matches[1] + domain = domain.split(':')[0] + LiveReloadGlobal.toggle(tab.id, domain) ToggleCommand.update(tab.id) chrome.tabs.onSelectionChanged.addListener (tabId, selectInfo) -> diff --git a/src/global.coffee b/src/global.coffee index faac42e..80cd6dd 100644 --- a/src/global.coffee +++ b/src/global.coffee @@ -27,8 +27,9 @@ class TabState @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' @@ -109,7 +110,7 @@ LiveReloadGlobal = else null - toggle: (tab) -> + toggle: (tab, host) -> if @isAvailable(tab) state = @findState(tab, yes) if state.enabled @@ -121,14 +122,15 @@ 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) @@ -142,8 +144,9 @@ LiveReloadGlobal = 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 @@ -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 @@ -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) From 67e6c7a55fd8f533b7ed71a132d46dd9898b5745 Mon Sep 17 00:00:00 2001 From: Greg Allen Date: Thu, 14 Mar 2013 17:42:32 -0700 Subject: [PATCH 2/2] fixed issue when refreshing page --- src/global-chrome.coffee | 13 ++++++++----- src/global.coffee | 16 ++++++++-------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/global-chrome.coffee b/src/global-chrome.coffee index 50944d0..adf450a 100644 --- a/src/global-chrome.coffee +++ b/src/global-chrome.coffee @@ -16,12 +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) -> - matches = tab.url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i) - domain = matches && matches[1] - domain = domain.split(':')[0] - LiveReloadGlobal.toggle(tab.id, domain) + host = getHost(tab.url) + LiveReloadGlobal.toggle(tab.id, host) ToggleCommand.update(tab.id) chrome.tabs.onSelectionChanged.addListener (tabId, selectInfo) -> @@ -35,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) diff --git a/src/global.coffee b/src/global.coffee index 80cd6dd..3bd8a05 100644 --- a/src/global.coffee +++ b/src/global.coffee @@ -23,13 +23,13 @@ Status = class TabState - constructor: (@tab) -> + constructor: (@tab, @host) -> @enabled = no @active = no enable: (host) -> - host = host || LiveReloadGlobal.host - @send 'enable', { @useFallback, scriptURI: @bundledScriptURI(), host: host, port: LiveReloadGlobal.port } + @host = @host || LiveReloadGlobal.host + @send 'enable', { @useFallback, scriptURI: @bundledScriptURI(), host: @host, port: LiveReloadGlobal.port } disable: -> @send 'disable' @@ -100,11 +100,11 @@ 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 @@ -112,7 +112,7 @@ LiveReloadGlobal = toggle: (tab, host) -> if @isAvailable(tab) - state = @findState(tab, yes) + state = @findState(tab, yes, host) if state.enabled state.disable() unless @areAnyTabsEnabled() @@ -137,8 +137,8 @@ LiveReloadGlobal = 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