Skip to content

Instantly share code, notes, and snippets.

@jix
Last active March 6, 2021 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jix/e034ebc8b6658d7d87cc40e517cfbc5e to your computer and use it in GitHub Desktop.
Save jix/e034ebc8b6658d7d87cc40e517cfbc5e to your computer and use it in GitHub Desktop.
User script that adds a hotkey to open the docs.rs link for the currently viewed crate on crates.io.
// ==UserScript==
// @name docs.rs hotkey for crates.io
// @namespace Violentmonkey Scripts
// @match https://crates.io/*
// @grant none
// @version 1.1
// @author @jix_
// @description Press d to open the docs.rs link for the currently viewed crate on crates.io.
// ==/UserScript==
const openLink = function (heading) {
for (headingTag of document.querySelectorAll('section[aria-label="Crate metadata"] h3')) {
if (headingTag.innerText !== heading)
continue;
const link = headingTag.parentNode.querySelector('a');
if (link === null) {
console.log(`Could not find link under heading ${heading}`);
} else {
window.open(link.href, '_blank');
}
return;
}
console.log(`Could not find link with heading ${heading}`);
};
const openTab = function (index) {
let tabs = document.querySelectorAll('main nav li a');
tabs[index].click();
}
document.body.addEventListener('keydown', function (event) {
if (event.target !== document.body || event.ctrlKey || event.altKey || event.metaKey)
return;
if (event.key === 'd') {
openLink('Documentation');
} else if (event.key === 'r') {
openLink('Repository');
} else if (event.key === 'h') {
openLink('Homepage');
} else if (event.key >= '1' && event.key <= '9') {
openTab(Number(event.key) - 1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment