Greasyfork forum MARKDOWN for new comments

Select MARKDOWN format by default, add formatting help links, add CODE markdown button

当前为 2014-12-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name          Greasyfork forum MARKDOWN for new comments
// @author        wOxxOm
// @description   Select MARKDOWN format by default, add formatting help links, add CODE markdown button
// @namespace     wOxxOm.scripts
// @version       1.0
// @include       https://greasyfork.org/*forum/discussion/*
// @run-at        document-start
// @grant         none
// ==/UserScript==

var ob = new MutationObserver(function(mutations){
  for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
    for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
      if (n.nodeType == 1) {
        if (n.localName == 'label') {
          if (n.for != 'Form_Format2')
            continue;
        } 
        else if (!(n = n.querySelector('label[for="Form_Format2"]')))
          continue;

        n.click();

        n.previousElementSibling.insertAdjacentHTML('beforeend',' (<a href="/help/allowed-markup" target="_blank">?</a>)');
        n.insertAdjacentHTML('beforeend',' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');

        var a = document.createElement('a');
        a.className = "Button CodeButton"; a.style.setProperty('float','right'); a.textContent = 'Code';
        a.title = 'Apply CODE markdown to selected text';
        a.addEventListener('click', function(e){
          var txtNode = document.getElementById('Form_Body');
          var s1 = txtNode.selectionStart, s2 = txtNode.selectionEnd;
          var txt = txtNode.value;
          var sel = txt.substring(s1, s2);
          
          if (sel.indexOf('\n') < 0)
            sel_pre = sel_post = '`';
          else {
            sel_pre = ((s1==0) || (txt.charAt(s1-1) == '\n') ? '' : '\n') + '```' + (sel.charAt(0) == '\n' ? '' : '\n');
            sel_post =  (sel.substr(-1) == '\n' ? '' : '\n') + '```' + (txt.substr(s2,1) == '\n' ? '' : '\n');
          }
          txtNode.value = txt.substr(0, s1) + sel_pre + sel + sel_post + txt.substr(s2);
          txtNode.setSelectionRange(s1 + sel_pre.length, s1 + sel_pre.length + sel.length);
          txtNode.focus();
        });
        n.parentNode.insertBefore(a, n.nextElementSibling);

        ob.disconnect();
        return;
      }
})
ob.observe(document, {subtree:true, childList:true});