From 278b03bd14ddad188000e87ec1f60f925f94697b Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 11:58:13 +0100 Subject: Global marks; find another (existing) tab if tabId has been removed. --- background_scripts/marks.coffee | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 5336e8da..05c8e579 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -14,7 +14,7 @@ Marks = url: sender.tab.url scrollX: req.scrollX scrollY: req.scrollY - console.log item + markName: req.markName chrome.storage.local.set item goto: (req, sender) -> @@ -32,25 +32,35 @@ Marks = @focusOrLaunch markInfo else # Check whether markInfo.tabId still exists. - { tabId, url, scrollX, scrollY } = markInfo - chrome.tabs.get tabId, (tab) => + chrome.tabs.get markInfo.tabId, (tab) => if chrome.runtime.lastError or not tab - # The tab no longer exists. + # The original tab no longer exists. @focusOrLaunch markInfo else # The original tab still exists. - chrome.tabs.update tabId, { selected: true }, -> - chrome.tabs.sendMessage tabId, - { name: "setScrollPosition", scrollX: scrollX, scrollY: scrollY }, -> - chrome.tabs.sendMessage tabId, - name: "showHUDforDuration", - text: "Jumped to global mark '#{req.markName}'." - duration: 1000 + @gotoPositionInTab markInfo + + gotoPositionInTab: ({ tabId, scrollX, scrollY, markName }) -> + chrome.tabs.update tabId, { selected: true }, -> + chrome.tabs.sendMessage tabId, + { name: "setScrollPosition", scrollX: scrollX, scrollY: scrollY }, -> + chrome.tabs.sendMessage tabId, + name: "showHUDforDuration", + text: "Jumped to global mark '#{markName}'." + duration: 1000 # The tab we're trying to find no longer exists. Either find another tab with a matching URL and use it, or # create a new tab. - focusOrLaunch: (info) -> - console.log info + focusOrLaunch: (markInfo) -> + chrome.windows.getAll { populate: true }, (windows) => + baseUrl = @getBaseUrl markInfo.url + for window in windows + for tab in window.tabs + if baseUrl == @getBaseUrl tab.url + # We have a matching tab. We'll use it. + return @gotoPositionInTab extend markInfo, tabId: tab.id + + getBaseUrl: (url) -> url.split("#")[0] root = exports ? window root.Marks = Marks -- cgit v1.2.3