Skip to content

Instantly share code, notes, and snippets.

@michal-ciechan
Last active December 2, 2021 10:22
Show Gist options
  • Save michal-ciechan/12debc4421bdd0b265d69c6a1f8b987a to your computer and use it in GitHub Desktop.
Save michal-ciechan/12debc4421bdd0b265d69c6a1f8b987a to your computer and use it in GitHub Desktop.
Copy JIRA as Branch Name
javascript:(function() {
function selectNode(node) {
const range = document.createRange();
range.selectNode(node);
const selection = window.getSelection();
selection.removeAllRanges();
selection.addRange(range);
}
function copyLinkToClipboard(text, url) {
if (window.clipboardData && window.clipboardData.setData) {
/*IE specific code path to prevent textarea being shown while dialog is visible.*/
return clipboardData.setData("Text", text);
} else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
var a = document.createElement("a");
var linkText = document.createTextNode(text);
a.appendChild(linkText);
a.title = linkText;
a.href = url;
a.style.position = "fixed"; /* Prevent scrolling to bottom of page in MS Edge.*/
document.body.appendChild(a);
selectNode(a);
try {
return document.execCommand("copy"); /* Security exception may be thrown by some browsers.*/
} catch (ex) {
console.warn("Copy to clipboard failed.", ex);
return false;
} finally {
document.body.removeChild(a);
}
}
}
var href = window.location.href;
var title = document.title;
var normalizedTitle = title.replace("[", "").replace("]", "").replace(" - Jira", "").replace(/[^A-Za-z0-9]/g, "-").replace(/([^A-Za-z0-9])\1+/g, '$1').replace(/-$/, '');
var match = normalizedTitle.match(/(?<id>[A-Za-z0-9]+-[0-9]+)-(?<name>.*)/);
var id = match.groups.id;
var name = match.groups.name.toLowerCase();
var text = id + "-" + name;
console.log(text);
copyLinkToClipboard(text, href);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment