Last active
July 29, 2024 21:16
-
-
Save Mordo95/64291ed84f22f57181bd706f48dfe03d to your computer and use it in GitHub Desktop.
Keyboard shortcuts for google and youtube
This file contains 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 Keyboard shortcuts for google and youtube | |
// @version 0.1 | |
// @description Adds shortcuts for google and youtube | |
// @author Mordo95 | |
// @match *://*/* | |
// @icon https://www.google.com/s2/favicons?domain=google.nl | |
// @grant none | |
// ==/UserScript== | |
// Made for: https://www.reddit.com/r/userscripts/comments/p5imfo/how_do_you_create_a_userscript_that_clicks_a/ | |
(function() { | |
'use strict'; | |
function clickElement(selector) { | |
let el = document.querySelector(selector); | |
if (el) | |
el.click(); | |
} | |
function focusElement(selector) { | |
let elements = document.querySelectorAll(selector); | |
for (let el of elements) { | |
el.focus(); | |
if (el.tagName === "INPUT") | |
el.setSelectionRange(el.value.length, el.value.length); | |
} | |
} | |
if (/:\/\/.*(?:google|youtube)\..*\//.test(window.location.href)) { | |
document.addEventListener("keydown", (e) => { | |
if (e.ctrlKey && e.code === "ArrowRight") | |
clickElement("#pnnext"); | |
if (e.ctrlKey && e.code === "ArrowLeft") | |
clickElement("#pnprev"); | |
if (e.ctrlKey && e.code === "Space") { | |
focusElement("[name=q],[name=search_query]"); | |
} | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment