GreasyFork Code: Monaco Editor

12/17/2023, 2:31:22 PM

当前为 2023-12-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GreasyFork Code: Monaco Editor
  3. // @namespace Violentmonkey Scripts
  4. // @match https://greasyfork.org/*
  5. // @grant none
  6. // @version 0.1.0
  7. // @author -
  8. // @description 12/17/2023, 2:31:22 PM
  9. // @run-at document-start
  10. // @unwrap
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. // localStorage.darkMode = 'true'
  15.  
  16. (() => {
  17.  
  18. const cssText01 = `
  19. .monaco-editor-container{
  20. height: 20em;
  21. }
  22. `;
  23.  
  24. let elmSet = {};
  25.  
  26. HTMLInputElement.prototype.addEventListener177 = HTMLInputElement.prototype.addEventListener;
  27. HTMLInputElement.prototype.addEventListener = function () {
  28. if (arguments.length === 2 && arguments[0] === 'change' && (arguments[1] || 0).name === 'handleChange') {
  29. // if(this === ) return;
  30. // console.log(this)
  31. if (this.id === 'enable-source-editor-code') return;
  32. // return;
  33. }
  34. return this.addEventListener177.apply(this, arguments);
  35.  
  36. }
  37.  
  38.  
  39. function loadResource(type, url) {
  40. if (type === 'css') {
  41. return new Promise(resolve => {
  42. var link = document.createElement('link');
  43. var onload = function () {
  44. link.removeEventListener('load', onload, false);
  45. resolve();
  46. }
  47. link.addEventListener('load', onload, false);
  48. link.rel = 'stylesheet';
  49. link.href = url;
  50. document.head.appendChild(link);
  51. });
  52. } else if (type === 'js') {
  53. return new Promise(resolve => {
  54. var script = document.createElement('script');
  55. var onload = function () {
  56. script.removeEventListener('load', onload, false);
  57. resolve();
  58. }
  59. script.addEventListener('load', onload, false);
  60. script.src = url;
  61. document.head.appendChild(script);
  62. })
  63. }
  64. }
  65.  
  66. function onChange817(e) {
  67.  
  68.  
  69.  
  70. // console.log(123213)
  71.  
  72. const target = (e || 0).target || null;
  73.  
  74. if (!target) return;
  75.  
  76. let monacoStatus = target.getAttribute('monaco-status')
  77. if (monacoStatus) {
  78.  
  79.  
  80. // console.log(monacoStatus)
  81. if (monacoStatus === '1') {
  82. elmSet.container.replaceWith(elmSet.textArea);
  83.  
  84.  
  85. target.setAttribute('monaco-status', monacoStatus = '2')
  86. return;
  87.  
  88. } else if (monacoStatus === '2') {
  89.  
  90. elmSet.textArea.replaceWith(elmSet.container);
  91.  
  92. target.setAttribute('monaco-status', monacoStatus = '1')
  93. return;
  94. } else {
  95. return;
  96. }
  97.  
  98. }
  99.  
  100. const codeId = target.getAttribute('data-related-editor') || '';
  101. if (!codeId) return;
  102.  
  103. const textArea = document.getElementById(codeId);
  104.  
  105. if (!textArea) return;
  106.  
  107. const codeLang = target.getAttribute('data-editor-language'); // 'javascript', 'css'
  108.  
  109. if (!codeLang) return;
  110.  
  111.  
  112. target.setAttribute('monaco-status', '1');
  113.  
  114.  
  115. e.stopImmediatePropagation();
  116. e.stopPropagation();
  117. e.preventDefault();
  118.  
  119.  
  120. const vsPath = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.45.0/min/vs"
  121. // Setting up Monaco Editor requirements
  122. var require = {
  123. paths: {
  124. vs: vsPath,
  125. },
  126. };
  127. window.require = require;
  128.  
  129.  
  130. const addCssText = (id, text) => {
  131. if (document.getElementById(id)) return;
  132. const style = document.createElement('style');
  133. style.id = id;
  134. style.textContent = text;
  135. document.head.appendChild(style);
  136.  
  137. }
  138.  
  139.  
  140. (async function () {
  141.  
  142. // Dynamically load CSS and JS
  143. await loadResource('css', `${vsPath}/editor/editor.main.css`);
  144. await loadResource('js', `${vsPath}/loader.js`);
  145. await loadResource('js', `${vsPath}/editor/editor.main.nls.js`);
  146. await loadResource('js', `${vsPath}/editor/editor.main.js`);
  147.  
  148. addCssText('rmbnctzOOksi', cssText01);
  149.  
  150. monaco.languages.typescript.javascriptDefaults.setCompilerOptions({
  151. allowNonTsExtensions: true,
  152. checkJs: true,
  153. });
  154.  
  155. const container = document.createElement('div');
  156. container.className = 'monaco-editor-container';
  157. textArea.replaceWith(container);
  158.  
  159. elmSet.textArea = textArea;
  160. elmSet.container = container;
  161.  
  162. const monacoLangs = {
  163. 'javascript': 'javascript',
  164. 'css': 'css',
  165. }
  166.  
  167. const monacoLang = monacoLangs[codeLang];
  168.  
  169. var editor = monaco.editor.create(container, {
  170. value: textArea.value,
  171. language: monacoLang,
  172. automaticLayout: true,
  173. foldingStrategy: 'indentation',
  174. lineNumbers: 'on',
  175. readOnly: false,
  176. minimap: {
  177. enabled: false,
  178. },
  179. cursorStyle: 'line',
  180. });
  181.  
  182. if (document.documentElement.hasAttribute('dark')) monaco.editor.setTheme("vs-dark");
  183.  
  184. editor.onDidChangeModelContent(e => {
  185. elmSet.textArea.value = editor.getValue()
  186. });
  187.  
  188.  
  189.  
  190. // console.log(monaco, monaco.onDidChangeModelContent)
  191.  
  192. // window.editor.getModel().onDidChangeContent((event) => {
  193. // render();
  194. // });
  195.  
  196. // editor.setTheme
  197.  
  198. // onDidChangeContent is attached to a model, and will only apply to that model
  199. // onDidChangeModelContent
  200.  
  201.  
  202.  
  203. })();
  204.  
  205.  
  206. }
  207.  
  208. function onReady() {
  209. window.removeEventListener("DOMContentLoaded", onReady, false);
  210.  
  211. if (localStorage.darkMode === 'true') document.documentElement.setAttribute('dark', '')
  212.  
  213.  
  214. document.querySelector('input#enable-source-editor-code[name="enable-source-editor-code"]').addEventListener('change', onChange817, { once: false, capture: true, passive: false });
  215.  
  216. }
  217.  
  218.  
  219. Promise.resolve().then(() => {
  220.  
  221. if (document.readyState !== 'loading') {
  222. onReady();
  223. } else {
  224. window.addEventListener("DOMContentLoaded", onReady, false);
  225. }
  226.  
  227. });
  228.  
  229. })();