GreasyFork markdown

在论坛默认使用 Markdown 格式,添加格式帮助链接及 Markdown 工具栏

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

  1. // ==UserScript==
  2. // @name GreasyFork markdown
  3. // @name:ru GreasyFork markdown
  4. // @name:zh-CN GreasyFork markdown
  5. // @author wOxxOm
  6. // @contributor JixunMoe
  7. // @license MIT License
  8. // @description Select MARKDOWN format by default, add help links, add toolbar formatting buttons for markdown
  9. // @description:ru Включает формат MARKDOWN по умолчанию, добавляет справочные ссылки по форматам, добавляет панель кнопок форматирования markdown
  10. // @description:zh-CN 在论坛默认使用 Markdown 格式,添加格式帮助链接及 Markdown 工具栏
  11. // @icon https://raw.githubusercontent.com/dcurtis/markdown-mark/master/png/66x40-solid.png
  12. // @namespace wOxxOm.scripts
  13. // @version 1.3.1
  14. // @include https://gf.qytechs.cn/*forum/discussion/*
  15. // @include https://gf.qytechs.cn/*forum/post/discussion*
  16. // @include https://gf.qytechs.cn/*scripts/*/versions/new
  17. // @include https://gf.qytechs.cn/*script_versions/new
  18. // @include https://gf.qytechs.cn/*forum/messages/*
  19. // @run-at document-start
  20. // @grant GM_addStyle
  21. // ==/UserScript==
  22.  
  23. var inForum = location.href.indexOf('/forum/') > 0;
  24.  
  25. var ob = new MutationObserver(function(mutations){
  26. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  27. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  28. if (n.nodeType == 1)
  29. if (inForum) {
  30. if (((n.localName == 'label') && (n.for == 'Form_Format2'))
  31. || (n = n.querySelector('label[for="Form_Format2"]')))
  32. return addFeatures(n);
  33. } else {
  34. if (((n.localName == 'input') && (n.value == 'markdown'))
  35. || (n = n.querySelector('input[value="markdown"]'))) {
  36. if (location.href.indexOf('/script_versions/'))
  37. n.click();
  38. return addFeatures(n.parentNode.appendChild(document.createElement('br')));
  39. }
  40. }
  41. });
  42. ob.observe(document, {subtree:true, childList:true});
  43.  
  44. function addFeatures(n) {
  45. for (var form=n; (form = form.parentNode) && (form.localName != 'form'); ) {}
  46. if (inForum) {
  47. if (form.action.indexOf('/editcomment/') < 0)
  48. n.click();
  49.  
  50. n.parentNode.textAreaNode = form.querySelector('textarea');
  51.  
  52. // add formatting help tooltips
  53. n.previousElementSibling.insertAdjacentHTML('beforeend',
  54. ' (<a href="/help/allowed-markup" target="_blank" title="'+
  55. '* (name, title), a (href), abbr, b, blockquote (cite), br, center, cite, code, dd, del, dfn, div, dl, dt, em, '+
  56. 'h1, h2, h3, h4, h5, h6, hr, i, ins, img (alt, height, src (https), width), kbd, li, mark, ol, p, pre, q (cite), '+
  57. 'rp, rt, ruby, s, samp, small, span, strike, strong, tt, table, tbody, tfoot, thead, td, th, tr, sub, sup, '+
  58. 'time (datetime, pubdate), u, ul, var">?</a>)');
  59. n.insertAdjacentHTML('beforeend',
  60. ' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');
  61. } else {
  62. n.parentNode.textAreaNode = form.querySelector('textarea[id*="additional-info"]');
  63. GM_addStyle('.Button {\
  64. display: inline-block;\
  65. cursor: pointer;\
  66. margin: 0px;\
  67. font-size: 12px;\
  68. line-height: 1;\
  69. font-weight: bold;\
  70. padding: 4px 6px;\
  71. background: -moz-linear-gradient(center bottom , #CCC 0%, #FAFAFA 100%) repeat scroll 0% 0% #F8F8F8;\
  72. border: 1px solid #999;\
  73. border-radius: 2px;\
  74. white-space: nowrap;\
  75. text-shadow: 0px 1px 0px #FFF;\
  76. box-shadow: 0px 1px 0px #FFF inset, 0px -1px 2px #BBB inset;\
  77. color: #333;}');
  78. }
  79.  
  80. // add buttons
  81. btnMake(n, '<b>'+__('B')+'</b>', __('Bold'), '**');
  82. btnMake(n, '<i>'+__('I')+'</i>', __('Italic'), '*');
  83. btnMake(n, '<u>'+__('U')+'</u>', __('Underline'), '<u>','</u>');
  84. btnMake(n, '<s>'+__('S')+'</s>', __('Strikethrough'), '<s>','</s>');
  85. btnMake(n, '&lt;br&gt;', __('Force line break'), '<br>','', true);
  86. btnMake(n, '---', __('Horizontal line'), '\n\n---\n\n', '', true);
  87. btnMake(n, __('URL'), __('Add URL to selected text'),
  88. function(e) {
  89. try {edWrapInTag('[', ']('+prompt(__('URL')+':')+')', edInit(e.target))}
  90. catch(e) {};
  91. });
  92. btnMake(n, __('Image (https)'), __('Convert selected https://url to inline image'), '!['+__('image')+'](', ')');
  93. if (inForum)
  94. btnMake(n, __('Table'), __('Insert table template'), __('\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n'), '', true);
  95. btnMake(n, __('Code'), __('Apply CODE markdown to selected text'),
  96. function(e){
  97. var ed = edInit(e.target);
  98. if (ed.sel.indexOf('\n') < 0)
  99. edWrapInTag('`', '`', ed);
  100. else
  101. edWrapInTag(((ed.sel1==0) || (ed.text.charAt(ed.sel1-1) == '\n') ? '' : '\n') + '```' + (ed.sel.charAt(0) == '\n' ? '' : '\n'),
  102. (ed.sel.substr(-1) == '\n' ? '' : '\n') + '```' + (ed.text.substr(ed.sel2,1) == '\n' ? '' : '\n'),
  103. ed);
  104. });
  105. }
  106.  
  107. function btnMake(afterNode, label, title, tag1_or_cb, tag2, noWrap) {
  108. var a = document.createElement('a');
  109. a.className = 'Button';
  110. a.innerHTML = label;
  111. a.title = title;
  112. if (inForum)
  113. a.style.setProperty('float','right');
  114. a.addEventListener('click',
  115. typeof(tag1_or_cb) == 'function'
  116. ? tag1_or_cb
  117. : noWrap ? function(e){edInsertText(tag1_or_cb, edInit(e.target))}
  118. : function(e){edWrapInTag(tag1_or_cb, tag2, edInit(e.target))});
  119. var nparent = afterNode.parentNode;
  120. a.textAreaNode = nparent.textAreaNode;
  121. inForum ? nparent.insertBefore(a, afterNode.nextElementSibling) : nparent.appendChild(a);
  122. }
  123.  
  124. function edInit(btn) {
  125. var ed = {node: btn.textAreaNode || btn.parentNode.textAreaNode}
  126. ed.sel1 = ed.node.selectionStart;
  127. ed.sel2 = ed.node.selectionEnd,
  128. ed.text = ed.node.value;
  129. ed.sel = ed.text.substring(ed.sel1, ed.sel2);
  130. return ed;
  131. }
  132.  
  133. function edWrapInTag(tag1, tag2, ed) {
  134. ed.node.value = ed.text.substr(0, ed.sel1) + tag1 + ed.sel + (tag2?tag2:tag1) + ed.text.substr(ed.sel2);
  135. ed.node.setSelectionRange(ed.sel1 + tag1.length, ed.sel1 + tag1.length + ed.sel.length);
  136. ed.node.focus();
  137. }
  138.  
  139. function edInsertText(text, ed) {
  140. ed.node.value = ed.text.substr(0, ed.sel2) + text + ed.text.substr(ed.sel2);
  141. ed.node.setSelectionRange(ed.sel2 + text.length, ed.sel2 + text.length);
  142. ed.node.focus();
  143. }
  144.  
  145. var __ = (function (l, langs) {
  146. var lang = langs[l] || langs[l.replace(/-.+/, '')];
  147. return lang ? function (text) { return lang[text] || text; }
  148. : function (text) { return text; } // No matching language, fallback to english
  149. })(location.pathname.match(/^\/(.+?)\//)[1], {
  150. // Can be full name, or just the beginning part.
  151. 'zh-CN': {
  152. 'Bold': '粗体',
  153. 'Italic': '斜体',
  154. 'Underline': '下划线',
  155. 'Strikethrough': '删除线',
  156. 'Force line break': '强制换行',
  157. 'Horizontal line': '水平分割线',
  158. 'URL': '链接',
  159. 'Add URL to selected text': '为所选文字添加链接',
  160. 'Image (https)': '图片 (https)',
  161. 'Convert selected https://url to inline image': '将所选地址转换为行内图片',
  162. 'image': '图片描述', // Default image alt value
  163. 'Table': '表格',
  164. 'Insert table template': '插入表格模板',
  165. 'Code': '代码',
  166. 'Apply CODE markdown to selected text': '将选中代码围起来',
  167.  
  168. '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n':
  169. '\n| 表头 1 | 表头 2 |\n|-------|-------|\n| 表格 1 | 表格 2 |\n| 表格 3 | 表格 4 |\n'
  170. },
  171. 'ru': {
  172. 'B': 'Ж',
  173. 'I': 'К',
  174. 'U': 'Ч',
  175. 'S': 'П',
  176. 'Bold': 'Жирный',
  177. 'Italic': 'Курсив',
  178. 'Underline': 'Подчеркнутый',
  179. 'Strikethrough': 'Перечеркнутый',
  180. 'Force line break': 'Новая строка',
  181. 'Horizontal line': 'Горизонтальная линия',
  182. 'URL': 'ссылка',
  183. 'Add URL to selected text': 'Добавить ссылку к выделенному тексту',
  184. 'Image (https)': 'Картинка (https)',
  185. 'Convert selected https://url to inline image': 'Преобразовать выделенный https:// адрес в картинку',
  186. 'image': 'картинка', // Default image alt value
  187. 'Table': 'Таблица',
  188. 'Insert table template': 'Вставить шаблон таблицы',
  189. 'Code': 'Код',
  190. 'Apply CODE markdown to selected text': 'Пометить выделенный фрагмент как программный код',
  191.  
  192. '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n':
  193. '\n| заголовок1 | заголовок2 |\n|-------|-------|\n| ячейка1 | ячейка2 |\n| ячейка3 | ячейка4 |\n'
  194. }
  195. });

QingJ © 2025

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