From c567d7d043b689d72eabbd671eab8ae8805dadc1 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Wed, 3 Jun 2015 11:23:29 +0100 Subject: Fix marks (incl. global marks)... Fixes #1712: - Make global marks work. - Add mode indicator. - Don't fail for global marks on background page if mark is not set. - Give HUD warning for global marks if global mark is not set. (The diff is big but, which the exception of infrastructure refactoring, the main change is to not exit on , thereby fixing #1712). --- background_scripts/marks.coffee | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 15d41205..db9c4ae2 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -23,12 +23,18 @@ removeMarksForTab = (id) -> root.goto = (req, sender) -> mark = marks[req.markName] - chrome.tabs.update mark.tabId, selected: true - chrome.tabs.sendMessage mark.tabId, - name: "setScrollPosition" - scrollX: mark.scrollX - scrollY: mark.scrollY - chrome.tabs.sendMessage mark.tabId, - name: "showHUDforDuration", - text: "Jumped to global mark '#{req.markName}'" - duration: 1000 + if mark? + chrome.tabs.update mark.tabId, selected: true + chrome.tabs.sendMessage mark.tabId, + name: "setScrollPosition" + scrollX: mark.scrollX + scrollY: mark.scrollY + chrome.tabs.sendMessage mark.tabId, + name: "showHUDforDuration", + text: "Jumped to global mark '#{req.markName}'." + duration: 1000 + else + chrome.tabs.sendMessage sender.tab.id, + name: "showHUDforDuration", + text: "Global mark not set: '#{req.markName}'." + duration: 1000 -- cgit v1.2.3 From 931d246e59389bd3070aa03bde4fada9f2ef9700 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 11:40:55 +0100 Subject: Global marks; move marks to chrome.storage. --- background_scripts/marks.coffee | 84 ++++++++++++++++++++++++----------------- 1 file changed, 50 insertions(+), 34 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index db9c4ae2..5336e8da 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -1,40 +1,56 @@ -root = window.Marks = {} -marks = {} +Marks = + marks: {} -root.create = (req, sender) -> - marks[req.markName] = - tabId: sender.tab.id - scrollX: req.scrollX - scrollY: req.scrollY + # This returns the key which is used for storing mark locations in chrome.storage.local. + getLocationKey: (markName) -> "vimiumGlobalMark|#{markName}" -chrome.tabs.onUpdated.addListener (tabId, changeInfo, tab) -> - if changeInfo.url? - removeMarksForTab tabId + create: (req, sender) -> + chrome.storage.local.get "vimiumSecret", (items) => + item = {} + item[@getLocationKey req.markName] = + vimiumSecret: items.vimiumSecret + tabId: sender.tab.id + url: sender.tab.url + scrollX: req.scrollX + scrollY: req.scrollY + console.log item + chrome.storage.local.set item -chrome.tabs.onRemoved.addListener (tabId, removeInfo) -> - # XXX(jez): what about restored tabs? - removeMarksForTab tabId + goto: (req, sender) -> + key = @getLocationKey req.markName + chrome.storage.local.get [ "vimiumSecret", key ], (items) => + markInfo = items[key] + if not markInfo + # The mark is not defined. + chrome.tabs.sendMessage sender.tab.id, + name: "showHUDforDuration", + text: "Global mark not set: '#{req.markName}'." + duration: 1000 + else if markInfo.vimiumSecret != items.vimiumSecret + # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date. + @focusOrLaunch markInfo + else + # Check whether markInfo.tabId still exists. + { tabId, url, scrollX, scrollY } = markInfo + chrome.tabs.get tabId, (tab) => + if chrome.runtime.lastError or not tab + # The 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 -removeMarksForTab = (id) -> - for markName, mark of marks - if mark.tabId is id - delete marks[markName] + # 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 -root.goto = (req, sender) -> - mark = marks[req.markName] - if mark? - chrome.tabs.update mark.tabId, selected: true - chrome.tabs.sendMessage mark.tabId, - name: "setScrollPosition" - scrollX: mark.scrollX - scrollY: mark.scrollY - chrome.tabs.sendMessage mark.tabId, - name: "showHUDforDuration", - text: "Jumped to global mark '#{req.markName}'." - duration: 1000 - else - chrome.tabs.sendMessage sender.tab.id, - name: "showHUDforDuration", - text: "Global mark not set: '#{req.markName}'." - duration: 1000 +root = exports ? window +root.Marks = Marks -- cgit v1.2.3 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 From cb3d18a8068063050dd25f0db03777b564f1a548 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 12:16:06 +0100 Subject: Global marks; create a new tab, if necessary. --- background_scripts/marks.coffee | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 05c8e579..94633c36 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -59,6 +59,11 @@ Marks = if baseUrl == @getBaseUrl tab.url # We have a matching tab. We'll use it. return @gotoPositionInTab extend markInfo, tabId: tab.id + # There is no existing matching tab, we'll have to create one. + chrome.tabs.create { url: @getBaseUrl(markInfo.url) }, (tab) => + # Note. tabLoadedHandlers is defined in "main.coffee". This handler will be called when the tab has + # is loaded, its DOM is ready and it registers with the background page. + tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id getBaseUrl: (url) -> url.split("#")[0] -- cgit v1.2.3 From 8e93f5bc28709b307f233f2326093ca47e0e4aba Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 12:35:31 +0100 Subject: Global marks; better comments and minor refactoring. --- background_scripts/marks.coffee | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 94633c36..c6c76dbb 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -1,17 +1,21 @@ Marks = - marks: {} - # This returns the key which is used for storing mark locations in chrome.storage.local. getLocationKey: (markName) -> "vimiumGlobalMark|#{markName}" + # Get the part of a URL we use for matching here (that is, everything up to the first anchor). + getBaseUrl: (url) -> url.split("#")[0] + + # Create a global mark. We record vimiumSecret with the mark so that we can tell later, when the mark is + # used, whether this is the original Vimium instantiation or a subsequent instantiation. This affects + # whether or not tabId can be considered valid. create: (req, sender) -> chrome.storage.local.get "vimiumSecret", (items) => item = {} item[@getLocationKey req.markName] = vimiumSecret: items.vimiumSecret tabId: sender.tab.id - url: sender.tab.url + url: @getBaseUrl sender.tab.url scrollX: req.scrollX scrollY: req.scrollY markName: req.markName @@ -31,14 +35,15 @@ Marks = # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date. @focusOrLaunch markInfo else - # Check whether markInfo.tabId still exists. + # Check whether markInfo.tabId still exists. According to here (https://developer.chrome.com/extensions/tabs), + # tab Ids are unqiue within a Chrome session. So, if we find a match, we can use. chrome.tabs.get markInfo.tabId, (tab) => - if chrome.runtime.lastError or not tab - # The original tab no longer exists. - @focusOrLaunch markInfo - else + if not chrome.runtime.lastError and tab?.url and markInfo.url == @getBaseUrl tab.url # The original tab still exists. @gotoPositionInTab markInfo + else + # The original tab no longer exists. + @focusOrLaunch markInfo gotoPositionInTab: ({ tabId, scrollX, scrollY, markName }) -> chrome.tabs.update tabId, { selected: true }, -> @@ -49,23 +54,21 @@ Marks = 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. + # The tab we're trying to find no longer exists. We either find another tab with a matching URL and use it, + # or we create a new tab. 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 + if markInfo.url == @getBaseUrl tab.url + # We have a matching tab: use it. + @gotoPositionInTab extend markInfo, tabId: tab.id + return # There is no existing matching tab, we'll have to create one. chrome.tabs.create { url: @getBaseUrl(markInfo.url) }, (tab) => - # Note. tabLoadedHandlers is defined in "main.coffee". This handler will be called when the tab has + # Note. tabLoadedHandlers is defined in "main.coffee". The handler below will be called when the tab # is loaded, its DOM is ready and it registers with the background page. tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id - getBaseUrl: (url) -> url.split("#")[0] - root = exports ? window root.Marks = Marks -- cgit v1.2.3 From a0b56c86461f9d40a839bc0a5cafb12e6a0e32ba Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 12:40:19 +0100 Subject: Global marks; move marks to chrome.storage.sync. --- background_scripts/marks.coffee | 50 +++++++++++++++++++++-------------------- 1 file changed, 26 insertions(+), 24 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index c6c76dbb..ab39883b 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -1,6 +1,6 @@ Marks = - # This returns the key which is used for storing mark locations in chrome.storage.local. + # This returns the key which is used for storing mark locations in chrome.storage.sync. getLocationKey: (markName) -> "vimiumGlobalMark|#{markName}" # Get the part of a URL we use for matching here (that is, everything up to the first anchor). @@ -19,31 +19,33 @@ Marks = scrollX: req.scrollX scrollY: req.scrollY markName: req.markName - chrome.storage.local.set item + chrome.storage.sync.set item goto: (req, sender) -> - key = @getLocationKey req.markName - chrome.storage.local.get [ "vimiumSecret", key ], (items) => - markInfo = items[key] - if not markInfo - # The mark is not defined. - chrome.tabs.sendMessage sender.tab.id, - name: "showHUDforDuration", - text: "Global mark not set: '#{req.markName}'." - duration: 1000 - else if markInfo.vimiumSecret != items.vimiumSecret - # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date. - @focusOrLaunch markInfo - else - # Check whether markInfo.tabId still exists. According to here (https://developer.chrome.com/extensions/tabs), - # tab Ids are unqiue within a Chrome session. So, if we find a match, we can use. - chrome.tabs.get markInfo.tabId, (tab) => - if not chrome.runtime.lastError and tab?.url and markInfo.url == @getBaseUrl tab.url - # The original tab still exists. - @gotoPositionInTab markInfo - else - # The original tab no longer exists. - @focusOrLaunch markInfo + chrome.storage.local.get "vimiumSecret", (items) => + vimiumSecret = items.vimiumSecret + key = @getLocationKey req.markName + chrome.storage.sync.get key, (items) => + markInfo = items[key] + if not markInfo + # The mark is not defined. + chrome.tabs.sendMessage sender.tab.id, + name: "showHUDforDuration", + text: "Global mark not set: '#{req.markName}'." + duration: 1000 + else if markInfo.vimiumSecret != items.vimiumSecret + # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date. + @focusOrLaunch markInfo + else + # Check whether markInfo.tabId still exists. According to here (https://developer.chrome.com/extensions/tabs), + # tab Ids are unqiue within a Chrome session. So, if we find a match, we can use. + chrome.tabs.get markInfo.tabId, (tab) => + if not chrome.runtime.lastError and tab?.url and markInfo.url == @getBaseUrl tab.url + # The original tab still exists. + @gotoPositionInTab markInfo + else + # The original tab no longer exists. + @focusOrLaunch markInfo gotoPositionInTab: ({ tabId, scrollX, scrollY, markName }) -> chrome.tabs.update tabId, { selected: true }, -> -- cgit v1.2.3 From bd7671f6289eb35d5c2d4c2dca43d0a3c7a75c13 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 12:45:39 +0100 Subject: Global marks; better comments and fix typo. --- background_scripts/marks.coffee | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index ab39883b..f599cfe3 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -7,20 +7,23 @@ Marks = getBaseUrl: (url) -> url.split("#")[0] # Create a global mark. We record vimiumSecret with the mark so that we can tell later, when the mark is - # used, whether this is the original Vimium instantiation or a subsequent instantiation. This affects - # whether or not tabId can be considered valid. + # used, whether this is the original Vimium session or a subsequent session. This affects whether or not + # tabId can be considered valid. create: (req, sender) -> chrome.storage.local.get "vimiumSecret", (items) => item = {} item[@getLocationKey req.markName] = vimiumSecret: items.vimiumSecret - tabId: sender.tab.id + markName: req.markName url: @getBaseUrl sender.tab.url + tabId: sender.tab.id scrollX: req.scrollX scrollY: req.scrollY - markName: req.markName chrome.storage.sync.set item + # Goto a global mark. We try to find the original tab. If we can't find that, then we try to find another + # tab with the original URL, and use that. And if we can't find such an existing tab, then we create a new + # one. Whichever of those we do, we then set the scroll position to the original scroll position. goto: (req, sender) -> chrome.storage.local.get "vimiumSecret", (items) => vimiumSecret = items.vimiumSecret @@ -33,12 +36,12 @@ Marks = name: "showHUDforDuration", text: "Global mark not set: '#{req.markName}'." duration: 1000 - else if markInfo.vimiumSecret != items.vimiumSecret + else if markInfo.vimiumSecret != vimiumSecret # This is a different Vimium instantiation, so markInfo.tabId is definitely out of date. @focusOrLaunch markInfo else # Check whether markInfo.tabId still exists. According to here (https://developer.chrome.com/extensions/tabs), - # tab Ids are unqiue within a Chrome session. So, if we find a match, we can use. + # tab Ids are unqiue within a Chrome session. So, if we find a match, we can use it. chrome.tabs.get markInfo.tabId, (tab) => if not chrome.runtime.lastError and tab?.url and markInfo.url == @getBaseUrl tab.url # The original tab still exists. @@ -47,6 +50,7 @@ Marks = # The original tab no longer exists. @focusOrLaunch markInfo + # Focus an existing tab and scroll to the given position within it. gotoPositionInTab: ({ tabId, scrollX, scrollY, markName }) -> chrome.tabs.update tabId, { selected: true }, -> chrome.tabs.sendMessage tabId, @@ -67,7 +71,7 @@ Marks = @gotoPositionInTab extend markInfo, tabId: tab.id return # There is no existing matching tab, we'll have to create one. - chrome.tabs.create { url: @getBaseUrl(markInfo.url) }, (tab) => + chrome.tabs.create { url: @getBaseUrl markInfo.url }, (tab) => # Note. tabLoadedHandlers is defined in "main.coffee". The handler below will be called when the tab # is loaded, its DOM is ready and it registers with the background page. tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id -- cgit v1.2.3 From 5901968be865588c2444055f8e63916a22c01156 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 13:44:46 +0100 Subject: Global marks; use chrome.tabs.query. (As suggested by @mrmr1993 in #1716.) --- background_scripts/marks.coffee | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index f599cfe3..6692dc6f 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -63,18 +63,16 @@ Marks = # The tab we're trying to find no longer exists. We either find another tab with a matching URL and use it, # or we create a new tab. focusOrLaunch: (markInfo) -> - chrome.windows.getAll { populate: true }, (windows) => - for window in windows - for tab in window.tabs - if markInfo.url == @getBaseUrl tab.url - # We have a matching tab: use it. - @gotoPositionInTab extend markInfo, tabId: tab.id - return - # There is no existing matching tab, we'll have to create one. - chrome.tabs.create { url: @getBaseUrl markInfo.url }, (tab) => - # Note. tabLoadedHandlers is defined in "main.coffee". The handler below will be called when the tab - # is loaded, its DOM is ready and it registers with the background page. - tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id + chrome.tabs.query { url: markInfo.url }, (tabs) => + if 0 < tabs.length + # We have a matching tab: use it. + @gotoPositionInTab extend markInfo, tabId: tabs[0].id + else + # There is no existing matching tab, we'll have to create one. + chrome.tabs.create { url: @getBaseUrl markInfo.url }, (tab) => + # Note. tabLoadedHandlers is defined in "main.coffee". The handler below will be called when the tab + # is loaded, its DOM is ready and it registers with the background page. + tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id root = exports ? window root.Marks = Marks -- cgit v1.2.3 From fea6e507761e6870d9c240d0718cb96711891a3a Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 14:57:24 +0100 Subject: Global marks; prefer to re-use a tab in the current window. --- background_scripts/marks.coffee | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 6692dc6f..45697ac4 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -65,8 +65,9 @@ Marks = focusOrLaunch: (markInfo) -> chrome.tabs.query { url: markInfo.url }, (tabs) => if 0 < tabs.length - # We have a matching tab: use it. - @gotoPositionInTab extend markInfo, tabId: tabs[0].id + # We have a matching tab: use it (prefering, if there are more than one, one in the current window). + @pickTabInWindow tabs, (tab) => + @gotoPositionInTab extend markInfo, tabId: tab.id else # There is no existing matching tab, we'll have to create one. chrome.tabs.create { url: @getBaseUrl markInfo.url }, (tab) => @@ -74,5 +75,12 @@ Marks = # is loaded, its DOM is ready and it registers with the background page. tabLoadedHandlers[tab.id] = => @gotoPositionInTab extend markInfo, tabId: tab.id + # Given a list of tabs, pick one in the current window, if possible, otherwise just pick any. + pickTabInWindow: (tabs, continuation) -> + chrome.windows.getCurrent ({ id }) -> + tabsInWindow = tabs.filter (tab) -> tab.windowId == id + continuation tabsInWindow[0] ? tabs[0] + + root = exports ? window root.Marks = Marks -- cgit v1.2.3 From bc4f3ecac81fd8f174b8c3ad92a0998ada8f7992 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Thu, 4 Jun 2015 15:14:10 +0100 Subject: Global marks; focus and only scroll in the main frame. --- background_scripts/marks.coffee | 1 - 1 file changed, 1 deletion(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 45697ac4..3a5af130 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -81,6 +81,5 @@ Marks = tabsInWindow = tabs.filter (tab) -> tab.windowId == id continuation tabsInWindow[0] ? tabs[0] - root = exports ? window root.Marks = Marks -- cgit v1.2.3 From 8eda31fb236b9506d577a25908d4e5334d3289d5 Mon Sep 17 00:00:00 2001 From: Stephen Blott Date: Fri, 5 Jun 2015 07:07:02 +0100 Subject: Global marks; global marks are always recorded for the top frame only. With global marks, we may later create a new tab when the mark is used. When doing so, we should always be using the URL and scroll position of the top frame within the tab. For example, if a global mark is created from within the Hangouts frame of Google's Inbox and the mark is later used, then we should create a new tab for Inbox's URL and scroll position, not for the Hangout's URL and scroll position. --- background_scripts/marks.coffee | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'background_scripts/marks.coffee') diff --git a/background_scripts/marks.coffee b/background_scripts/marks.coffee index 3a5af130..6e5f08ba 100644 --- a/background_scripts/marks.coffee +++ b/background_scripts/marks.coffee @@ -11,15 +11,27 @@ Marks = # tabId can be considered valid. create: (req, sender) -> chrome.storage.local.get "vimiumSecret", (items) => - item = {} - item[@getLocationKey req.markName] = + markInfo = vimiumSecret: items.vimiumSecret markName: req.markName url: @getBaseUrl sender.tab.url tabId: sender.tab.id scrollX: req.scrollX scrollY: req.scrollY - chrome.storage.sync.set item + + if markInfo.scrollX? and markInfo.scrollY? + @saveMark markInfo + else + # The front-end frame hasn't provided the scroll position (because it's not the top frame within its + # tab). We need to ask the top frame what its scroll position is. (With the frame Id set to 0, below, + # the request will only be handled by the top frame within the tab.) + chrome.tabs.sendMessage sender.tab.id, name: "getScrollPosition", frameId: 0, (response) => + @saveMark extend markInfo, scrollX: response.scrollX, scrollY: response.scrollY + + saveMark: (markInfo) -> + item = {} + item[@getLocationKey markInfo.markName] = markInfo + chrome.storage.sync.set item # Goto a global mark. We try to find the original tab. If we can't find that, then we try to find another # tab with the original URL, and use that. And if we can't find such an existing tab, then we create a new -- cgit v1.2.3