Last active
October 21, 2024 18:38
-
-
Save fengtan/4493530e0989b896692d3804937e17a9 to your computer and use it in GitHub Desktop.
Generate hyperlinks for Jira, Gitlab, ServiceNow or drupal.org and copy to clipboard
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 Generate hyperlinks for Jira, Gitlab, ServiceNow or drupal.org and copy to clipboard | |
// @namespace https://gist.github.com/fengtan/4493530e0989b896692d3804937e17a9 | |
// @version 1.1 | |
// @description View a page in Jira, Gitlab, ServiceNow or drupal.org, and then hit Alt+H, Alt+M or Alt+J: this will copy an HTML, markdown or jira-formatted hyperlink of this page to the system clipboard | |
// @author fengtan | |
// @include *gitlab* | |
// @include *jira*/browse/* | |
// @include *jira*/issues/* | |
// @include *.service-now.com/* | |
// @include *.drupal.org/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function copyToClipboard(copy) { | |
// Copy to clipboard (plain - works on all browsers) | |
/* | |
const element = document.createElement('textarea'); | |
element.value = copy; | |
element.setAttribute('readonly', ''); | |
element.style.position = 'absolute'; | |
element.style.left = '-9999px'; | |
document.body.appendChild(element); | |
element.select(); | |
document.execCommand('copy'); | |
document.body.removeChild(element); | |
*/ | |
// Copy to clipboard (rich content - works only on modern browsers) | |
function listener(event) { | |
event.clipboardData.setData("text/html", copy); | |
event.clipboardData.setData("text/plain", copy); | |
event.preventDefault(); | |
} | |
document.addEventListener("copy", listener); | |
document.execCommand("copy"); | |
document.removeEventListener("copy", listener); | |
// Inform user. | |
alert("Copied to clipboard:\n\n"+copy); | |
}; | |
document.addEventListener('keyup', function(e) { | |
var event = window.event || e; | |
// Listen for Alt key. | |
if (event.altKey) { | |
// Go through the list of supported URLs (see @include rules above) and extract hyperlink text & url. | |
// The most reliable patterns go first (the format of ServiceNow urls is hard to predict). | |
var href = window.location.href; | |
var url; | |
var text; | |
if (href.indexOf('gitlab') > -1) { | |
var jsonld = JSON.parse(document.querySelector('script[type="application/ld+json"]').innerText); | |
var lastCrumb = jsonld.itemListElement.slice(-1).pop(); | |
url = lastCrumb.item; | |
text = lastCrumb.name; | |
} else if (href.indexOf('jira') > -1) { | |
url = href; | |
var ticketId = href.split("?")[0].split('/').slice(-1).pop(); | |
var ticketSummary = document.evaluate("//div[contains(@id, 'summary-val')]", document, null, XPathResult.STRING_TYPE, null).stringValue; | |
text = ticketId + " " + ticketSummary; | |
} else if (href.indexOf('service-now.com') > -1) { | |
url = href; | |
var xpath="//input[contains(@id, 'sys_readonly') and contains(@id, 'number')]/@value"; | |
var result = document.evaluate(xpath, document, null, XPathResult.STRING_TYPE, null); | |
text = result.stringValue; | |
} else if (href.indexOf('drupal.org') > -1) { | |
url = href; | |
text = href.split("?")[0].split('/').slice(-1).pop(); | |
if (!isNaN(text)) { | |
text = "#"+text; | |
} | |
} else { | |
url = href; | |
text = 'link'; | |
} | |
switch (event.keyCode) { | |
// Alt+H: build HTML and copy to clipboard. | |
case 72: | |
copyToClipboard("<a href=\"" + url + "\">" + text + "</a>"); | |
break; | |
// Alt+M: build markdown and copy to clipboard. | |
case 77: | |
copyToClipboard("[" + text + "](" + url + ")"); | |
break; | |
// Alt+J: build jira markup and copy to clipboard. | |
case 74: | |
copyToClipboard("[" + text + "|" + url + "]"); | |
break; | |
} | |
} | |
}, false); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment