// ==UserScript== // @name MikroEdit – WYSIWYG-Editor für Mikrocontroller.net // @namespace https://github.com/ebachmann/mikroedit // @version 0.1.0 // @description Rüstet der Foren-Postform von Mikrocontroller.net (und EmbDev.net) einen WYSIWYG-Markdown-Editor (ToastUI) nach – komplett clientseitig, ohne zusätzliche requests ans Forum. // @author https://www.mikrocontroller.net/user/show/ernst // @match https://www.mikrocontroller.net/* // @match https://mikrocontroller.net/* // @match https://www.embdev.net/* // @match https://embdev.net/* // @require https://uicdn.toast.com/editor/3.2.2/toastui-editor-all.min.js // @require https://uicdn.toast.com/editor/3.2.2/i18n/de-de.min.js // @run-at document-idle // @noframes // @grant none // ==/UserScript== (() => { // src/config.js var TOASTUI_VERSION = "3.2.2"; var CDN = `https://uicdn.toast.com/editor/${TOASTUI_VERSION}`; var TOASTUI_JS = `${CDN}/toastui-editor-all.min.js`; var TOASTUI_I18N_DE = `${CDN}/i18n/de-de.min.js`; var TOASTUI_CSS = [`${CDN}/toastui-editor.min.css`]; var FORM_SELECTOR = "form#postform-form"; var TEXTAREA_SELECTOR = "textarea#form_post_text"; var SPECIAL_CHARS = [ "\u03A9", "\xB5", "\xB1", "\u2248", "\xB0", "\xB2", "\xB3", "\xB9", "\u2082", "\u2206", "\u2192", "\u2190", "\u2194", "\xD7", "\xF7", "\u03C0", "\u221E", "\u221A" ]; // src/sync.js var EXTERNAL_EVENT = "mikroedit:external"; function watchExternalWrites(textarea) { const desc = Object.getOwnPropertyDescriptor( HTMLTextAreaElement.prototype, "value" ); Object.defineProperty(textarea, "value", { configurable: true, get() { return desc.get.call(this); }, set(v) { desc.set.call(this, v); if (!textarea.__mikroeditInternal) { textarea.dispatchEvent(new Event(EXTERNAL_EVENT)); } } }); } function attachSync(textarea, form, editor, wrap) { let last = editor.getMarkdown(); const setInternal = (v) => { textarea.__mikroeditInternal = true; textarea.value = v; textarea.__mikroeditInternal = false; }; const syncOut = () => { const md = editor.getMarkdown(); if (md === textarea.value) { last = md; return; } last = md; setInternal(md); textarea.dispatchEvent(new Event("input", { bubbles: true })); textarea.dispatchEvent(new Event("change", { bubbles: true })); }; editor.on("change", syncOut); const syncIn = () => { if (textarea.value !== last) { last = textarea.value; editor.setMarkdown(textarea.value); } }; watchExternalWrites(textarea); textarea.addEventListener(EXTERNAL_EVENT, syncIn); document.addEventListener("click", (ev) => { const link = ev.target && ev.target.closest && ev.target.closest(".postmenuitem-insert_quoting a"); if (link) { setTimeout(syncIn, 400); setTimeout(syncIn, 1200); } }); const applyVisibility = () => { wrap.style.display = textarea.hidden ? "none" : ""; if (!textarea.hidden) { syncIn(); } }; new MutationObserver(applyVisibility).observe(textarea, { attributes: true, attributeFilter: ["hidden"] }); applyVisibility(); form.addEventListener("submit", () => { const md = editor.getMarkdown(); if (md !== textarea.value) { last = md; setInternal(md); } }, true); } // src/core.js var CSS_MARKER = "mikroedit-css"; var THEME_MARKER = "mikroedit-theme"; var THEME = ` .mikroedit-wrap .toastui-editor-defaultUI { border: 1px solid #c8c8c8; border-radius: 6px; font-family: 'Source Sans 3', 'Segoe UI', sans-serif; } .mikroedit-wrap .toastui-editor-main { background: #fff; } /* Die echte Foren-Textarea bleibt Submit-Quelle, soll aber nie sichtbar sein. display:none (nicht das hidden-Attribut!) \u2013 das Attribut geh\xF6rt dem Vorschau-Umschalter der Site. */ .mikroedit-textarea { display: none !important; } .mikroedit-special-wrap { position: relative; } .mikroedit-special-panel { position: absolute; top: 100%; left: 0; z-index: 100; display: none; grid-template-columns: repeat(6, auto); gap: 2px; padding: 6px; background: #fff; border: 1px solid #c8c8c8; border-radius: 6px; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15); } .mikroedit-special-panel.mikroedit-open { display: grid; } .mikroedit-special-panel button { min-width: 28px; padding: 3px 6px; font-size: 15px; line-height: 1.2; background: none; border: none; border-radius: 4px; cursor: pointer; } .mikroedit-special-panel button:hover { background: #eef1f5; } `; function ensureCss() { TOASTUI_CSS.forEach((href) => { if (document.querySelector(`link[data-marker="${CSS_MARKER}"][href="${href}"]`)) { return; } const link = document.createElement("link"); link.rel = "stylesheet"; link.href = href; link.dataset.marker = CSS_MARKER; document.head.appendChild(link); }); if (!document.getElementById(THEME_MARKER)) { const style = document.createElement("style"); style.id = THEME_MARKER; style.textContent = THEME; document.head.appendChild(style); } } function addSpecialChars(editor, wrap) { const toolbar = wrap.querySelector(".toastui-editor-defaultUI-toolbar") || wrap.querySelector(".toastui-editor-toolbar") || wrap.querySelector('[class*="toolbar"]'); if (!toolbar) { return; } const group = document.createElement("div"); group.className = "toastui-toolbar-group mikroedit-special-group"; const btnWrap = document.createElement("div"); btnWrap.className = "toastui-btn-wrap mikroedit-special-wrap"; const toggle = document.createElement("button"); toggle.type = "button"; toggle.className = "toastui-btn mikroedit-special-toggle"; toggle.title = "Sondertzeichen einf\xFCgen"; toggle.textContent = "\u03A9"; const panel = document.createElement("div"); panel.className = "mikroedit-special-panel"; SPECIAL_CHARS.forEach((ch) => { const btn = document.createElement("button"); btn.type = "button"; btn.textContent = ch; btn.addEventListener("click", () => { editor.insertText(ch); panel.classList.remove("mikroedit-open"); }); panel.appendChild(btn); }); toggle.addEventListener("click", (ev) => { ev.stopPropagation(); panel.classList.toggle("mikroedit-open"); }); document.addEventListener("click", (ev) => { if (!group.contains(ev.target)) { panel.classList.remove("mikroedit-open"); } }); btnWrap.appendChild(toggle); btnWrap.appendChild(panel); group.appendChild(btnWrap); toolbar.appendChild(group); } function mountForm(textarea) { if (textarea.__mikroedit) { return; } textarea.__mikroedit = true; const form = textarea.closest(FORM_SELECTOR); if (!form) { return; } ensureCss(); const wrap = document.createElement("div"); wrap.className = "mikroedit-wrap"; textarea.parentNode.insertBefore(wrap, textarea); textarea.classList.add("mikroedit-textarea"); textarea.required = false; textarea.minLength = 0; const editor = new window.toastui.Editor({ el: wrap, language: "de-de", initialValue: textarea.value, initialEditType: "wysiwyg", previewStyle: "tab", previewHighlight: false, hideModeSwitch: false, usageStatistics: false, height: "420px", toolbarItems: [ ["heading", "bold", "italic", "strike"], ["hr", "quote"], ["ul", "ol", "table"], ["link", "code"] ] }); addSpecialChars(editor, wrap); attachSync(textarea, form, editor, wrap); } function scan() { document.querySelectorAll(`${FORM_SELECTOR} ${TEXTAREA_SELECTOR}`).forEach(mountForm); } function init() { if (window.__mikroeditActive) { return; } if (!window.toastui || !window.toastui.Editor) { console.warn("MikroEdit: ToastUI Editor ist nicht geladen."); return; } window.__mikroeditActive = true; if (document.readyState === "loading") { document.addEventListener("DOMContentLoaded", scan); } else { scan(); } new MutationObserver(scan).observe(document.documentElement, { childList: true, subtree: true }); } // src/user.js init(); })();