User:TheDaveRoss/searchFocus.js
Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.
- Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
- Konqueror and Chrome: click Reload or press F5;
- Opera: clear the cache in Tools → Preferences;
- Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.
- This script lacks a documentation subpage. Please create it.
- Useful links: root page • root page’s subpages • links • redirects • your own
// Suchfeldfokus - from de.wikt, focuses search box on most non-edit pages
url = document.URL;
var doFocus;
var exFocus;
var gotFocus;
var doNotFocus;
if (!doNotFocus && mw.config.get('wgAction') != "edit" && mw.config.get('wgAction') != "submit" && !(mw.config.get('wgNamespaceNumber') < 0 && mw.config.get('wgCanonicalSpecialPageName').match(/^[Rr]eset|^[Pp]ass|^[Uu]ser[lL]ogin|^[Ll]ogin/))) {
var actionKey;
$(document).ready(function() {
doFocus = 1;
});
document.onmousedown = function() {gotFocus = 1;};
document.onkeydown = function(kEvent) {
if (doFocus && !gotFocus) {
if (!kEvent) {kEvent = window.event;}
if ((!actionKey && (kEvent.which == 16 || (kEvent.which > 40 && kEvent.which < 112) || (!kEvent.which && (!kEvent.keyCode || kEvent.keyCode > 40))) || kEvent.keyCode == 16) || (actionKey && (kEvent.which == 86 || kEvent.keyCode == 86 || kEvent.keyCode == 118))) {
gotFocus = 1;
if (exFocus || url.indexOf("#") > 0 || mw.config.get('skin') != "monobook") {focusSearch();}
} else if ((kEvent.which > 32 && kEvent.which < 41) || (kEvent.keyCode > 32 && kEvent.keyCode < 41)) {
exFocus = 1;
focusSearch(8);
} else if (kEvent.which == 17 || kEvent.which == 18 || kEvent.keyCode == 17 || kEvent.keyCode == 18) {
actionKey = 1;
} else if (kEvent.which == 9 || kEvent.keyCode == 9) {
gotFocus = 1;
}
}
};
}
function focusSearch(x) {
if (document.getElementById("searchText") && url.indexOf("&fulltext=") > 0) {
if (x) {
document.getElementById("searchText").blur();
} else {
document.getElementById("searchText").focus();
document.getElementById("searchText").select();
}
} else {
if (x) {
document.getElementById("searchInput").blur();
} else {
document.getElementById("searchInput").focus();
document.getElementById("searchInput").select();
}
}
}