Greasy Fork

Greasy Fork is available in English.

HTML表格转化为Markdown表格

在指定网站的表格上方添加按钮,点击将表格以Markdown格式复制到剪贴板

当前为 2022-12-10 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         HTML表格转化为Markdown表格
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  在指定网站的表格上方添加按钮,点击将表格以Markdown格式复制到剪贴板
// @author       You
// @match        https://blog.csdn.net/*
// @match        https://www.cnblogs.com/*
// @match        https://www.runoob.com/*
// @match        https://www.jianshu.com/*
// @match        https://*.zhihu.com/*
// @match        https://www.quanxiaoha.com/*
// @match        https://www.geeksforgeeks.org/*
// @match        https://online.stat.psu.edu/stat800/*
// @match        https://www.javatpoint.com/*
// @match        https://cloud.tencent.com/*
// @match        https://scikit-learn.org/*
// @match        https://www.w3school.com.cn/*
// @match        http://c.biancheng.net/*
// @match        https://juejin.cn/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=csdn.net
// @grant        GM.setClipboard
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';


    const NL = "\n";

    function convertTableElementToMarkdown(tableEl) {
        const rows = [];
        const trEls = tableEl.getElementsByTagName('tr');
        for (let i = 0; i < trEls.length; i++) {
            const tableRow = trEls[i];
            const markdownRow = convertTableRowElementToMarkdown(tableRow, i);
            rows.push(markdownRow);
        }
        return rows.join(NL);
    }

    function convertTableRowElementToMarkdown(tableRowEl, rowNumber) {
        const cells = [];
        const cellEls = tableRowEl.children;
        for (let i = 0; i < cellEls.length; i++) {
            const 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) {
        const dividerCells = [];
        for (let i = 0; i < cellCount; i++) {
            dividerCells.push('---' + ' |');
        }
        return '| ' + dividerCells.join(" ");
    }


    function convertTable(x) {
        const content = "<table>" + x.innerHTML + "</table>";
        processor.innerHTML = content.replace(/\s+/g, ' ');

        const tables = processor.getElementsByTagName('table');
        let markdownResults = '';
        if (tables) {
            for (let e of tables) {
                const markdownTable = convertTableElementToMarkdown(e);
                markdownResults += markdownTable + NL + NL;
            }

            let p = document.createElement("p");
            p.innerHTML = "复制成功";
            GM.setClipboard(markdownResults);
            x.parentNode.insertBefore(p, x);


        } else {
            console.log('No table found');
        }
    }


    // 存放markdown表格
    let processor = document.createElement("processor");
    // 所有的table
    let x = document.getElementsByTagName("table")

    setTimeout(f1, 500);

    function f1() {

        // 遍历table,添加button
        for (let i = 0; i < x.length; i++) {
            let button = document.createElement("button");
            button.innerText = "Copy";
            button.style.zIndex = '999';
            //console.log(1);

            // 点击按钮,进行转化
            button.addEventListener('click', function () {
                convertTable(x[i])
            })
            x[i].parentNode.insertBefore(button, x[i]);
            console.log(1);
        }
    }


})();