Greasy Fork

Confluence Jira 复制标题和链接

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

目前为 2024-10-15 提交的版本。查看 最新版本

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