Last active
November 17, 2024 01:39
-
-
Save NickSlash/6a7ce3187cdf11725fc49801f2b66d38 to your computer and use it in GitHub Desktop.
Userscript to add a direct link to the latest version of a gist
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 Latest Gist | |
| // @namespace nickslash | |
| // @version 1.1 | |
| // @description Shift clicking "Raw" link will take you to the latest version | |
| // @author nickslash | |
| // @match https://gist.github.com/*/* | |
| // @grant none | |
| // ==/UserScript== | |
| (function() { | |
| 'use strict'; | |
| function processLink(link) { | |
| if (link.getAttribute("latest-href")) return | |
| let href = link.getAttribute("href") | |
| if (!href) return | |
| let match = href.match(/^\/([^\/]+)\/([^\/]+)\/raw\/[^\/]+\/(.+)$/) | |
| if (match) { | |
| link.setAttribute("latest-href", `/${match[1]}/${match[2]}/raw/${match[3]}`) | |
| let buttonLabel = link.querySelector(".Button-label") | |
| if (buttonLabel) buttonLabel.innerText += "+" | |
| link.addEventListener("click", event => { | |
| if (event.shiftKey) { | |
| event.preventDefault() | |
| window.location.href = link.getAttribute("latest-href") // Use `link` directly | |
| } | |
| }) | |
| } | |
| } | |
| function processLinks() { | |
| document.querySelectorAll("a:not([latest-href])").forEach(link => processLink(link)) | |
| } | |
| processLinks() | |
| const observer = new MutationObserver(mutations => { | |
| for (let mutation of mutations) { | |
| if (mutation.type === "childList") { | |
| if (mutation.addedNodes.length > 0) { | |
| processLinks() | |
| } | |
| } | |
| } | |
| }) | |
| observer.observe(document.body, { childList: true, subtree: true }) | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment