Easy Markdown for StackExchange

Adds "Markdown" and "Copy" buttons to display original Markdown content in StackExchange sites.

当前为 2023-11-29 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Easy Markdown for StackExchange
  3. // @version 2.3
  4. // @author Murphy Lo (http://github.com/MurphyLo)
  5. // @description Adds "Markdown" and "Copy" buttons to display original Markdown content in StackExchange sites.
  6. // @license GNU GPL v3 (http://www.gnu.org/copyleft/gpl.html)
  7. // @match https://*.stackoverflow.com/questions/*
  8. // @match https://*.superuser.com/questions/*
  9. // @match https://*.serverfault.com/questions/*
  10. // @match https://*.stackexchange.com/questions/*
  11. // @match https://*.askubuntu.com/questions/*
  12. // @match https://*.math.stackexchange.com/questions/*
  13. // @match https://*.tex.stackexchange.com/questions/*
  14. // @match https://*.english.stackexchange.com/questions/*
  15. // @match https://*.gaming.stackexchange.com/questions/*
  16. // @match https://*.physics.stackexchange.com/questions/*
  17. // @match https://*.chemistry.stackexchange.com/questions/*
  18. // @match https://*.biology.stackexchange.com/questions/*
  19. // @match https://*.programmers.stackexchange.com/questions/*
  20. // @match https://*.electronics.stackexchange.com/questions/*
  21. // @grant none
  22. // @namespace https://greasyfork.org/users/1224148
  23. // ==/UserScript==
  24.  
  25. // This script is a modified version of an original script by Manish Goregaokar (http://stackapps.com/users/10098/manishearth)
  26. // Original script: https://github.com/Manishearth/Manish-Codes/raw/master/StackExchange/PrintPost.user.js
  27. // The original script is licensed under the GNU GPL v3 (http://www.gnu.org/copyleft/gpl.html)
  28. // Modifications made by Murphy Lo
  29.  
  30. (async function() {
  31. 'use strict';
  32.  
  33. // Function to fetch content of a post
  34. async function fetchMarkdown(postId) {
  35. const response = await fetch(`/posts/${postId}/edit-inline`);
  36. // Check user login status.
  37. if (response.status === 404) {
  38. return "--- Please LOG IN StackExchange/Stackoverflow to view and copy the Markdown content. ---";
  39. }
  40.  
  41. const data = await response.text();
  42. let markdown = data.match(/<textarea[^>]*>([\s\S]*?)<\/textarea>/)[1];
  43. // Decode HTML entities
  44. return decodeHtmlEntities(markdown);
  45. }
  46.  
  47. // Function to decode HTML entities from the content into Markdown plain text
  48. function decodeHtmlEntities(str) {
  49. const tempElement = document.createElement('div');
  50. tempElement.innerHTML = str;
  51. return tempElement.textContent;
  52. }
  53.  
  54. // Function to show markdown content in a modal
  55. function showMarkdown(event) {
  56. // Prevent the default action
  57. event.preventDefault();
  58.  
  59. // Disable scrolling on the main page
  60. const scrollbarWidth = window.innerWidth - document.documentElement.clientWidth;
  61. document.body.style.overflow = 'hidden';
  62. document.body.style.paddingRight = `${scrollbarWidth}px`;
  63.  
  64. // Add paddingRight to fixed positioned elements
  65. const fixedElements = document.querySelectorAll('header.s-topbar');
  66. fixedElements.forEach(el => el.style.paddingRight = `${scrollbarWidth}px`);
  67.  
  68. // Create a modal to display the markdown (updated style)
  69. const modal = document.createElement('div');
  70. modal.style.cssText = `
  71. position: fixed;
  72. z-index: 5051;
  73. left: 0;
  74. top: 0;
  75. width: 100%;
  76. height: 100%;
  77. overflow: none;
  78. background-color: rgba(0,0,0,0.5);`;
  79.  
  80. const modalContent = document.createElement('div');
  81. modalContent.style.cssText = `
  82. background-color: #FFF;
  83. margin: 5% auto;
  84. padding: 20px;
  85. border: 1px solid #888;
  86. width: 70%;
  87. max-height: 90%;
  88. overflow-y: auto;
  89. box-sizing: border-box;
  90. border-radius: 5px;
  91. box-shadow: 0 4px 8px 0 rgba(0,0,0,0.3);`;
  92.  
  93. // Updated close button style
  94. const closeButton = document.createElement('span');
  95. closeButton.style.cssText = `
  96. color: #aaa;
  97. float: right;
  98. font-size: 28px;
  99. font-weight: bold;
  100. cursor: pointer;
  101. margin-left: 10px;`;
  102. closeButton.textContent = '✕';
  103. closeButton.onmouseover = () => closeButton.style.color = 'black';
  104. closeButton.onmouseout = () => closeButton.style.color = '#aaa';
  105.  
  106. const markdownElement = document.createElement('pre');
  107. markdownElement.style.cssText = `
  108. margin: 0;
  109. white-space: pre-wrap;
  110. word-wrap: break-word;
  111. overflow-y: auto;`;
  112. markdownElement.textContent = this.markdownContent;
  113.  
  114. modalContent.appendChild(closeButton);
  115. modalContent.appendChild(markdownElement);
  116. modal.appendChild(modalContent);
  117. document.body.appendChild(modal);
  118.  
  119. // Close modal behaviors
  120. function closeModal() {
  121. // Removing the modal from the DOM
  122. document.body.removeChild(modal);
  123. document.removeEventListener('keydown', handleKeyDown);
  124.  
  125. // Re-enable scrolling on the main page and remove the paddingRight
  126. document.body.style.overflow = '';
  127. document.body.style.paddingRight = '';
  128.  
  129. // Remove paddingRight from fixed positioned elements
  130. const fixedElements = document.querySelectorAll('header.s-topbar');
  131. fixedElements.forEach(el => el.style.paddingRight = '');
  132. }
  133.  
  134. // Click `x` to close modal
  135. closeButton.onclick = closeModal;
  136. // Press `Esc` to close modal
  137. const handleKeyDown = (event) => event.keyCode === 27 && closeModal(); // 27 is the keyCode for the Esc key
  138. document.addEventListener('keydown', handleKeyDown);
  139. // Click modal background to close modal
  140. modal.onclick = (e) => e.target === modal && closeModal();
  141.  
  142. }
  143.  
  144. // Function to copy text to clipboard
  145. async function copyToClipboard(text) {
  146. try {
  147. await navigator.clipboard.writeText(text);
  148. // console.log('Markdown content copied to clipboard');
  149. } catch (err) {
  150. console.error('Failed to copy Markdown content: ', err);
  151. }
  152. }
  153.  
  154. // Add "Markdown" and "Copy" buttons to each post
  155. const posts = document.querySelectorAll('.question, .answer');
  156. for (const post of posts) {
  157. const postMenu = post.querySelector('.d-flex.gs8.s-anchors.s-anchors__muted.fw-wrap');
  158. const separator = document.createElement('span');
  159. separator.className = 'lsep';
  160. separator.textContent = '|';
  161. postMenu.appendChild(separator);
  162.  
  163. // Add Markdown button
  164. const printButton = document.createElement('a');
  165. printButton.href = '#';
  166. printButton.textContent = 'Markdown';
  167. printButton.title = 'View this post\'s original Markdown content';
  168. printButton.onclick = showMarkdown;
  169.  
  170. const printButtonWrapper = document.createElement('div');
  171. printButtonWrapper.className = 'flex--item';
  172. printButtonWrapper.appendChild(printButton);
  173.  
  174. postMenu.appendChild(printButtonWrapper);
  175.  
  176. // Add Copy button
  177. const copyButton = document.createElement('a');
  178. copyButton.href = '#';
  179. copyButton.textContent = 'Copy';
  180. copyButton.title = 'Copy this post\'s original Markdown content to clipboard';
  181. copyButton.onclick = (event) => {
  182. event.preventDefault();
  183. copyToClipboard(printButton.markdownContent);
  184. };
  185.  
  186. const copyButtonWrapper = document.createElement('div');
  187. copyButtonWrapper.className = 'flex--item';
  188. copyButtonWrapper.appendChild(copyButton);
  189.  
  190. postMenu.appendChild(copyButtonWrapper);
  191.  
  192. // Fetch and store markdown content
  193. const postId = post.dataset.questionid || post.dataset.answerid;
  194. printButton.markdownContent = await fetchMarkdown(postId);
  195. }
  196. })();