Confluence Jira 复制标题和链接

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

目前為 2024-10-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();