Skip to content

Instantly share code, notes, and snippets.

@Mariven
Created February 28, 2025 19:20
Show Gist options
  • Select an option

  • Save Mariven/39616f32b6928eb532dce37581622ca8 to your computer and use it in GitHub Desktop.

Select an option

Save Mariven/39616f32b6928eb532dce37581622ca8 to your computer and use it in GitHub Desktop.
Press '/' to focus on the first search input on any webpage
// ==UserScript==
// @name Slash Jumps to Search
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Press '/' to focus on the first search input on any webpage
// @author Mariven
// @match *://*/*
// @exclude *://*google.com*
// @exclude *://*youtube.com*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const isSlash = (e) => e.key === '/' || e.keyCode === 191;
const activeInput = (el = document.activeElement) => ['INPUT', 'TEXTAREA'].includes(el.tagName) || el.isContentEditable;
const inputSelectors = `
input[type="search"],
input[name*="search" i],
input[placeholder*="search" i],
input[aria-label*="search" i],
form input[type="text"],
input[type="text"]`;
document.addEventListener('keydown', function(e) {
if (isSlash(e)) {
if (activeInput())
return;
e.preventDefault(); // prevents '/' from being typed
const searchInput = document.querySelector(inputSelectors);
if (searchInput)
searchInput.focus();
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment