Skip to content

Code snippets for custom keyboard shortcuts

Charles edited this page Apr 4, 2016 · 21 revisions

Firefox already have very large number of keyboard shortcuts, and other addons also provide their own keyboard shortcuts, I cannot find out safe combinations for my features. So I gave up and decided to provide only APIs for other addons. Please use other addons (for example: Dorando keyconfig) to customize keyboard shortcuts which can define custom actions based on scripts. Sorry.

See also:

Move focus of tabs

Firefox has its own keyboard shortcuts to move focus between tabs, but they are deactivated by default. You need to activate it by userChrome.css like:

.tabbrowser-tab {
  -moz-user-focus: normal !important;
}

By the way, Ctrl-PageUp and Ctrl-PageDown are available to focus previous/next tab by default.

Toggle show/hide the tab bar

if (gBrowser.treeStyleTab.tabbarShown) {
  gBrowser.treeStyleTab.hideTabbar();
} else {
  gBrowser.treeStyleTab.showTabbar();
}

Close the current and all descendant tabs

gBrowser.treeStyleTab.removeTabSubtree(gBrowser.selectedTab);

Reload ancestor tabs

TreeStyleTabService.getAncestorTabs(gBrowser.selectedTab).forEach(function(aTab) {
  gBrowser.reloadTab(aTab);
});

Jump to the previous tree

var root = TreeStyleTabService.getRootTab(gBrowser.selectedTab);
var prev = TreeStyleTabService.getPreviousSiblingTab(root);
if (prev)
  gBrowser.selectedTab = prev;

Jump to the next tree

var root = TreeStyleTabService.getRootTab(gBrowser.selectedTab);
var next = TreeStyleTabService.getNextSiblingTab(root);
if (next)
  gBrowser.selectedTab = next;

Move tab

Right

gBrowser.treeStyleTab.demoteCurrentTab();

Left

gBrowser.treeStyleTab.promoteCurrentTab();

Down

var tab = gBrowser.selectedTab, b = gBrowser.treeStyleTab.getTabBrowserFromChild(tab).treeStyleTab, next = b.getNextVisibleTab(tab);
if (next) {
	var getTabLevel = (tab) => b.getAncestorTabs(tab).length;
	var nextLevel = getTabLevel(next), tabLevel = getTabLevel(tab);
	if (nextLevel >= tabLevel) {
		if (nextLevel > tabLevel)
			next = b.getNextSiblingTab(tab);
		while (b.hasChildTabs(next))
			next = b.getLastChildTab(next);
		gBrowser.moveTabTo(tab, next._tPos);
	}
}```

#### Up

```javascript
var tab = gBrowser.selectedTab, b = gBrowser.treeStyleTab.getTabBrowserFromChild(tab).treeStyleTab, prev = b.getPreviousVisibleTab(tab);
if (prev) {
	var getTabLevel = (tab) => b.getAncestorTabs(tab).length;
	var prevLevel = getTabLevel(prev), tabLevel = getTabLevel(tab);
	if (prevLevel >= tabLevel) {
		if (prevLevel > tabLevel)
			prev = b.getPreviousSiblingTab(tab);
		gBrowser.moveTabTo(tab, prev._tPos);
		var newLevel = getTabLevel(tab);
		while (newLevel-- > tabLevel)
			b.promoteTab(tab);
	}
}```