Confluence Jira 复制标题和链接

Add a button to copy the title and link of a Confluence page

Από την 15/10/2024. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Confluence Jira 复制标题和链接
// @description:zh-CN 点击按钮以markdown格式复制标题文本+链接
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Add a button to copy the title and link of a Confluence page
// @author      cheerchen37
// @license     MIT
// @copyright   2024, https://github.com/cheerchen37/confluence-kopipe
// @match        *://*.atlassian.net/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // 添加按钮样式和提示样式
    const style = document.createElement('style');
    style.type = 'text/css';
    style.innerHTML = `
        #customCopyButton {
            position: fixed;
            top: 20px;
            right: 470px; /* Moved left 50px from the right edge */
            padding: 10px 15px;
            background-color: #0052CC;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            z-index: 1000;
        }
        #customCopyButton:hover {
            background-color: #003380;
        }
        #copyFeedback {
            position: fixed;
            top: 50px;
            right: 470px;
            background-color: #000;
            color: #fff;
            padding: 5px 10px;
            border-radius: 5px;
            display: none;
            z-index: 1001;
        }
        #customCopyButton:hover .tooltip {
            visibility: visible;
            opacity: 1;
        }
    `;
    document.head.appendChild(style);

    function addButton() {
        if (!document.getElementById('customCopyButton')) {
            const button = document.createElement('button');
            button.id = 'customCopyButton';
            button.textContent = 'Copy Title & Link';
            document.body.appendChild(button);

            const feedback = document.createElement('div');
            feedback.id = 'copyFeedback';
            feedback.textContent = 'Copied!';
            document.body.appendChild(feedback);

            button.addEventListener('click', function() {
               let titleText = '';
                // Check if it's Confluence
                if (document.location.href.includes("wiki")) {
                    titleText = document.getElementById('title-text') ? document.getElementById('title-text').textContent : '';
                }
                // Check if it's Jira
                else if (document.location.href.includes("browse")) {
                    const jiraTitleElement = document.querySelector('h1[data-testid="issue.views.issue-base.foundation.summary.heading"]');
                    titleText = jiraTitleElement ? jiraTitleElement.textContent : '';
                }
                const pageLink = window.location.href;

                const tempTextarea = document.createElement('textarea');
                document.body.appendChild(tempTextarea);
                tempTextarea.value = `[${titleText}](${pageLink})`;
                tempTextarea.select();
                document.execCommand('copy');
                document.body.removeChild(tempTextarea);

                // 显示反馈信息
                feedback.style.display = 'block';
                setTimeout(() => { feedback.style.display = 'none'; }, 1000);
            });
        }
    }

    // Mutation Observer to monitor DOM changes
    const observer = new MutationObserver(function(mutations) {
        mutations.forEach(function(mutation) {
            if (mutation.removedNodes.length || mutation.addedNodes.length) {
                addButton();
            }
        });
    });

    const config = { childList: true, subtree: true };
    observer.observe(document.body, config);

    // Initial button add
    addButton();
})();