Forked from fengtan/hyperlinks-html-markdown-jira.js
Created
November 9, 2022 14:26
-
-
Save vbrozik/fc710e65bcf19309c87c32e139b706ee 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.0 | |
// @description View a page on Jira, Gitlab, ServiceNow or drupal.org, and then hit Alt+M or Alt+J: this will build a markdown or jira-formatted hyperlink to the 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. | |
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); | |
// 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; | |
text = href.split("?")[0].split('/').slice(-1).pop(); | |
} 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+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