mariotatis / Jira Cards Context Menu Additions

// ==UserScript==
// @name           Jira Cards Context Menu Additions
// @author         mariotatis
// @namespace      https://openuserjs.org/users/mariotatis/scripts
// @description    Add new actions to Jira Cards context menu
// @icon           https://www.mariotatis.com/wp-content/uploads/2020/05/favicon-32x32-1.png
// @copyright      2020+, mariotatis.com
// @version        1.0.3
// @license        MIT
// @require        http://code.jquery.com/jquery-3.6.0.min.js
// @grant          none
// @include        /^https?:\/\/.*atlassian.*
// ==/UserScript==
​
var $ = window.jQuery;
​
window.addEventListener('load', function() {
​
    $(".ghx-issue").contextmenu(function() {
        setTimeout(function() {
            $(`<li id="ghx-issue-ctx-action-copy-card-name-container" class="aui-list-item">
                   <a class="aui-list-item-link" title="Copy card name" id="ghx-issue-ctx-action-copy-card-name" href="#">Copy card name</a>
               </li>`)
            .insertAfter('#ghx-issue-ctx-action-copy-issue-link-container');
        }, 10);
    });
​
    function copyToClipboard(text) {
        if (window.clipboardData && window.clipboardData.setData) {
            return window.clipboardData.setData("Text", text);
        }
        else if (document.queryCommandSupported && document.queryCommandSupported("copy")) {
            var textarea = document.createElement("textarea");
            textarea.textContent = text;
            textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge.
            document.body.appendChild(textarea);
            textarea.select();
            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(textarea);
            }
        }
    }
​
    $(document).on('click', '#ghx-issue-ctx-action-copy-card-name', function() {
        copyToClipboard($('.ghx-selected > section:first').text());
    });
​
    $(document).on('mouseenter', '#ghx-issue-ctx-action-copy-card-name-container', function() {
        $(".aui-list-item").removeClass('active');
        $('#ghx-issue-ctx-action-copy-card-name-container').addClass('active');
    });
​
    $(document).on('mouseleave', '#ghx-issue-ctx-action-copy-card-name-container', function() {
        $('#ghx-issue-ctx-action-copy-card-name-container').removeClass('active');
    });
​
})();