Copy table

Adds a button to copy tables to the clipboard in HTML format for pasting into Word with full borders.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Copy table
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a button to copy tables to the clipboard in HTML format for pasting into Word with full borders.
// @author       Bui Quoc Dung
// @match        *://*/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Add some CSS for the copy button
    GM_addStyle(`
        .copy-table-btn {
            margin-top: 5px;
            padding: 5px 10px;
            font-size: 13px;
            background: #333333;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
    `);

    // Function to add the "Copy to Word" button below a table element
    function addCopyButtonToTable(table) {
        // Avoid adding duplicate buttons
        if (table.nextElementSibling && table.nextElementSibling.classList.contains('copy-table-btn')) {
            return;
        }

        // Ensure all table cells have borders
        table.style.borderCollapse = 'collapse';
        table.querySelectorAll('th, td').forEach(cell => {
            cell.style.border = '1px solid black';
            cell.style.padding = '4px';
        });

        // Create the button element
        const btn = document.createElement('button');
        btn.textContent = 'Copy';
        btn.className = 'copy-table-btn';
        btn.addEventListener('click', function(e) {
            e.stopPropagation();
            e.preventDefault();
            copyTableToClipboard(table);
        });

        // Insert button after the table
        table.parentElement.insertBefore(btn, table.nextSibling);
    }

    // Function that copies the table's HTML to the clipboard
    function copyTableToClipboard(table) {
        const html = table.outerHTML;
        // Try using the Clipboard API with HTML support
        if (navigator.clipboard && navigator.clipboard.write) {
            const type = "text/html";
            const blob = new Blob([html], { type: type });
            const data = [new ClipboardItem({ [type]: blob })];
            navigator.clipboard.write(data).catch(err => {
                console.error('Error copying table: ', err);
                fallbackCopy(html);
            });
        } else {
            fallbackCopy(html);
        }
    }

    // Fallback copy function using a temporary element and execCommand
    function fallbackCopy(html) {
        const tempDiv = document.createElement('div');
        tempDiv.style.position = 'absolute';
        tempDiv.style.left = '-9999px';
        tempDiv.innerHTML = html;
        document.body.appendChild(tempDiv);
        const range = document.createRange();
        range.selectNodeContents(tempDiv);
        const sel = window.getSelection();
        sel.removeAllRanges();
        sel.addRange(range);
        try {
            document.execCommand('copy');
        } catch (err) {
            console.error('Fallback: Unable to copy', err);
        }
        sel.removeAllRanges();
        document.body.removeChild(tempDiv);
    }

    // Observe the document for new tables (for dynamically loaded content)
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1) { // ELEMENT_NODE
                    if (node.tagName === 'TABLE') {
                        addCopyButtonToTable(node);
                    } else {
                        const tables = node.querySelectorAll('table');
                        tables.forEach(addCopyButtonToTable);
                    }
                }
            });
        });
    });
    observer.observe(document.body, { childList: true, subtree: true });

    // Initially add buttons to any tables already on the page
    document.querySelectorAll('table').forEach(addCopyButtonToTable);
})();