Confluence Jira 复制标题和链接

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

2024-10-15 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         Confluence Jira 复制标题和链接
// @description:zh-CN 点击按钮以markdown格式复制标题文本+链接
// @namespace    http://tampermonkey.net/
// @version      0.1
// @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';

    function addButton() {
        if (!document.getElementById('customCopyButton')) {
            const button = document.createElement('button');
            button.id = 'customCopyButton';
            button.textContent = 'Copy Title & Link';
            button.style.position = 'fixed';
            button.style.top = '20px';
            button.style.right = '20px';
            button.style.padding = '10px 15px';
            button.style.backgroundColor = '#0052CC';
            button.style.color = 'white';
            button.style.border = 'none';
            button.style.borderRadius = '5px';
            button.style.cursor = 'pointer';
            button.style.zIndex = '1000';
            document.body.appendChild(button);

            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);
            });
        }
    }

    // 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();
})();