Last active
July 1, 2022 09:26
-
-
Save andfinally/39a3c8abbbfad4c17adfb2e8ea8ee4bc to your computer and use it in GitHub Desktop.
Greasemonkey script for GitHub pull requests and issues. Adds icon after title which, if clicked, copies the markup for a link to the PR to the clipboard
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 PR – click to copy link | |
// @version 0.1 | |
// @description Adds a "copy" link to the title of GitHub PR or issue. On clicking on it, copies link with title in text form to clipboard. | |
// @author andfinally | |
// @match https://github.com/*/pull/* | |
// @match https://github.com/*/issues/* | |
// @icon https://www.google.com/s2/favicons?domain=github.com | |
// @grant none | |
// ==/UserScript== | |
'use strict'; | |
if (document.readyState === 'loading') { | |
document.addEventListener('DOMContentLoaded', afterDOMLoaded); | |
} else { | |
afterDOMLoaded(); | |
} | |
function afterDOMLoaded() { | |
// Get PR title and URL | |
const metaTag = document.querySelector("meta[property='og:url']"); | |
if (!metaTag) { | |
return; | |
} | |
const url = metaTag.getAttribute('content'); | |
const h1 = document.querySelector('h1.gh-header-title'); | |
if (!h1) { | |
return; | |
} | |
const title = h1.querySelector('.js-issue-title').textContent; | |
// Add div to copy linked title | |
const div = document.createElement('div'); | |
div.style.display = 'inline-block'; | |
div.style.color = 'rgb(139, 148, 158)'; | |
div.style.cursor = 'copy'; | |
div.style.width = '32px'; | |
div.style.height = '32px'; | |
div.style.fontSize = '32px'; | |
div.innerText = '⎘'; | |
const blob = new Blob(['<a href="' + url + '">' + title + '</a>'], { type: 'text/plain' }); | |
const clipboardData = [new window.ClipboardItem({ ['text/plain']: blob })]; | |
// Add event listener to save productId in clipboard | |
div.addEventListener('click', () => { | |
navigator.clipboard.write(clipboardData); | |
}); | |
h1.insertAdjacentElement('beforeend', div); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment