Skip to content

Instantly share code, notes, and snippets.

@maxkerp
Last active March 23, 2021 15:55
Show Gist options
  • Select an option

  • Save maxkerp/3e50164b06598816fe556acf6246543a to your computer and use it in GitHub Desktop.

Select an option

Save maxkerp/3e50164b06598816fe556acf6246543a to your computer and use it in GitHub Desktop.

Revisions

  1. maxkerp revised this gist Feb 8, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions copy_from_webpage_to_clipboard.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    (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, we had to add the button to the top of the page.
    // 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
    @@ -32,7 +32,7 @@
    });
    }

    const ticketNumber = $('#content h2').text();
    const ticketNumber = $('#content h2').first().text().match(/#\d*/)[0];
    const ticketTitle = $('#content .subject h3').text();

    const textToCopy = `${ticketTitle} ${ticketNumber} - `;
    @@ -48,7 +48,7 @@

    copyTextToClipboardButton(
    textToCopy,
    'Copy Ticket for ...',
    'Copy Ticket for Tyme',
    buttonStyle
    );
    })()
  2. maxkerp created this gist Feb 8, 2020.
    54 changes: 54 additions & 0 deletions copy_from_webpage_to_clipboard.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    (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, we 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').text();
    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 ...',
    buttonStyle
    );
    })()