jira复制

在jira上复制id和标题,另外还可以复制评论中的文字

As of 2024-01-26. See the latest version.

// ==UserScript==
// @name         jira复制
// @namespace    http://tampermonkey.net/
// @version      1.0.3
// @description  在jira上复制id和标题,另外还可以复制评论中的文字
// @author       Kovan
// @match        http://*/browse/INOR*
// @icon         https://www.google.com/s2/favicons?domain=192.168.0.1
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
  'use strict';

  function add_copy_btn(){
    const elem = document.getElementById("summary-val");
    const keyVal = document.getElementById("key-val");
    let kk_copy_btn = document.getElementById('kk-copy-button');
    if (elem && keyVal && !kk_copy_btn) {
        let kk_copy_btn_style = document.createElement('style');
        kk_copy_btn_style.innerHTML = `
          #kk-copy-button {
            position: relative;
            z-index: 9999;
            height: 34px;
            width: 90px;
            background-color: #dfe1e5;
            color: #172b4d;
            border: none;
            border-radius: 5px;
            margin: 0 25px;
            cursor: pointer;
            font-size: 14px;
            font-weight:400;
            cursor:pointer;
            transition: background-color 0.5s, color 0.5s; /* 添加过渡效果,0.5秒的过渡时间 */
          }
          #kk-copy-button:hover {
            background-color: #b3b6bb; /* hover时的背景颜色 */
            color: #172b4d; /* hover时的文字颜色 */
          }
        `;
        document.head.appendChild(kk_copy_btn_style);
        kk_copy_btn = document.createElement("button");
        kk_copy_btn.id = 'kk-copy-button';
        kk_copy_btn.textContent = "COPY";
        kk_copy_btn.setAttribute("title", "点击复制问题和ID");
        elem.after(kk_copy_btn);
        kk_copy_btn.addEventListener('click', () => {
            // 复制文本
            if (!navigator.clipboard) {
                // 使用其他方式实现复制
                const textToCopy = keyVal.innerText + elem.innerText;
                const tmp = document.createElement('TEXTAREA');
                tmp.value = textToCopy;
                document.body.appendChild(tmp);
                tmp.select();
                document.execCommand('copy');
                document.body.removeChild(tmp);
                showNotification(' 文本已成功复制 ');
            }
            // Hide the notification after 3 seconds
            setTimeout(() => {
                document.body.removeChild(notificationDiv);
            }, 2000);
        });

    }
    console.log("已添加copy按钮");
  }


  /**创建复制评论按钮**/
  function add_response_btn(){
      // 找到目标节点
      let targetNodes = document.querySelectorAll('.action-body');
      console.log(targetNodes.length);
      var t_res_btn = document.getElementsByName('kk-response-btn');
      var t_res_message = document.getElementsByClassName('message-container');
      console.log("t_res_message.length:"+t_res_message.length);
      // 将按钮插入到目标节点的后面
      if (targetNodes.length > 0 && t_res_btn.length != targetNodes.length && t_res_message.length == 0) {
        targetNodes.forEach(function(targetNode, index) {
            // 创建按钮
            const newButton = document.createElement('button');
            newButton.textContent = '复制评论';
            newButton.setAttribute("name", "kk-response-btn");
            newButton.style.position = "absolute";
            newButton.style.display = "block";
            newButton.style.top = "1px";
            newButton.style.right = "4px";
            targetNode.parentNode.insertBefore(newButton, targetNode);
            // 添加按钮点击事件
            newButton.addEventListener('click', function() {
                // 在这里添加按钮点击事件的逻辑
                // 复制文本
                if (!navigator.clipboard) {
                    // 使用其他方式实现复制
                    const textToCopy = targetNode.innerText;
                    const tmp = document.createElement('TEXTAREA');
                    tmp.value = textToCopy;
                    document.body.appendChild(tmp);
                    tmp.select();
                    document.execCommand('copy');
                    document.body.removeChild(tmp);
                    showNotification(' 文本已成功复制 ');
                }
                // Hide the notification after 3 seconds
                setTimeout(() => {
                    document.body.removeChild(notificationDiv);
                }, 2000);
            });
        });
      }
  }

  // 定义检查元素存在的函数
  function checkElementsExist() {
      var t_btn = document.getElementById("kk-copy-button");
      let t_res_btn = document.getElementsByName("kk-response-btn");
      var t_res_message = document.getElementsByClassName('message-container');
      if (t_btn && t_res_btn.length > 0) {
          clearInterval(intervalId);
          return;
      }
      if(!t_btn){
          add_copy_btn();
      }
      if(t_res_message.length == 0){
          add_response_btn();
      }
  }


  // 在页面加载时检查是否存在标记
  window.onload = function() {
        let intervalId = setInterval(checkElementsExist, 200);
  };

  // 设置初始的间隔函数
  let intervalId = setInterval(checkElementsExist, 200);

  let notificationDiv; // Declare notificationDiv globally
  function showNotification(message) {
    notificationDiv = document.createElement('div');
    notificationDiv.textContent = message;
    notificationDiv.style.position = 'fixed';
    notificationDiv.style.top = '20px';
    notificationDiv.style.left = '50%';
    notificationDiv.style.transform = 'translateX(-50%)';
    notificationDiv.style.padding = '10px';
    notificationDiv.style.background = '#4CAF50';
    notificationDiv.style.color = 'white';
    notificationDiv.style.borderRadius = '5px';
    notificationDiv.style.zIndex = '9999';
    notificationDiv.style.pointerEvents = 'none'; // Disable pointer events for the notification
    notificationDiv.setAttribute('tabindex', '-1'); // Make the div non-focusable
    notificationDiv.style.outline = 'none'; // Remove visible outline
    document.body.appendChild(notificationDiv);
  }

})();