🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Greasy Fork is available in English.

Copy table

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

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