Last active
July 3, 2023 22:16
-
-
Save gregory-seidman/67c2186bdf6496f4f238209943cdc1b5 to your computer and use it in GitHub Desktop.
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 Github Gist Edit Link | |
// @namespace https://gist.github.com/gregory-seidman/dd8b7a93f4603ef3e484c82d45809a95 | |
// @version 1.1.0 | |
// @description Add an edit links on your own list page | |
// @downloadURL https://gist.github.com/gregory-seidman/67c2186bdf6496f4f238209943cdc1b5/raw/github_gist_edit_link.user.js | |
// @updateURL https://gist.github.com/gregory-seidman/67c2186bdf6496f4f238209943cdc1b5/raw/github_gist_edit_link.user.js | |
// @author Gregory Seidman | |
// @match https://gist.github.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=github.com | |
// @grant none | |
// @run-at document-idle | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const html = `<a class="btn btn-sm" aria-label="Edit this Gist" rel="nofollow"> | |
<svg aria-hidden="true" height="16" viewBox="0 0 16 16" version="1.1" width="16" data-view-component="true" class="octicon octicon-pencil"> | |
<path d="M11.013 1.427a1.75 1.75 0 0 1 2.474 0l1.086 1.086a1.75 1.75 0 0 1 0 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 0 1-.927-.928l.929-3.25c.081-.286.235-.547.445-.758l8.61-8.61Zm.176 4.823L9.75 4.81l-6.286 6.287a.253.253 0 0 0-.064.108l-.558 1.953 1.953-.558a.253.253 0 0 0 .108-.064Zm1.238-3.763a.25.25 0 0 0-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 0 0 0-.354Z"></path> | |
</svg> Edit </a>` | |
const listPathRE = /^\/([^/]+)(\/|\/public|\/private)?$/; | |
const endUrlRE = /\/?$/; | |
let tamperTimeout = null; | |
function tamper() { | |
tamperTimeout = null; | |
const match = listPathRE.exec(window.location.pathname); | |
if (!match) { | |
// not a user's gist list page | |
return; | |
} | |
// Bonus! Get rid of the link overlay from the snippets. Why is | |
// that even a thing? Nobody wants that bullshit. | |
const linkOverlays = document.querySelectorAll( | |
'.gist-snippet a.link-overlay'); | |
Array.prototype.forEach.call(linkOverlays, | |
e => e.parentNode.removeChild(e)) | |
const avatar = document.querySelector( | |
'.Header .Header-link.name img.avatar-user[alt^="@"]'); | |
if (!avatar) { | |
// not logged in | |
return; | |
} | |
const user = avatar.alt.substring(1); | |
if (user.toLowerCase() !== match[1].toLowerCase()) { | |
// listing another user's gists | |
return; | |
} | |
const links = Array.prototype.slice.call( | |
document.querySelectorAll( | |
'.gist-snippet li a[href$="/stargazers"]')); | |
for (let i=0; i<links.length; ++i) { | |
const link = links[i]; | |
const container = document.createElement('li'); | |
container.innerHTML = html; | |
container.className = 'd-inline-block'; | |
container.firstChild.href = link.href.replace(/\/stargazers$/, | |
'/edit'); | |
link.parentNode.parentNode.appendChild(container); | |
} | |
} | |
if (!window.history.yesIreplacedReplaceState) { | |
const oldReplace = window.history.replaceState; | |
function replaceState() { | |
if (tamperTimeout !== null) { | |
window.clearTimeout(tamperTimeout); | |
} | |
tamperTimeout = window.setTimeout(tamper, 100); | |
return oldReplace.apply(window.history, arguments); | |
} | |
window.history.yesIreplaced_replaceState = true; | |
window.history.replaceState = replaceState; | |
tamperTimeout = window.setTimeout(tamper, 0); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment