Copy Code in Code Block Footer

Duplicate the header of code blocks as a footer to give a second copy code button

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Copy Code in Code Block Footer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Duplicate the header of code blocks as a footer to give a second copy code button
// @author       Gavin Trutzenbach / @gaveroid
// @match        https://chat.openai.com/*
// ==/UserScript==

(function() {
    'use strict';

    // Helper function to copy text to clipboard
    function copyToClipboard(text) {
        var dummy = document.createElement("textarea");
        document.body.appendChild(dummy);
        dummy.value = text;
        dummy.select();
        document.execCommand("copy");
        document.body.removeChild(dummy);
    }

    // Function to process code blocks
    function processCodeBlocks() {
        var codeBlocks = document.querySelectorAll('div[class^="bg-black"]');
        codeBlocks.forEach(function(codeBlock) {
            var headerDiv = codeBlock.querySelector('div[class^="flex items-center"]');
            if (headerDiv && !codeBlock.querySelector('.duplicated-footer')) {
                var footerDiv = headerDiv.cloneNode(true);
                footerDiv.classList.add('duplicated-footer');
                codeBlock.appendChild(footerDiv);

                // Update copy button function
                var updateCopyButtonFunction = function(button) {
                    var originalInnerHTML = button.innerHTML;  // Store the original inner HTML
                    var copyFunction = function() {
                        var codeText = codeBlock.querySelector('code').innerText;
                        copyToClipboard(codeText);
                        this.innerHTML = '✔ Copied!';  // ASCII icon for checkmark
                        var btn = this;
                        setTimeout(function() {
                            btn.innerHTML = originalInnerHTML;  // Revert to original inner HTML
                        }, 3000);  // Revert back after 3 seconds
                    };
                    button.removeEventListener('click', copyFunction);  // Remove old event listener
                    button.addEventListener('click', copyFunction);  // Add new event listener
                };

                // Update copy buttons in header and footer
                var copyButtonHeader = headerDiv.querySelector('button');
                var copyButtonFooter = footerDiv.querySelector('button');
                updateCopyButtonFunction(copyButtonHeader);
                updateCopyButtonFunction(copyButtonFooter);
            }
        });
    }

    // Create a MutationObserver to monitor the DOM for changes
    var observer = new MutationObserver(function(mutationsList, observer) {
        processCodeBlocks();
    });

    // Start observing the document with the configured parameters
    observer.observe(document, { childList: true, subtree: true });
})();