Greasyfork forum MARKDOWN for new comments

Select MARKDOWN format by default, add help links, add toolbar formatting buttons for markdown

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Greasyfork forum MARKDOWN for new comments
// @author        wOxxOm
// @description   Select MARKDOWN format by default, add help links, add toolbar formatting buttons for markdown
// @namespace     wOxxOm.scripts
// @version       1.1
// @include       https://greasyfork.org/*forum/discussion/*
// @include       https://greasyfork.org/*forum/post/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();
        addFeatures(n);
        ob.disconnect();
        return;
      }
});
ob.observe(document, {subtree:true, childList:true});

function addFeatures(n) {
  // add formatting help tooltips
  n.previousElementSibling.insertAdjacentHTML('beforeend',
         ' (<a href="/help/allowed-markup" target="_blank" title="'+
         '* (name, title), a (href), abbr, b, blockquote (cite), br, center, cite, code, dd, del, dfn, div, dl, dt, em, '+
         'h1, h2, h3, h4, h5, h6, hr, i, ins, img (alt, height, src (https), width), kbd, li, mark, ol, p, pre, q (cite), '+
         'rp, rt, ruby, s, samp, small, span, strike, strong, tt, table, tbody, tfoot, thead, td, th, tr, sub, sup, '+
         'time (datetime, pubdate), u, ul, var">?</a>)');
  n.insertAdjacentHTML('beforeend',
         ' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');

  // add buttons
  n.parentNode.textAreaNode = n.parentNode.querySelector('textarea');
  btnMake(n, '<b>B</b>', 'Bold', '**');
  btnMake(n, '<i>I</i>', 'Italic', '*');
  btnMake(n, '<u>U</u>', 'Underline', '<u>','</u>');
  btnMake(n, '<s>S</s>', 'Strikethrough', '<s>','</s>');
  btnMake(n, '---', 'Horizontal line', '\n\n---\n\n', '', true);
  btnMake(n, 'URL', 'Add URL to selected text', 
          function(e) {
            try {edWrapInTag('[', ']('+prompt("URL:")+')', null, e.target)}
            catch(e) {};
          });
  btnMake(n, 'Image (https)', 'Convert selected https://url to inline image', '![image](', ')');
  btnMake(n, 'Table', 'Insert table template', '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n', '', true);
  btnMake(n, 'Code', 'Apply CODE markdown to selected text',
          function(e){
            var ed = edInit(e.target);
            if (ed.sel.indexOf('\n') < 0)
              edWrapInTag('`', '`', ed);
            else
              edWrapInTag(((ed.sel1==0) || (ed.text.charAt(ed.sel1-1) == '\n') ? '' : '\n') + '```' + (ed.sel.charAt(0) == '\n' ? '' : '\n'),
                          (ed.sel.substr(-1) == '\n' ? '' : '\n') + '```' + (ed.text.substr(ed.sel2,1) == '\n' ? '' : '\n'),
                          ed);
          });
}

function btnMake(afterNode, label, title, tag1_or_cb, tag2, noWrap) {
  var a = document.createElement('a');
  a.className = 'Button';
  a.style.setProperty('float','right');
  a.innerHTML = label;
  a.title = title;
  a.addEventListener('click',
            typeof(tag1_or_cb) == 'function'
                     ? tag1_or_cb
                     : noWrap ? function(e){edInsertText(tag1_or_cb, edInit(e.target))} 
                              : function(e){edWrapInTag(tag1_or_cb, tag2, edInit(e.target))});
  a.textAreaNode = afterNode.parentNode.textAreaNode;
  afterNode.parentNode.insertBefore(a, afterNode.nextElementSibling);
}

function edInit(btn) {
  var ed = {node: btn.textAreaNode || btn.parentNode.textAreaNode}
  ed.sel1 = ed.node.selectionStart;
  ed.sel2 = ed.node.selectionEnd,
  ed.text = ed.node.value;
  ed.sel = ed.text.substring(ed.sel1, ed.sel2);
  return ed;
}

function edWrapInTag(tag1, tag2, ed) {
  ed.node.value = ed.text.substr(0, ed.sel1) + tag1 + ed.sel + (tag2?tag2:tag1) + ed.text.substr(ed.sel2);
  ed.node.setSelectionRange(ed.sel1 + tag1.length, ed.sel1 + tag1.length + ed.sel.length);
  ed.node.focus();
}

function edInsertText(text, ed) {
  var ed = edInit(btn);
  ed.node.value = ed.text.substr(0, ed.sel2) + text + ed.text.substr(ed.sel2);
  ed.node.setSelectionRange(ed.sel2 + text.length, ed.sel2 + text.length);
  ed.node.focus();
}