Jira Copy Ticket

Adds a "Copy Ticket" button to copy the title and ticket link as rich text

Per 30-11-2023. Zie de nieuwste versie.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

// ==UserScript==
// @name         Jira Copy Ticket
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a "Copy Ticket" button to copy the title and ticket link as rich text
// @author       Othman Shareef ([email protected])
// @match        https://eventmobi.atlassian.net/*
// @icon         https://www.google.com/s2/favicons?domain=atlassian.net
// @grant        none
// @license MIT
// ==/UserScript==
(function() {
    'use strict';
    let button
    let debounceTimer;

    // Wait for the Jira ticket title element to be available
    const waitForElement = (selector, callback) => {
        const element = document.querySelector(selector);
        if (element) {
            callback();
        } else {
            setTimeout(() => {
                waitForElement(selector, callback);
            }, 500);
        }
    };

    // copy ticket title to clipboard
    const copyTicketTitle = () => {
        button.innerText = 'Loading...';
        const ticketTitleElement = document.querySelector('[data-testid="issue.views.issue-base.foundation.summary.heading"]');
        const ticketLinkElement = document.querySelector('[data-testid="issue.views.issue-base.foundation.breadcrumbs.current-issue.item"]');
        if (ticketTitleElement && ticketLinkElement) {
            const ticketTitle = ticketTitleElement.innerText;
            const ticketLink = ticketLinkElement.href
            const ticketID = ticketLinkElement.firstChild.innerText
            const html = `<a href="${ticketLink}">${ticketID}: ${ticketTitle}</a>`
            const clipboardItem = new ClipboardItem({
                'text/html': new Blob([html], {
                    type: 'text/html'
                }),
                'text/plain': new Blob([html], {
                    type: 'text/plain'
                })
            });
            navigator.clipboard.write([clipboardItem]).then(_ => {
                // Change button text to "Copied!" for a moment
                button.innerText = 'Copied!';
                setTimeout(() => {
                    // Change button text back to the original text after a delay
                    button.innerText = 'Copy Ticket';
                }, 1000); // You can adjust the delay (in milliseconds) as needed
            }, error => alert(error));
        } else {
            alert('Ticket title element not found!');
        }
    };

    // Add button next to the ticket title
    const addButton = () => {
        const existingCopyBtn = document.getElementById("copy-button")
        if(existingCopyBtn) return
        const copyButton = document.createElement('button');
        copyButton.innerText = 'Copy Ticket';
        copyButton.className = 'css-1xewsy6';
        copyButton.id = "copy-button"
        copyButton.style.marginLeft = '10px';
        copyButton.addEventListener('click', copyTicketTitle);

        // Add hover and click effects
        copyButton.addEventListener('mouseover', () => {
            copyButton.style.filter = 'brightness(2) contrast(2)';
        });
        copyButton.addEventListener('mouseout', () => {
            copyButton.style.filter = 'none';
        });
        copyButton.addEventListener('mousedown', () => {
        copyButton.style.filter = 'brightness(0.1) contrast(0.1)';
        });
        copyButton.addEventListener('mouseup', () => {
            copyButton.style.filter = 'none';
        });

        button = copyButton
        const element = document.querySelector('[data-testid="issue.views.issue-base.foundation.quick-add.link-button.ui.link-dropdown-button"]');
        element.parentElement.parentElement.parentElement.appendChild(copyButton);
    };

    const debounce = (func, delay) => {
        clearTimeout(debounceTimer);
        debounceTimer = setTimeout(func, delay);
    };

            // Use MutationObserver to detect changes in the DOM
    const observer = new MutationObserver(() => {
        debounce(() => {
            waitForElement('[data-testid="issue.views.issue-base.foundation.summary.heading"]', addButton)
        }, 100);
    });

    // Observe changes in the body and its descendants
    observer.observe(document.body, {
        childList: true,
        subtree: true,
    });

})();