Amazing Firefox Shortcuts Using Regex
As a keyboard shortcut maniac, I love the keyconfig Firefox extension, which lets you manage conflicting shortcuts, as well as create fancy shortcuts by binding keys to pieces of JavaScript code.
Recently, inspired by Mingyi Liu's terrific Fastest Search extension, I resolved to make my Firefox usage even faster by creating some fancy shortcuts using regex.
On this page, I share the ones I've come up with so far. Please note that my approach here has been "quick and dirty", so I make no representations that either the JavaScript or the regex are ideal. I've included some related shortcuts that do not require regex.
If you have suggestions, please send them along at the bottom of this page.
How to Set Up these Shortcuts
✽ Install keyconfig then run it (Ctrl + Sh + F12).✽ To activate the "Add a new key" button, you may need to use the pull-down at the top to change the selection, use the same menu to return where you were.
✽ Click "Add a new key".
✽ In the name, insert a name that starts with a "0" so your shortcuts will stay together when you sort them by name.
✽ In the code box, paste the code. Press OK.
✽ Enter a key combination in the small box at the bottom and click Apply.
✽ If there is a conflict, sort by shortcut and resolve.
Navigation Shortcuts
(direct link)Navigate Up from the Current URL (suggested shortcut: Ctrl + Shift + U)
Pressing this shortcut repeatedly navigates up and up. First, the anchor part of the string (what follows the #), if any, is stripped. Next, the query part of the string (what follows the ?) is stripped. Then we navigate up the file path.
var root = content.document.location.origin;
var path = content.document.location.pathname; // includes the leading "/"
// Do we have a hash anchor?
if (content.document.location.hash) {
// Go Up by stripping the anchor
var newloc = root + content.document.location.pathname + content.document.location.search;
openUILinkIn(newloc,"current");
}
// Do we have a query string?
else if (content.document.location.search) {
var newloc = root + content.document.location.pathname;
openUILinkIn(newloc,"current");
}
else {
var upRegex = /^.*\/(?!$)/;
var matchArray = upRegex.exec(path);
if (matchArray != null) {
var upPath = matchArray[0];
var newloc = root + upPath;
openUILinkIn(newloc,"current");
}
}
(direct link)
Navigate Up Domain Hierarchy (suggested shortcut: Ctrl + Alt + U)
Pressing this shortcut removes the leftmost subdomain from the domain name. For instance, http://mail.google.com becomes http://google.com.
Sure, you can end up with domains that don't exist. So what? The point is to use the shortcut when it can save you time.
var host = content.document.location.hostname;
var protocol = content.document.location.protocol;
var upRegex = /^[^.]+\.([^.]+\..*)/;
var matchArray = upRegex.exec(host);
if (matchArray != null) {
var upPath = matchArray[1];
var newloc = protocol + "//" + upPath;
openUILinkIn(newloc,"current");
}
(direct link)
Navigate to the Root of the Current URL (suggested shortcut: Ctrl + Shift + R)
For instance, if the url is http://www.rexegg.com/regex-firefox-shortcuts.html, the browser will navigate to http://www.rexegg.com. No regex needed.
openUILinkIn(content.document.location.origin, "current");
(direct link)
Increment / Decrement URL (suggested shortcut: Ctrl + Shift + plus / minus)
This is for sites that have pages with incrementing numbers. Pressing the shortcut increments or decrements the last number of the url by one. Clearly, the regex needs to be adapted to particular cases (for instance, you may want to increment the next to last number).
Code to decrement:
var url = content.document.location.href; // the entire url
var numRegex = /^(.*\D)(\d+)(.*)/;
var matchArray = numRegex.exec(url);
if (matchArray != null) {
var num = parseInt( matchArray[2] );
var newnum = (num-1).toString();
var newloc = matchArray[1] + newnum + matchArray[3];
openUILinkIn(newloc,"current");
}
Code to increment:
var url = content.document.location.href; // the entire url
var numRegex = /^(.*\D)(\d+)(.*)/;
var matchArray = numRegex.exec(url);
if (matchArray != null) {
var num = parseInt( matchArray[2] );
var newnum = (num+1).toString();
var newloc = matchArray[1] + newnum + matchArray[3];
openUILinkIn(newloc,"current");
}
(direct link)
Google this Site (suggested shortcut: Ctrl + G)
This shortcut opens a Google tab that searches for the word "gold" (to be replaced by what you need at the time) on the current site. No regex required.
var site = content.document.location.hostname;
var newloc = "https://www.google.com/?gws_rd=ssl#q=site:" + site + " gold";
openUILinkIn(newloc,"tab");
(direct link)
Transform URL: toggle this site between the .com version and the .co.nz version
I took New Zealand addresses as an example: Adapt to your needs.
var protocol = content.document.location.protocol;
var host = content.document.location.hostname;
var tail = content.document.location.pathname +
content.document.location.search +
content.document.location.hash;
var tldRegex = /^(.*?)(\.co)?\.([^.]+)$/;
var matchArray = tldRegex.exec(host);
if (matchArray != null) {
if (matchArray[3] != "nz") {
var newloc = protocol + matchArray[1] + ".co.nz" + tail;
openUILinkIn(newloc,"current");
}
else {
var newloc = protocol + matchArray[1] + ".com" + tail;
openUILinkIn(newloc,"current");
}
}
(direct link)
Transform URL: find Subtitles for this IMDB movie ID
The idea is to take one piece from the current url and to use it to visit a different url. The idea should be adapted for your needs. In this example, if you are on this IMDB page: http://www.imdb.com/title/tt0366551/, the shortcut will extract the IMDB ID and search for it on the Open Subtitles website.
var path = content.document.location.pathname;
var ttRegex = /tt\d+/;
var matchArray = ttRegex.exec(path);
if (matchArray != null) {
tt = matchArray[0];
var newloc = "http://www.opensubtitles.org/en/search2?MovieName=" +
tt + "&action=search&SubLanguageID=eng";
openUILinkIn(newloc,"tab");
}
Shortcuts to Copy URL Fragments
(direct link)Copy Page IDs (with Amazon & IMDB examples) (suggested shortcut: Ctrl + I)
This shortcut aims to copy page identifiers for various sites. If specific sites interest you, you need to add them in the code. As is, the shortcut handles IMDB's tt and nm fields (such as tt0366551) as well as Amazon product (dp) codes.
// Copies Identifiers ("Tags") for Various Sites
// See location properties
// http://www.w3schools.com/jsref/obj_location.asp
var host = content.document.location.hostname;
var path = content.document.location.pathname;
var pathsearch = content.document.location.pathname + content.document.location.search;
var tag;
var tagRegex;
var matchArray;
// For each site we're interested in, set up a regex
// The regex captures the tag to Group 1
// Depending on the site, we may match different components of the url
if( /\bimdb\.com/.test(host) ) {
tagRegex = /(?:title|name)\/([^\/?]+)/;
matchArray = tagRegex.exec(path);
}
else if( /\bamazon\./.test(host) ) {
tagRegex = /(?:dp|product)\/([^\/?]+)/;
matchArray = tagRegex.exec(path);
}
// Don't know this site? Grab some digits
else {
tagRegex = /(\d+)/;
matchArray = tagRegex.exec(pathsearch);
}
if (matchArray != null) {
tag = matchArray[1];
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(tag);
}
(direct link)
Copy the entire url (suggested shortcut: Ctrl + Ins)
No regex required.
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(content.document.location.href);
(direct link)
Copy the website's name (suggested shortcut: Ctrl + Alt + Ins)
For instance, if the url is http://www.rexegg.com/regex-firefox-shortcuts.html, the clipboard will contain www.rexegg.com. No regex required.
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(content.document.location.hostname);
(direct link)
Copy the file name (without anchors and query fragments) (suggested shortcut: Ctrl + Shift + Ins)
For instance, if the url is http://www.rexegg.com/regex-firefox-shortcuts.html#filename, the clipboard will contain regex-firefox-shortcuts.html
var path = content.document.location.pathname;
var pageRegex = /[^\/]+$/;
var matchArray = pageRegex.exec(path);
if (matchArray != null) {
var page = matchArray[0];
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(page);
}
(direct link)
Copy the anchor fragment (suggested shortcut: Alt + Ins)
For instance, if the url is http://www.example.com/main.html?s=1#top, the clipboard will contain #top. No regex required.
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(content.document.location.hash);
(direct link)
Copy the query ("search") fragment (suggested shortcut: Shift + Ins)
For instance, if the url is http://www.example.com/main.html?s=1#top, the clipboard will contain ?s=1. No regex required.
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(content.document.location.search);
(direct link)
Copy the file name (including anchors and query fragments) (suggested shortcut: Ctrl + Alt + Shift + Ins)
For instance, if the url is http://www.rexegg.com/regex-firefox-shortcuts.html#filename, the clipboard will contain regex-firefox-shortcuts.html#filename
var path = content.document.location.pathname;
var hash = content.document.location.hash;
var query = content.document.location.search;
var pageRegex = /[^\/]+$/;
var matchArray = pageRegex.exec(path);
if (matchArray != null) {
var page = matchArray[0];
var wholepage = page + hash + query;
var clipboard = Cc["@mozilla.org/widget/clipboardhelper;1"]
.getService(Ci.nsIClipboardHelper);
clipboard.copyString(wholepage);
}
Other Useful Firefox Shortcuts
(direct link)Next / Previous Tab (suggested shortcut: Ctrl + Alt + Left/Right Arrows)
These shortcuts navigate to the next or previous tabs. No regex required.
Next Tab:
gBrowser.mTabContainer.advanceSelectedTab(1,true);
Previous Tab:
gBrowser.mTabContainer.advanceSelectedTab(-1,true);
(direct link)
Preferences (suggested shortcuts: F12 or Ctrl + K or Ctrl + , )
Opens the Firefox preferences. No regex required.
openPreferences();
(direct link)
Add-Ons (suggested shortcut: X)
Opens the Firefox Add-on page. No regex required.
BrowserOpenAddonsMgr();
(direct link)
Back (suggested shortcut: Ctrl + Backspace)
Navigates to the previous page.
Browser:Back
(direct link)
Location Bar (suggested shortcut: F4)
I never liked Ctrl + L to target the location bar. This lets you progam an alternate shortcut.
openLocation();
Smiles,
Rex