aboutsummaryrefslogtreecommitdiffstats
path: root/background_scripts
diff options
context:
space:
mode:
authorStephen Blott2015-06-04 11:40:55 +0100
committerStephen Blott2015-06-04 11:40:55 +0100
commit931d246e59389bd3070aa03bde4fada9f2ef9700 (patch)
treefbd06a982925d12eddf457ede4d9a2df3180a961 /background_scripts
parent8b8cc319cc02521a11e766e0d059a6fa53e5f4ae (diff)
downloadvimium-931d246e59389bd3070aa03bde4fada9f2ef9700.tar.bz2
Global marks; move marks to chrome.storage.
Diffstat (limited to 'background_scripts')
-rw-r--r--background_scripts/marks.coffee84
1 files changed, 50 insertions, 34 deletions
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