Created
February 28, 2025 19:20
-
-
Save Mariven/39616f32b6928eb532dce37581622ca8 to your computer and use it in GitHub Desktop.
Press '/' to focus on the first search input on any webpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==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