GreasyFork Code: Syntax Highlight by CodeMirror

To syntax hightlight GreasyFork Code by CodeMirror

As of 2023-07-30. See the latest version.

  1. // ==UserScript==
  2. // @name GreasyFork Code: Syntax Highlight by CodeMirror
  3. // @namespace Violentmonkey Scripts
  4. // @match https://greatest.deepsurf.us/*
  5. // @grant none
  6. // @version 0.1.4
  7. // @author CY Fung
  8. // @description To syntax hightlight GreasyFork Code by CodeMirror
  9. // @run-at document-start
  10. // @inject-into page
  11. // @unwrap
  12. // @license MIT
  13. // ==/UserScript==
  14.  
  15.  
  16.  
  17. (() => {
  18. let byPass = true;
  19.  
  20. let documentReady = new Promise(resolve => {
  21. Promise.resolve().then(() => {
  22. if (document.readyState !== 'loading') {
  23. resolve();
  24. } else {
  25. window.addEventListener("DOMContentLoaded", resolve, false);
  26. }
  27. });
  28. });
  29.  
  30. // Function to load CodeMirror library
  31. function loadCodeMirror(arr) {
  32.  
  33.  
  34. const promises = arr.map((href) => {
  35. return new Promise(resolve => {
  36.  
  37.  
  38. const script = document.createElement('script');
  39. script.src = href;
  40. script.onload = () => {
  41. resolve(script);
  42. };
  43. document.head.appendChild(script);
  44.  
  45. });
  46.  
  47.  
  48. });
  49.  
  50. return Promise.all(promises);
  51. }
  52.  
  53. // Function to load CodeMirror CSS
  54. function loadCodeMirrorCSS(href) {
  55. const link = document.createElement('link');
  56. link.rel = 'stylesheet';
  57. link.href = href;
  58. document.head.appendChild(link);
  59. }
  60.  
  61. async function runBlock(codeBlock) {
  62.  
  63. let textarea = document.createElement('textarea');
  64. textarea.value = `${codeBlock.textContent}`;
  65. textarea.readOnly = true;
  66. textarea.id = 'editor651';
  67.  
  68. textarea.style.width = '100%';
  69. textarea.style.height = '400px';
  70.  
  71. codeBlock.replaceWith(textarea);
  72. codeBlock.remove();
  73.  
  74. await new Promise(r => setTimeout(r, 40));
  75.  
  76.  
  77.  
  78.  
  79. let editor651 = CodeMirror.fromTextArea(document.querySelector('#editor651'), {
  80.  
  81. mode: "javascript",
  82.  
  83. readOnly: true,
  84. styleActiveLine: true,
  85. lineNumbers: true,
  86. });
  87. editor651.save();
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. }
  96.  
  97. // Main function to apply CodeMirror syntax highlighting to pre elements
  98. function applyCodeMirrorSyntaxHighlighting() {
  99. const codeBlocks = document.querySelectorAll('pre.prettyprint.linenums.lang-js');
  100.  
  101.  
  102. // Check if CodeMirror is loaded
  103. if (typeof CodeMirror !== 'undefined') {
  104.  
  105. codeBlocks.forEach(runBlock);
  106. } else {
  107. console.error('CodeMirror library is not loaded. Syntax highlighting cannot be applied.');
  108. }
  109. }
  110.  
  111. async function doAction() {
  112.  
  113.  
  114. await new Promise(r => setTimeout(r, 0));
  115.  
  116. if (mgg) {
  117.  
  118. byPass = false;
  119.  
  120.  
  121. document.head.appendChild(document.createElement('style')).textContent = `
  122.  
  123. body {
  124.  
  125. display: flex;
  126. flex-direction: column;
  127. height: 100vh;
  128. }
  129.  
  130. body > div:last-child{
  131. height:0;
  132. flex-grow:1;
  133. display: flex;
  134. flex-direction:column;
  135. }
  136.  
  137. body > div:last-child > #script-info:last-child{
  138.  
  139. height:0;
  140. flex-grow:1;
  141. display: flex;
  142. flex-direction:column;
  143.  
  144. }
  145.  
  146. body > div:last-child > #script-info:last-child > #script-content:last-child{
  147.  
  148.  
  149. height:0;
  150. flex-grow:1;
  151. display: flex;
  152. flex-direction:column;
  153. }
  154.  
  155. body > div:last-child > #script-info:last-child > #script-content:last-child > .code-container:last-child{
  156.  
  157.  
  158. height:0;
  159. flex-grow:1;
  160. display: flex;
  161. flex-direction:column;
  162.  
  163. }
  164.  
  165. body > div:last-child > #script-info:last-child > #script-content:last-child > .code-container:last-child > textarea:last-child{
  166.  
  167.  
  168. height:0;
  169. flex-grow:1;
  170. display: flex;
  171. flex-direction:column;
  172.  
  173. }
  174.  
  175. body > div:last-child > #script-info:last-child > #script-content:last-child > .code-container:last-child > .CodeMirror:last-child{
  176.  
  177.  
  178. height:0;
  179. flex-grow:1;
  180. display: flex;
  181. flex-direction:column;
  182.  
  183. }
  184.  
  185. `;
  186.  
  187. await loadCodeMirror(['https://cdn.jsdelivr.net/npm/codemirror@5.65.14/lib/codemirror.min.js']);
  188.  
  189. await loadCodeMirror([
  190. 'https://cdn.jsdelivr.net/npm/codemirror@5.65.14/mode/javascript/javascript.min.js',
  191. 'https://cdn.jsdelivr.net/npm/codemirror@5.65.14/addon/selection/active-line.min.js']);
  192.  
  193. loadCodeMirrorCSS('https://cdn.jsdelivr.net/npm/codemirror@5.65.14/lib/codemirror.min.css');
  194.  
  195. await new Promise(r => setTimeout(r, 20));
  196.  
  197.  
  198. applyCodeMirrorSyntaxHighlighting();
  199.  
  200.  
  201.  
  202.  
  203. }
  204.  
  205.  
  206.  
  207. }
  208.  
  209.  
  210. let mgg = 0;
  211. async function mTz() {
  212. if (mgg) return;
  213. mgg = 1;
  214. byPass = false;
  215. documentReady.then(doAction);
  216. }
  217.  
  218.  
  219.  
  220. function getElementsByTagName(tag) {
  221.  
  222. if (byPass) {
  223. if (tag === 'pre' || tag === 'code' || tag === 'xmp') {
  224. setTimeout(mTz, 10)
  225. return [];
  226. }
  227. }
  228. return this.getElementsByTagName331(tag);
  229. }
  230.  
  231.  
  232.  
  233. HTMLElement.prototype.getElementsByTagName331 = HTMLElement.prototype.getElementsByTagName
  234. Document.prototype.getElementsByTagName331 = Document.prototype.getElementsByTagName
  235.  
  236. HTMLElement.prototype.getElementsByTagName = getElementsByTagName
  237. Document.prototype.getElementsByTagName = getElementsByTagName
  238.  
  239. /*
  240. let mz= function(evt){
  241.  
  242. if(evt && evt.type ==='readystatechange') return;
  243. return EventTarget.prototype.addEventListener.apply(this,arguments)
  244.  
  245. };
  246. window.addEventListener = mz
  247. document.addEventListener = mz;
  248. */
  249.  
  250.  
  251. documentReady.then(async () => {
  252. if(location.pathname.endsWith('/code')) return;
  253.  
  254. byPass = false;
  255.  
  256. });
  257.  
  258. })();
  259.  
  260.  
  261.  
  262.  
  263.