Skip to content

Instantly share code, notes, and snippets.

@maxkerp
Last active March 23, 2021 15:55
Show Gist options
  • Save maxkerp/3e50164b06598816fe556acf6246543a to your computer and use it in GitHub Desktop.
Save maxkerp/3e50164b06598816fe556acf6246543a to your computer and use it in GitHub Desktop.
(function(){
// This snippet is a customized version of the code found on
// stackoverflow. Since firefox only copies to the clipboard when the event
// was triggered by a user, I had to add the button to the top of the page.
// In chrome it might work without the need for a button, directly from a
// bookmarklet. See the following for more information.
// https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript?page=1&tab=votes#tab-top
// To create a bookmarklet you might want to use the following:
// https://www.yourjs.com/bookmarklet/
function copyTextToClipboardButton(copyText, buttonText, buttonStyle) {
// Firefox only copies any text if the code doing it was triggered by a user
// event. This is why we have to insert a button at the top of the page.
var submitButton = document.createElement("button");
Object.assign(submitButton.style, buttonStyle)
submitButton.innerHTML = buttonText
document.body.appendChild(submitButton);
submitButton.addEventListener('click', function() {
navigator.clipboard.writeText(copyText).then(function() {
console.log(`Async: Copying to clipboard was successful!\n String was: ${copyText}`);
}, function(err) {
console.error('Async: Could not copy text: ', err);
});
document.body.removeChild(submitButton);
});
}
const ticketNumber = $('#content h2').first().text().match(/#\d*/)[0];
const ticketTitle = $('#content .subject h3').text();
const textToCopy = `${ticketTitle} ${ticketNumber} - `;
// Will be assigned to HTMLElement.style
const buttonStyle = {
position: 'fixed',
top: 0,
left: 0,
width: '100%',
fontWeight: 'bold'
}
copyTextToClipboardButton(
textToCopy,
'Copy Ticket for Tyme',
buttonStyle
);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment