How to open everything in a tab in Firefox

To open everything in a tab in Firefox, many steps are required. You will need some extension to achieve the more complicated ones.

TL;DR

Change the followings:

  1. browser.search.openintab to true in about:config
  2. Install userChrome.js and add the following script.
  3. Done.

What decides where something is open ?

When you click or type somewhere in Firefox, there is code that decides where your click or enter should open. This code is mostly javascript, which means we can inspect it and modify it.

Two (main) functions are deciding where to open: urlbar.handleCommand and whereToOpenLink.

To see those functions, you have two options:

  1. Open the omni.ja file, which is just a zip file in disguise, and dig through the files.
  2. Open the following URL in firefox: chrome://browser/content/browser.xul and use the debug in Firefox.

For example, by doing window.whereToOpenLink.toString() we get the following function:

function whereToOpenLink( e, ignoreButton, ignoreAlt )
{
  // This method must treat a null event like a left click without modifier keys (i.e.
  // e = { shiftKey:false, ctrlKey:false, metaKey:false, altKey:false, button:0 })
  // for compatibility purposes.
  if (!e)
    return "current";

  var shift = e.shiftKey;
  var ctrl =  e.ctrlKey;
  var meta =  e.metaKey;
  var alt  =  e.altKey && !ignoreAlt;

  // ignoreButton allows "middle-click paste" to use function without always opening in a new window.
  var middle = !ignoreButton && e.button == 1;
  var middleUsesTabs = getBoolPref("browser.tabs.opentabfor.middleclick", true);

  // Don't do anything special with right-mouse clicks.  They're probably clicks on context menu items.

  var metaKey = AppConstants.platform == "macosx" ? meta : ctrl;
  if (metaKey || (middle && middleUsesTabs))
    return shift ? "tabshifted" : "tab";

  if (alt && getBoolPref("browser.altClickSave", false))
    return "save";

  if (shift || (middle && !middleUsesTabs))
    return "window";

  return "current";
}

Problem is, this function is used in many places so it’s hard to just replace it with a ‘tab’. There are some times where we want to reuse the same tab (for example, when it’s empty).

Open searches in a new tab

That one is very easy. Open the URL about:config in your browser, then navigate to:

browser.search.openintab

and set it to true.

Open URLs in a new tab

This one is a little bit more complicated. When you type an URL in the urlbar, there is a function that decides where to open it. However, there is a neat mechanism that allows us to hijack it. Usual behavior is that when you press alt key, the link opens in a new tab. We just invert the logic to have the alt key ‘pressed’ by default.

First step is to install userChrome.js. After that, it’s just a matter of adding the following script:

(function() {
var urlbar = document.getElementById("urlbar");
if (urlbar != null) {
    var functxt = urlbar.handleCommand.toString();
    functxt = functxt.replace("event.altKey &&", "!event.altKey &&").replace("where = whereToOpenLink(event, false, false);", "where = 'tab';");
    eval( "urlbar.handleCommand = " + functxt);
}
})();