// ==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 // @downloadURL none // ==/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 = "