MediaWiki:Gadget-omnipresent search.js

From OpenStreetMap Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/**
 * Automatically focus the search box when typing outside a text box.
 */
$(document.body).keypress(function (evt) {
    // A text box is already focused.
    if (evt.target.type || $(document.documentElement).hasClass("ve-active")) return;
    
    // Skip control keys and the space bar.
    // Also skip the / key because it’s the Quick Find shortcut key in Firefox.
    if (evt.ctrlKey || evt.altKey || evt.metaKey) return;
    var ch = String.fromCharCode(evt.which);
    if (ch.match(/[\0\r\n\t\b /]/)) return;
    
    // Put the typed character and caret in the search box.
    var searchBox = $("#searchInput");
    if (!searchBox.length) return;
    searchBox.val(searchBox.val() + ch).focus();
    
    // The first time this function runs, the caret could still be at the
    // beginning of the line. Adjust the caret to avoid reversing the query.
    searchBox.textSelection("setSelection", {start: searchBox.val().length});
});