Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(function() {
//if (location != "chrome://browser/content/browser.xul") return;
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/Services.jsm");
var AutoFocusUrlbar = {
cs: Services.console, // nsIConsoleService
ww: Services.ww, // nsIWindowWatcher
wm: Services.wm, // nsIWindowMediator
ps: Services.prefs,
get doc() {
return this.wm.getMostRecentWindow("navigator:browser").document;
},
get eres() {
return this.ps.getCharPref('extensions.AutoFocusUrlbar.excludeChar');
},
handleEvent: function(e) {
var document = this.doc;
// don't focus urlbar when overflow-button is open
var oflow = document.getElementById("nav-bar-overflow-button");
var open = oflow.getAttribute("open");
if (open == 'true') return;
// don't focus urlbar when sidebar is open
var sidebar = document.getElementById("sidebar-box");
var checked = sidebar.getAttribute("checked");
if (checked == 'true') return;
// only focus urlbar on 'Lab16' (home)page
var title = document.title;
if (!title.includes('Lab16')) return;
var elm = document.activeElement;
while (1) {
if (elm == null) return;
//if (elm.localName == "browser" || elm.localName == "iframe" ||
// elm.localName == "frame") {
// elm = elm.contentDocument.activeElement;
// continue;
//}
break;
}
if (elm.ownerDocument.designMode == 'on' ||
elm.localName == "input" || elm.localName == "select" || elm.localName == "textarea" ||
elm.isContentEditable) return;
var mod = e.ctrlKey || e.metaKey || e.altKey;
if (mod) return;
var c = String.fromCharCode(e.charCode);
var excludePattern;
try {
var excludeRegExpString = this.eres;
excludePattern = new RegExp(excludeRegExpString);
} catch (err) {
excludePattern = /[^a-zA-Z]/;
}
if (excludePattern.test(c)) return;
var urlbar = document.getElementById("urlbar");
if (!urlbar) return;
e.preventDefault();
urlbar.focus();
try {
urlbar.value = '';
var evt = document.createEvent("KeyboardEvent");
evt.initKeyEvent('keypress', false, true, null,
false, false, false, false, 0, e.charCode);
var input = urlbar.inputField;
input.dispatchEvent(evt);
} catch (err) {
urlbar.value = c;
}
},
aListener: {
onOpenWindow: function(aWindow) {
var win = aWindow.docShell.QueryInterface(Ci
.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindow);
win.addEventListener("load", function() {
win.removeEventListener("load", arguments.callee, true);
if (win.document.documentElement.getAttribute("windowtype") !=
"navigator:browser") return;
win.addEventListener("keypress", AutoFocusUrlbar, false);
}, true);
},
onCloseWindow: function(aWindow) {},
onWindowTitleChange: function(aWindow, aTitle) {},
},
startup: function() {
this.wm.addListener(this.aListener);
var cw = this.wm.getEnumerator("navigator:browser");
while (cw.hasMoreElements()) {
var win = cw.getNext().QueryInterface(Ci.nsIDOMWindow);
win.addEventListener("keypress", this, false);
}
},
shutdown: function() {
this.wm.removeListener(this.aListener);
var cw = this.wm.getEnumerator("navigator:browser");
while (cw.hasMoreElements()) {
var win = cw.getNext().QueryInterface(Ci.nsIDOMWindow);
win.removeEventListener("keypress", this, false);
}
}
}
function startup(data, reason) {
var cs = Services.console;
AutoFocusUrlbar.startup();
}
startup();
function shutdown(data, reason) {
AutoFocusUrlbar.shutdown();
}
function install(data, reason) {}
function uninstall(data, reason) {}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.