Greasyfork forum MARKDOWN for new comments

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

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

  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.0
  7. // @include https://gf.qytechs.cn/*forum/discussion/*
  8. // @run-at document-start
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. var ob = new MutationObserver(function(mutations){
  13. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  14. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  15. if (n.nodeType == 1) {
  16. if (n.localName == 'label') {
  17. if (n.for != 'Form_Format2')
  18. continue;
  19. }
  20. else if (!(n = n.querySelector('label[for="Form_Format2"]')))
  21. continue;
  22.  
  23. n.click();
  24.  
  25. n.previousElementSibling.insertAdjacentHTML('beforeend',' (<a href="/help/allowed-markup" target="_blank">?</a>)');
  26. n.insertAdjacentHTML('beforeend',' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');
  27.  
  28. var a = document.createElement('a');
  29. a.className = "Button CodeButton"; a.style.setProperty('float','right'); a.textContent = 'Code';
  30. a.title = 'Apply CODE markdown to selected text';
  31. a.addEventListener('click', function(e){
  32. var txtNode = document.getElementById('Form_Body');
  33. var s1 = txtNode.selectionStart, s2 = txtNode.selectionEnd;
  34. var txt = txtNode.value;
  35. var sel = txt.substring(s1, s2);
  36. if (sel.indexOf('\n') < 0)
  37. sel_pre = sel_post = '`';
  38. else {
  39. sel_pre = ((s1==0) || (txt.charAt(s1-1) == '\n') ? '' : '\n') + '```' + (sel.charAt(0) == '\n' ? '' : '\n');
  40. sel_post = (sel.substr(-1) == '\n' ? '' : '\n') + '```' + (txt.substr(s2,1) == '\n' ? '' : '\n');
  41. }
  42. txtNode.value = txt.substr(0, s1) + sel_pre + sel + sel_post + txt.substr(s2);
  43. txtNode.setSelectionRange(s1 + sel_pre.length, s1 + sel_pre.length + sel.length);
  44. txtNode.focus();
  45. });
  46. n.parentNode.insertBefore(a, n.nextElementSibling);
  47.  
  48. ob.disconnect();
  49. return;
  50. }
  51. })
  52. ob.observe(document, {subtree:true, childList:true});

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址