Copy table as Markdown

Copy table as Markdown to clipboard

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Copy table as Markdown
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  Copy table as Markdown to clipboard
// @match      *://*/*
// @grant none
// @author       replica
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let NL = "\n";

    init();
    // Find the table element
    function init() {
        const tables = document.querySelectorAll('table');
        console.log(tables.length);
        if (tables) {
            for(let table of tables) {
                // Create a new button element
                const button = document.createElement('button');
                button.textContent = 'Copy';

                // Set the button's style and position
                button.style.position = 'relative';
                button.style.top = '0';
                button.style.right = '0';
                button.style.zIndex = '9999';

                // Add a click event listener to the button
                button.addEventListener('click', () => {
                    const markdown = convertTable(table);
                    navigator.clipboard.writeText(markdown).then(function() {
                        notification("Copied");
                        console.log('Copying to clipboard was successful!');
                    }, function(err) {
                        console.error('Could not copy text: ', err);
                    });
                });
                table.insertBefore(button, table.firstChild);
            }
        }
    }

    function notification(text) {
        let notification = document.createElement('div');
        notification.textContent = text;
        notification.style.position = 'fixed';
        notification.style.top = '20px';
        notification.style.right = '20px';
        notification.style.padding = '10px';
        notification.style.backgroundColor = '#323232';
        notification.style.color = '#fff';
        notification.style.borderRadius = "4px";
        notification.style.opacity = 0;
        notification.style.zIndex = '9999';
        notification.style.transition = 'opacity 0.5s ease-in-out';
        document.body.appendChild(notification);

        notification.style.opacity = '1';

        // 设置3秒后自动消失
        setTimeout(function() {
            notification.style.opacity = '0';
        }, 1000);
    }


    function convertTable(table) {
        let markdownResults = '';
        let markdownTable = convertTableElementToMarkdown(table);
        markdownResults += markdownTable + NL + NL;
        return markdownResults;
    }

    // https://github.com/johnbeech/html-table-to-markdown-converter/blob/master/table-converter.js
    function convertTableElementToMarkdown(tableEl) {
        let rows = [];
        let trEls = tableEl.getElementsByTagName('tr');
        for(let i=0; i<trEls.length; i++) {
            let tableRow = trEls[i];
            let markdownRow = convertTableRowElementToMarkdown(tableRow, i);
            rows.push(markdownRow);
        }
        return rows.join(NL);
    }

    function convertTableRowElementToMarkdown(tableRowEl, rowNumber) {
        let cells = [];
        let cellEls = tableRowEl.children;
        for(let i=0; i<cellEls.length; i++) {
            let cell = cellEls[i];
            cells.push(cell.innerText + ' |');
        }
        let row = '| ' + cells.join(" ");

        if(rowNumber == 0) {
            row = row + NL + createMarkdownDividerRow(cellEls.length);
        }

        return row;
    }

    function createMarkdownDividerRow(cellCount) {
        let dividerCells = [];
        for(let i = 0; i<cellCount; i++) {
            dividerCells.push('---' + ' |');
        }
        return '| ' + dividerCells.join(" ");
    }
})();