- Clear the searchbar after submit & open in new fg tab or current tab if blank (must set browser.search.openintab = false)
- Auto context menu on selection (much improved from my last posting of this)
- Open urlbar queries in new fg tab or in current tab if blank
- Open bookmark-menu URIs in new fg tab or in current tab if blank
- Open history-menu URIs in new fg tab or in current tab if blank
- Open external app links in new tab or in current tab if blank
Code: Select all
if (location == "chrome://browser/content/browser.xul") {
/*======= Clear the searchbar after submit & open in new fg tab or current tab if blank (browser.search.openintab = false) =======*/
var ucSearchbar = {
controller: null,
popupAC_bool: false,
init: function() {
this.searchbar = document.getElementById("searchbar");
if (!this.searchbar) return;
var searchbarTextbox = document.getAnonymousElementByAttribute(this.searchbar, "anonid", "searchbar-textbox");
searchbarTextbox.addEventListener("keydown", function(event) { ucSearchbar.onKeydown(event); }, false);
var searchGoButton = document.getAnonymousElementByAttribute(this.searchbar, "anonid", "search-go-button");
searchGoButton.addEventListener("mousedown", function(event) { ucSearchbar.onMousedown(event); }, false);
searchGoButton.addEventListener("click", function(event) { ucSearchbar.onClick(event); }, false);
if (this.controller) return;
this.controller = Components.classes["@mozilla.org/autocomplete/controller;1"].getService(Components.interfaces.nsIAutoCompleteController);
var popupAC = document.getElementById("PopupAutoComplete");
this.acPopup = popupAC.QueryInterface(Components.interfaces.nsIAutoCompletePopup);
popupAC.addEventListener("mousedown", function(event) { ucSearchbar.popupAC_onMousedown(event); }, false);
popupAC.addEventListener("click", function(event) { ucSearchbar.popupAC_onClick(event); }, false);
},
onKeydown: function(event) {
if (event.keyCode == 13) {
this.tabFunc();
setTimeout("ucSearchbar.searchbar.value='';", 0);
}
},
onMousedown: function(event) { this.tabFunc(); },
tabFunc: function() {
if (gBrowser.currentURI.spec != 'about:blank' || gBrowser.webProgress.isLoadingDocument) {
gBrowser.selectedTab = gBrowser.addTab();
this.searchbar.focus();
}
},
onClick: function(event) { setTimeout("ucSearchbar.searchbar.value='';", 0); },
popupAC_onMousedown: function(event) {
if (this.controller.input.searchParam != "searchbar-history") return;
this.searchbar.value = this.controller.getValueAt(this.acPopup.selectedIndex);
this.tabFunc();
popupAC_bool = true;
},
popupAC_onClick: function(event) {
if (!popupAC_bool) return;
this.onClick(event);
popupAC_bool = false;
}
};
ucSearchbar.init();
eval("BrowserToolboxCustomizeDone = " + BrowserToolboxCustomizeDone.toString().replace('window.f','ucSearchbar.init(); $&'));
/*======= Auto context menu on selection =======*/
var autoContextmenu = {
onMousedown: function(event) {
if (event.button != 0 || !(event.originalTarget.ownerDocument instanceof HTMLDocument)) return;
this.selection = content.getSelection();
if (!this.selection.isCollapsed) //prevent popup on click w/in the selection
this.selection.removeAllRanges();
},
onMouseup: function(event) {
if (event.button != 0 || !(event.originalTarget.ownerDocument instanceof HTMLDocument)) return;
if (this.selection.isCollapsed
|| !/\S/.test(this.selection.toString()) //prevent popup if selection contains only white space (eg, clicking embed'd flash)
|| /^https...mail\.go|buch\.rev|ebay.com\//.test(gBrowser.currentURI.spec)) return; //exclude gmail compose, woerterbuch.reverso.net, ebay
var mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initMouseEvent("contextmenu", true, true, window, 1, event.screenX, event.screenY, event.clientX, event.clientY, false, false, false, false, 2, null);
event.target.dispatchEvent(mouseEvent);
},
hide: function() { if (gContextMenu) gContextMenu.menu.hidePopup(); }
};
document.addEventListener("mousedown", function(event) { autoContextmenu.onMousedown(event); }, true);
document.addEventListener("mouseup", function(event) { autoContextmenu.onMouseup(event); }, false);
document.addEventListener("TextLinkServiceOpenClickedURI", autoContextmenu.hide, false);
/*======= Open urlbar queries in new fg tab or in current tab if blank =======*/
var urlbarInput = {
handle: function(aTriggeringEvent) {
if (aTriggeringEvent instanceof MouseEvent && aTriggeringEvent.button == 2)
return; // Do nothing for right clicks
var [url, postData] = gURLBar._canonizeURL(aTriggeringEvent);
if (!url)
return;
gURLBar.value = url;
gBrowser.userTypedValue = url;
try {
addToUrlbarHistory(url);
} catch (ex) {
// Things may go wrong when adding url to session history,
// but don't let that interfere with the loading of the url.
Cu.reportError(ex);
}
if (aTriggeringEvent instanceof MouseEvent) {
if ((gBrowser.currentURI.spec != 'about:blank' || gBrowser.webProgress.isLoadingDocument) && (aTriggeringEvent == null || (!aTriggeringEvent.ctrlKey && !aTriggeringEvent.shiftKey && !aTriggeringEvent.altKey))) {
gURLBar.handleRevert();
content.focus();
gBrowser.selectedTab = gBrowser.addTab();
}
// We have a mouse event (from the go button), so use the standard
// UI link behaviors
openUILink(url, aTriggeringEvent, false, false,
true /* allow third party fixup */, postData);
return;
}
if ((gBrowser.currentURI.spec != 'about:blank' || gBrowser.webProgress.isLoadingDocument) && (aTriggeringEvent == null || (!aTriggeringEvent.ctrlKey && !aTriggeringEvent.shiftKey))) {
gURLBar.handleRevert();
content.focus();
gBrowser.loadOneTab(url, null, null, postData, false,
true /* allow third party fixup */);
aTriggeringEvent.preventDefault();
aTriggeringEvent.stopPropagation();
}
else
loadURI(url, null, postData, true /* allow third party fixup */);
focusElement(content);
}
};
document.getElementById("urlbar").setAttribute("ontextentered","urlbarInput.handle(param);");
document.getElementById("go-button").setAttribute("onclick","urlbarInput.handle(event);");
/*======= Open bookmark-menu URIs in new fg tab or in current tab if blank =======*/
eval("PlacesUIUtils.openNodeIn = " + PlacesUIUtils.openNodeIn.toString().replace('openU','if ((gBrowser.currentURI.spec != "about:blank" || gBrowser.webProgress.isLoadingDocument) && !/^j/.test(aNode.uri) && aWhere == "current") aWhere = "tab"; $&')); //http://forums.mozillazine.org/viewtopic.php?p=3201065#3201065
eval("PlacesUIUtils._openTabset = " + PlacesUIUtils._openTabset.toString().replace('ue;','$& var tabBrowser = null; if (browserWindow) tabBrowser = browserWindow.getBrowser(); if (tabBrowser && (tabBrowser.currentURI.spec != "about:blank" || tabBrowser.webProgress.isLoadingDocument)) replaceCurrentTab = false;'));
/*======= Open history-menu URIs in new fg tab or in current tab if blank =======*/
document.getElementById("history-menu").setAttribute("oncommand","var node = event.target.node; if (node) { PlacesUIUtils.markPageAsTyped(node.uri); if ((gBrowser.currentURI.spec != 'about:blank' || gBrowser.webProgress.isLoadingDocument) && (event == null || (!event.ctrlKey && !event.shiftKey && !event.altKey))) { gBrowser.selectedTab = gBrowser.addTab(); } openUILink(node.uri, event, false, true); }");
/*======= Open external app links in new tab or in current tab if blank =======*/
eval('nsBrowserAccess.prototype.openURI = ' + nsBrowserAccess.prototype.openURI.toString().replace('b =','$& (win.gBrowser.currentURI.spec == "about:blank" && !win.gBrowser.webProgress.isLoadingDocument) ? win.gBrowser.mCurrentTab :'));
} //chrome://browser/content/browser.xul