Skip to content

Instantly share code, notes, and snippets.

@ZER0
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZER0/10637946 to your computer and use it in GitHub Desktop.
Save ZER0/10637946 to your computer and use it in GitHub Desktop.
Add-on SDK: Reverse the tabs' order in the current window
const { ActionButton } = require("sdk/ui/button/action");
const { browserWindows } = require("sdk/windows");
let button = ActionButton({
id: "reverse-tabs",
label: "Reverse Tab Order",
icon: {
// The new APIs use "./" as shortcut for data folder, you don't need
// to require `self` for that purpose.
"16": "./16.png",
"32": "./32.png",
"64": "./64.png"
},
onClick: function(state) {
// Given the type of code you wrote, you don't want to reverse the tabs across all windows;
// but only the tabs of the current one.
let { tabs } = browserWindows.activeWindow;
// console's method are already bound to the console object,
// you don't need to do so.
// Plus, by default, `console.log` dumps only when you're in
// development mode (from cfx run) and not when you install
// the add-on (XPI); so you don't need an additional variable `debug`.
let LOG = console.log;
// don't use for...each, is deprecated.
// See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for_each...in
let notPinned = ({isPinned}) => !isPinned;
let byIndex = ({index: a}, {index: b}) => a - b;
// We need to get the tabs ordered by index, 'cause tabs
// module returns the tabs always in the same order
let normalTabs = Array.filter(tabs, notPinned).sort(byIndex)
let { length } = normalTabs;
// we iterate only half tabs, because we switching them
let count = length >> 1;
for (let i = 0; i < count; i++) {
let index = normalTabs[i].index;
let k = length - i - 1;
normalTabs[i].index = normalTabs[k].index;
normalTabs[k].index = index;
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment