Skip to content

Instantly share code, notes, and snippets.

@Mordo95
Last active August 19, 2021 00:11
Show Gist options
  • Save Mordo95/64291ed84f22f57181bd706f48dfe03d to your computer and use it in GitHub Desktop.
Save Mordo95/64291ed84f22f57181bd706f48dfe03d to your computer and use it in GitHub Desktop.
Keyboard shortcuts for google and youtube
// ==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