tridactyl/addon/main.coffee

82 lines
3 KiB
CoffeeScript
Raw Normal View History

2017-02-13 15:16:34 +00:00
# Quick and dirty prototyping.
#
# Functions declared here can be called in the debug window in ff.
handleBrowserAction = () ->
# .then takes a function that consumes at least one argument. this is an
# example of a named function, but anonymous functions are fine, too.
browser.tabs.query({active: true}).then(console.log)
2017-02-13 21:42:23 +00:00
# Change to the next tab in window, wrapping back to the start, if req.
#
# Get an array of all tabs, find the active tab's index, and the tab id of
# the tab with the next index.
#
# TODO: limit to focused window.
nextTab1 = () ->
browser.tabs.query({}).then(
(tabs) ->
# Doing my own list comprehensions means they're not done
# asynchronously from each other, but they will be very fast and
# this makes the code much simpler.
active = (tab for tab in tabs when tab.active == true)[0]
target_index = (active.index + 1) % tabs.length
target = (tab for tab in tabs when tab.index == target_index)[0]
displayTab(target.id)
, console.error)
# Change to the next tab in window, wrapping back to the start, if req.
#
# Get an array of all tabs, find the active tab's index, and the tab id of
# the tab with the next index.
#
# TODO: limit to focused window.
nextTab2 = () ->
# Alternative implementation without list comprehensions.
2017-02-14 01:27:02 +00:00
#
# If querying all tabs to get number of tabs is too slow (as if), could
# query for index+1 and then test if result array.length == 0.
2017-02-13 21:42:23 +00:00
browser.tabs.query({}).then(
(tabs) ->
browser.tabs.query({active: true}).then(
(tabs2) ->
active = tabs2[0]
target_index = (active.index + 1) % tabs.length
browser.tabs.query({index: target_index}).then(
(tabs3) ->
displayTab(tabs3[0].id)
, console.error)
, console.error)
, console.error)
# Convenience: mark tab_id as active (active tabs are displayed in their window)
displayTab = (tab_id) ->
browser.tabs.update(tab_id, {active: true}).then(null, console.error)
2017-02-13 15:16:34 +00:00
# Example of a listener. Presumably we wouldn't use the browserAction button in
# the real thing.
browser.browserAction.onClicked.addListener(handleBrowserAction)
nextTab = () ->
2017-02-13 23:01:00 +00:00
browser.windows.getCurrent().then(
(window) ->
browser.tabs.query({windowId:window.id}).then(
(tabs) ->
active = (tab for tab in tabs when tab.active)[0]
nextIndex = (active.index + 1) % tabs.length
desiredTab = (tab for tab in tabs when tab.index == nextIndex)[0]
setTab(desiredTab.id)
).catch(console.error)
).catch(console.error)
setTab = (id) ->
browser.tabs.update(id,{active:true})
tabByIndex = (index) ->
browser.tabs.query({}).then((tabs) ->
desiredTab = tab for tab in tabs when tab.index == desIndex
)
2017-02-13 15:16:34 +00:00
console.log("Loaded Tridactyl")