User:TheDaveRoss/searchFocus.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • 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.

// 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();
   }
  }
 }