Table to Markdown Copier

Copy table as Markdown to clipboard

目前为 2023-12-28 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Table to Markdown Copier
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Copy table as Markdown to clipboard
  6. // @match *://*/*
  7. // @grant none
  8. // @author replica
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. let NL = "\n";
  16.  
  17. init();
  18. // Find the table element
  19. function init() {
  20. const tables = document.querySelectorAll('table');
  21. console.log(tables.length);
  22. if (tables) {
  23. for(let table of tables) {
  24. // Create a new button element
  25. const button = document.createElement('button');
  26. button.textContent = 'Copy';
  27.  
  28. // Set the button's style and position
  29. button.style.position = 'relative';
  30. button.style.top = '0';
  31. button.style.right = '0';
  32. button.style.zIndex = '9999';
  33.  
  34. // Add a click event listener to the button
  35. button.addEventListener('click', () => {
  36. const markdown = convertTable(table);
  37. navigator.clipboard.writeText(markdown).then(function() {
  38. notification("Copied");
  39. console.log('Copying to clipboard was successful!');
  40. }, function(err) {
  41. console.error('Could not copy text: ', err);
  42. });
  43. });
  44. table.insertBefore(button, table.firstChild);
  45. }
  46. }
  47. }
  48.  
  49. function notification(text) {
  50. let notification = document.createElement('div');
  51. notification.textContent = text;
  52. notification.style.position = 'fixed';
  53. notification.style.top = '20px';
  54. notification.style.right = '20px';
  55. notification.style.padding = '10px';
  56. notification.style.backgroundColor = '#323232';
  57. notification.style.color = '#fff';
  58. notification.style.borderRadius = "4px";
  59. notification.style.opacity = 0;
  60. notification.style.zIndex = '9999';
  61. notification.style.transition = 'opacity 0.5s ease-in-out';
  62. document.body.appendChild(notification);
  63.  
  64. notification.style.opacity = '1';
  65.  
  66. // 设置3秒后自动消失
  67. setTimeout(function() {
  68. notification.style.opacity = '0';
  69. }, 1000);
  70. }
  71.  
  72.  
  73. function convertTable(table) {
  74. let markdownResults = '';
  75. let markdownTable = convertTableElementToMarkdown(table);
  76. markdownResults += markdownTable + NL + NL;
  77. return markdownResults;
  78. }
  79.  
  80. // https://github.com/johnbeech/html-table-to-markdown-converter/blob/master/table-converter.js
  81. function convertTableElementToMarkdown(tableEl) {
  82. let rows = [];
  83. let trEls = tableEl.getElementsByTagName('tr');
  84. for(let i=0; i<trEls.length; i++) {
  85. let tableRow = trEls[i];
  86. let markdownRow = convertTableRowElementToMarkdown(tableRow, i);
  87. rows.push(markdownRow);
  88. }
  89. return rows.join(NL);
  90. }
  91.  
  92. function convertTableRowElementToMarkdown(tableRowEl, rowNumber) {
  93. let cells = [];
  94. let cellEls = tableRowEl.children;
  95. for(let i=0; i<cellEls.length; i++) {
  96. let cell = cellEls[i];
  97. cells.push(cell.innerText + ' |');
  98. }
  99. let row = '| ' + cells.join(" ");
  100.  
  101. if(rowNumber == 0) {
  102. row = row + NL + createMarkdownDividerRow(cellEls.length);
  103. }
  104.  
  105. return row;
  106. }
  107.  
  108. function createMarkdownDividerRow(cellCount) {
  109. let dividerCells = [];
  110. for(let i = 0; i<cellCount; i++) {
  111. dividerCells.push('---' + ' |');
  112. }
  113. return '| ' + dividerCells.join(" ");
  114. }
  115. })();