Last active
October 12, 2020 17:50
-
-
Save michal-ciechan/06e4c614ea51468e8711b557c3ff7e9f to your computer and use it in GitHub Desktop.
Bookmarklet - SNOW - Create HTML Link
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
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 iframe = document.getElementById("gsft_main"); | |
var doc = iframe?.contentWindow?.document || document; | |
var numberInput = doc.querySelectorAll('[aria-label="Number"]'); | |
if (!numberInput || numberInput.length == 0) { | |
throw 'Could not find Ticket Number Input by Query Selector [aria-label="Number"]'; | |
} | |
var shortDescInput = doc.querySelectorAll('[aria-label="Short description"]'); | |
if (!shortDescInput || shortDescInput.length == 0) { | |
throw 'Could not find Short Description Input by Query Selector [aria-label="Short description"]'; | |
} | |
var ticketNumber = numberInput[0].value; | |
var shortDescription = shortDescInput[0].value; | |
var href = window.location.href; | |
var title = '[' + ticketNumber + '] ' + shortDescription; | |
console.log(title); | |
copyLinkToClipboard(title, href); | |
})(); | |
/* | |
document.getElementById("gsft_main").contentWindow.document.querySelectorAll('[aria-label="Short description"]'); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment