This addon will add a new shortcut for Close Other Tabs command
1. Create a folder with name close-other-tabs-test.
2. Create file install.rdf to give the addon a name, id and application binding (Thunderbird, Firefox, Android)
<?xml version="1.0"?> <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:em="http://www.mozilla.org/2004/em-rdf#"> <Description about="urn:mozilla:install-manifest"> <em:id>close-other-tabs-test@test.org</em:id> <em:name>Close Other Tabs Test</em:name> <em:version>1.0</em:version> <em:type>2</em:type> <em:bootstrap>true</em:bootstrap> <em:description>Closes other tabs</em:description> <em:creator>creator</em:creator> <!-- Firefox --> <em:targetApplication> <Description> <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id> <em:minVersion>20.0</em:minVersion> <em:maxVersion>36.0</em:maxVersion> </Description> </em:targetApplication> </Description> </RDF>
id - any id with @ type - it's an addon (not theme) bootstrap - it's restartless targetApplication -> id - constant bound to Firefox
3. Create file bootstrap.js which is the entry point of the addon
const {classes: Cc, interfaces: Ci, utils: Cu}=Components Cu.import("resource://gre/modules/Services.jsm") Cu.import("resource://gre/modules/AddonManager.jsm") /* **************** vars **************** */ const shortcutProps={ id:"key_CloseOtherTest", key:"W", modifiers:"alt", command:"gBrowser.removeAllTabsBut(gBrowser.selectedTab)" } var self=this /* **************** main functions **************** */ function startup(data,reason){ AddonManager.getAddonByID(data.id, function(addon){ include(addon, "content/ui.js") include(addon, "content/main.js") eachWindow(loadIntoWindow) //ui building function Services.ww.registerNotification(windowWatcher) }) } function shutdown(data,reason){ if(reason==ADDON_DISABLE){ Services.ww.unregisterNotification(windowWatcher) eachWindow(unloadFromWindow) //ui destroying function } } /* **************** add functions **************** */ function include(addon, path){ //load scripts Services.scriptloader.loadSubScript(addon.getResourceURI(path).spec, self) }
include() – is used to add external .js scripts to the addon
On uninstall only (which is filtered by the ADDON_DISABLE reason) the function unloadFromWindow runs for each open window and removes the UI elements (removes the shortcut).
4. Create folder named content and create files inside it.
main.js ui.js
5. Add code to the main.js file
/* **************** ui build ***************** */ function loadIntoWindow(window){ addKeyboardShortcut(window) } function unloadFromWindow(window){ if (!window) return removeKeyboardShortcut(window) } /* **************** load functions **************** */ function eachWindow(callback){ let enumerator=Services.wm.getEnumerator("navigator:browser") while (enumerator.hasMoreElements()){ let win=enumerator.getNext() if (win.document.readyState==="complete") callback(win) else runOnLoad(win, callback) } } function windowWatcher (subject, topic){ if (topic==="domwindowopened") runOnLoad(subject, loadIntoWindow) } function runOnLoad (window, callback){ window.addEventListener("load", function(){ window.removeEventListener("load", arguments.callee, false) callback(window) }, false) }
The addon functions only work when the active browser window is completely loaded.
5. Add code to the ui.js file
function addKeyboardShortcut(w){ var document=w.document var keyset=document.createElement("keyset") var key=document.createElement("key") if(keyset && key){ key.setAttribute("id", shortcutProps.id); key.setAttribute("key", shortcutProps.key); key.setAttribute("modifiers", shortcutProps.modifiers); key.setAttribute("oncommand", shortcutProps.command); keyset.setAttribute("id","closeOtherKeyset") keyset.appendChild(key); document.documentElement.appendChild(keyset) } } function removeKeyboardShortcut(w){ var document=w.document var keyset=document.getElementById("closeOtherKeyset") keyset && keyset.parentNode.removeChild(keyset) }
The removeKeyboardShortcut() function removes these elements from the tree and disables the created shortcut.
6. Now open the install.rdf file, copy the id property, create new blank file and paste the copied value as its name. So the file will be named
close-other-tabs-test@test.org
7. Open this file and paste the path to the close-other-tabs-test folder.
8. Close Firefox
9. Now go to Firefox extensions folder.
C:\Documents and Settings\[username]\Application Data\Mozilla\Firefox\Profiles\[profile-name].default\extensions\
C:\Users\[username]\AppData\Roaming\Mozilla\Firefox\Profiles\[profile-name].default\extensions\
10. And copy the close-other-tabs-test@test.org file here
11. Open Firefox. It should display the Install Addon screen. Check Allow this installation and press Continue.
12. Test the addon. Open multiple tabs. Press Alt+W. All tabs except the current one should close (there could be a warning before asking permission to close multiple tabs)
13. Pack the addon. Go to the addon folder select all and create a .zip archive with 7-zip. Then change the extension to .xpi.
Install it as a regular addon.
Additional Information
1. Addon folder structure:
/ close-other-tabs-test - / content - main.js - ui.js - bootstrap.js - install.rdf
2. Project GitHub repository
3. Check the DOM structure
- Install DOM Inspector an Element inspector addons
- Holding Shift rightclick anywhere in the browser titlebar
- Press End or scroll down to the end of the DOM Inspector window
-
Check the closeOtherKeyset keyset element is there.
- Also try to uninstall the addon and check again the DOM, the keyset should be removed.