Greasyfork forum MARKDOWN for new comments

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

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

  1. // ==UserScript==
  2. // @name Greasyfork forum MARKDOWN for new comments
  3. // @author wOxxOm
  4. // @description Select MARKDOWN format by default, add formatting help links, add CODE markdown button
  5. // @namespace wOxxOm.scripts
  6. // @version 1.01
  7. // @include https://greasyfork.org/*forum/discussion/*
  8. // @include https://greasyfork.org/*forum/post/discussion*
  9. // @run-at document-start
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13. var ob = new MutationObserver(function(mutations){
  14. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  15. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  16. if (n.nodeType == 1) {
  17. if (n.localName == 'label') {
  18. if (n.for != 'Form_Format2')
  19. continue;
  20. }
  21. else if (!(n = n.querySelector('label[for="Form_Format2"]')))
  22. continue;
  23.  
  24. n.click();
  25.  
  26. n.previousElementSibling.insertAdjacentHTML('beforeend',
  27. ' (<a href="/help/allowed-markup" target="_blank" title="'+
  28. '* (name, title), a (href), abbr, b, blockquote (cite), br, center, cite, code, dd, del, dfn, div, dl, dt, em, '+
  29. 'h1, h2, h3, h4, h5, h6, hr, i, ins, img (alt, height, src (https), width), kbd, li, mark, ol, p, pre, q (cite), '+
  30. 'rp, rt, ruby, s, samp, small, span, strike, strong, tt, table, tbody, tfoot, thead, td, th, tr, sub, sup, '+
  31. 'time (datetime, pubdate), u, ul, var">?</a>)');
  32. n.insertAdjacentHTML('beforeend',' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');
  33.  
  34. var a = document.createElement('a');
  35. a.className = "Button CodeButton"; a.style.setProperty('float','right'); a.textContent = 'Code';
  36. a.title = 'Apply CODE markdown to selected text';
  37. a.addEventListener('click', function(e){
  38. var txtNode = document.getElementById('Form_Body');
  39. var s1 = txtNode.selectionStart, s2 = txtNode.selectionEnd;
  40. var txt = txtNode.value;
  41. var sel = txt.substring(s1, s2);
  42. if (sel.indexOf('\n') < 0)
  43. sel_pre = sel_post = '`';
  44. else {
  45. sel_pre = ((s1==0) || (txt.charAt(s1-1) == '\n') ? '' : '\n') + '```' + (sel.charAt(0) == '\n' ? '' : '\n');
  46. sel_post = (sel.substr(-1) == '\n' ? '' : '\n') + '```' + (txt.substr(s2,1) == '\n' ? '' : '\n');
  47. }
  48. txtNode.value = txt.substr(0, s1) + sel_pre + sel + sel_post + txt.substr(s2);
  49. txtNode.setSelectionRange(s1 + sel_pre.length, s1 + sel_pre.length + sel.length);
  50. txtNode.focus();
  51. });
  52. n.parentNode.insertBefore(a, n.nextElementSibling);
  53.  
  54. ob.disconnect();
  55. return;
  56. }
  57. })
  58. ob.observe(document, {subtree:true, childList:true});