jrwarwick / Jira Issue Reference to Clipboard

// ==UserScript==
// @name         Jira Issue Reference to Clipboard
// @namespace    https://jrwarwick.github.io/
// @version      1.1
// @description  This script is written to allow easy copying of Task Title and Description to Clipboard, and Customer Approval Portal link, too.
// @author       Wieslaw Mosakowski, Justin Warwick
// @license      MIT
// @match        https://onprem-jira.domain.com/browse/*
// @match        https://onprem-jira.domain.com/projects/*/queues/issue/*
// @require      https://cdn.jsdelivr.net/npm/clipboard@1/dist/clipboard.min.js
// @require      https://cdn.jsdelivr.net/npm/notifyjs-browser@0.4.2/dist/notify.js
// @icon         https://marketplace.atlassian.com/s/images/favicon.ico
// ==/UserScript==
// Based on original https://openuserjs.org/meta/wmosakowskicraft/Jira_Task_to_Clipboard.meta.js by Wieslaw Mosakowski.


var clipboardIssue = new Clipboard('.extBtn1', {
    text: function(trigger) {
        return document.querySelector("#key-val").innerText.trim() + ': "' + document.querySelector("#summary-val").innerText.trim() + '"';
    }
});

var clipboardApproval = new Clipboard('.extBtn2', {
    text: function(trigger) {
        //TODO: somehow determine if this issue even has approvals. If not, don't show this button...
        //Some of these element IDs to change over different versions of jira/jirasd/jirasm.
        var issueHref = document.querySelector("#customer-request-details-web-panel-root div.sd-rt-preview > a").href
        return issueHref;
    }
});

clipboardIssue.on('success', function(e) {
    //TODO: replace this call with a direct read of clipboard, replace colon with newline. more confirmatory that way.
    $.notify("Copied to clipboard:\n" + $('#key-val').text() + ': \n' + $('#summary-val').text(), "success");
});
clipboardIssue.on('error', function(e) {
    $.notify("Could not grab that URL", "error");
});

clipboardApproval.on('success', function(e) {
    $.notify("Portal URL Copied to clipboard:\n" + e.text, "success");
    //TODO: replace this call with a direct read of clipboard, replace colon with newline. more confirmatory that way.
    //$.notify($('#key-val').text() + ' \n' + $('#summary-val').text(), "success");
});
clipboardApproval.on('error', function(e) {
    $.notify("Could not grab that URL", "error");
});

function recollectPastables(enode) {
    if(!$('#clipboardBtnSummary').next().length) {
        $('#clipboardBtnSummary').remove();
        $($("#summary-val").parents()[0]).append('<button id="clipboardBtnSummary" class="extBtn1 aui-button aui-button-compact"><span class="aui-icon aui-icon-small aui-iconfont-copy-clipboard">Copy summary to clipboard</span></button>');
    }

    if(!$('#clipboardBtnPortal').next().length) {
        $('#clipboardBtnPortal').remove();
        $($("#summary-val").parents()[0]).append('<button id="clipboardBtnPortal" class="extBtn2 aui-button aui-button-compact"><span class="aui-icon aui-icon-small aui-iconfont-checkbox">Copy Approval Portal hyperlink to clipboard</span></button>');
    }
}

$(document).ajaxComplete(function(evt) {
    recollectPastables(evt);
});

$( window ).on( 'hashchange', function(evt) { //weered. why doesn't this work.
    console.log( '[jira clipboards] hash changed!' );
    //recollectPastables(evt);
});


(function() {
    'use strict';

})();