Geekhub Markdown Editor

add CodeMirror Editor for comment & topic

Από την 10/10/2020. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name     Geekhub Markdown Editor
// @namespace https://geekhub.com/
// @version  2
// @description  add CodeMirror Editor for comment & topic
// @author       dallaslu
// @match        https://geekhub.com/*
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/codemirror/lib/codemirror.min.js
// @require      https://cdn.jsdelivr.net/npm/codemirror/mode/markdown/markdown.js
// @require      https://cdn.jsdelivr.net/npm/codemirror/mode/gfm/gfm.js
// @require      https://cdn.jsdelivr.net/npm/codemirror/addon/mode/overlay.js
// ==/UserScript==

(function() {
	'use strict';
  
  	var ghSchemeButtons = {
        'light': 'dark',
        'dark': 'darcula',
        'solar': 'darcula',
        'ocean': 'darcula',
        'jade': 'default'
    };

    function init() {
      	var cmElement = document.querySelector('.CodeMirror');
      	if(!cmElement){
            var textElement = document.getElementById('comment-box') 
                || document.getElementById('post_content')
                || document.getElementById('second_hand_content')
                || document.getElementById('molecule_content')
                || document.getElementById('group_buy_content')
                || document.getElementById('service_content');
          
          	if(textElement){
				var theme = '';
                for (var btn in ghSchemeButtons) {
                    var schemeBtn = document.getElementById(btn);
                    if (schemeBtn && !schemeBtn.classList.contains('hidden')) {
                        theme = ghSchemeButtons[btn];
                        break;
                    }
                }

				theme = theme || 'darcula';
              
              	loadStyles('https://cdn.jsdelivr.net/npm/codemirror/lib/codemirror.css');
              	loadStyles('https://cdn.jsdelivr.net/npm/codemirror/theme/darcula.css');
              
      			var cm = CodeMirror.fromTextArea(textElement, {
    				mode : {
						name : "gfm",
						tokenTypeOverrides : {
							emoji : "emoji"
						}
					},
					lineWrapping : true,
					lineNumbers : false,
              	    theme: theme
       		 	});
              
              	cm.on('change', function(){
            		cm.save();
                });
              	
              	// TODO clear with textarea after submit
            }
        }
    }
  
  	function loadStyles(url) {
		var link = document.createElement("link");
		link.type = "text/css";
		link.rel = "stylesheet";
		link.href = url;
		var head = document.getElementsByTagName("head")[0];
		head.appendChild(link);
	}

    var observer = new MutationObserver(function(doc, observer) {
		init();
    });

    observer.observe(document, {
        characterData: true,
        childList: true,
        attributes: true,
        subtree: true,
        attributeOldValue: true,
        characterDataOldValue: true
    });

    init();
})();