UserStyles.org markdown

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

  1. // ==UserScript==
  2. // @name Markdown toolbar for USO
  3. // @name:ru Markdown-тулбар для USO
  4. // @name:zh-CN UserStyles.org 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.4.9.1
  14. // @include https://forum.userstyles.org/discussion/*
  15. // @include https://forum.userstyles.org/post/discussion*
  16. // @include https://forum.userstyles.org/messages/*
  17. // @include https://forum.userstyles.org/messages/add*
  18. // @include https://forum.userstyles.org/*/editdiscussion/*
  19. // @run-at document-start
  20. // @grant GM_addStyle
  21. // ==/UserScript==
  22.  
  23. /* jshint lastsemic:true, multistr:true, laxbreak:true, -W030, -W041, -W084 */
  24.  
  25. var inForum = location.href.indexOf('/forum') > 0;
  26.  
  27. window.addEventListener('DOMContentLoaded', function(e) {
  28. if (inForum)
  29. addFeatures(document.querySelector('#Form_Format2:not([type="hidden"]), #Form_Format1').closest('label'));
  30. else
  31. if (nn = document.querySelectorAll('input[value="markdown"]'))
  32. for (var n, i=0; (i<nn.length) && (n=nn[i]); i++) {
  33. if (location.href.indexOf('/script_versions/'))
  34. n.click(); // posting a new script
  35. addFeatures(n.parentNode.appendChild(document.createElement('br')));
  36. }
  37.  
  38. new MutationObserver(function(mutations) {
  39. for (var i=0, ml=mutations.length, m; (i<ml) && (m=mutations[i]); i++)
  40. for (var j=0, nodes=m.addedNodes, nl=nodes.length, n; (j<nl) && (n=nodes[j]); j++)
  41. if (n.nodeType == 1)
  42. if (inForum) {
  43. if ((n.localName == 'label' && n.querySelector('input[value="Markdown"], input[value="Html"], input[value="markdown"], input[value="html"]'))
  44. || (n = n.querySelector('input[value="Markdown"], input[value="markdown"]') || n.querySelector('input[value="Html"], input[value="html"]')))
  45. return addFeatures(n.closest('label'));
  46. } else {
  47. if (((n.localName == 'input') && (n.value.toLowerCase() == 'Markdown'))
  48. || (n = n.querySelector('input[value="Markdown"], input[value="markdown"]'))) {
  49. if (location.href.indexOf('/script_versions/'))
  50. n.click();
  51. return addFeatures(n.parentNode.appendChild(document.createElement('br')));
  52. }
  53. }
  54. }).observe(document, {subtree:true, childList:true});
  55. });
  56.  
  57. function addFeatures(n) {
  58. if (!n)
  59. return;
  60.  
  61. if (inForum) {
  62.  
  63. var form = n.closest('form');
  64. if (form.action.indexOf('/edit') < 0)
  65. n.click();
  66. n.parentNode.textAreaNode = form.querySelector('textarea.TextBox, textarea.previewable');
  67. // add formatting help tooltips
  68. n.previousElementSibling.insertAdjacentHTML('beforeend',
  69. ' (<a href="/help/allowed-markup" target="_blank" title="'+
  70. '* (name, title), a (href), abbr, b, blockquote (cite), br, center, cite, code, dd, del, dfn, div, dl, dt, em, '+
  71. 'h1, h2, h3, h4, h5, h6, hr, i, ins, img (alt, height, src (https), width), kbd, li, mark, ol, p, pre, q (cite), '+
  72. 'rp, rt, ruby, s, samp, small, span, strike, strong, tt, table, tbody, tfoot, thead, td, th, tr, sub, sup, '+
  73. 'time (datetime, pubdate), u, ul, var">?</a>)');
  74. n.insertAdjacentHTML('beforeend',
  75. ' (<a href="http://www.darkcoding.net/software/markdown-quick-reference/" target="_blank">?</a>)');
  76. if (location.href.indexOf('/forum/messages/') > -1)
  77. GM_addStyle('#ConversationForm label { display:inline-block; margin-right:2ex }\
  78. #ConversationForm .TextBox { margin-top:0 }');
  79. } else {
  80.  
  81. for (var wrapper=n; wrapper = wrapper.parentNode; )
  82. if (t = wrapper.querySelector('textarea[id*="additional-info"]')) {
  83. n.parentNode.textAreaNode = t;
  84. break;
  85. }
  86. GM_addStyle('\
  87. .Button {\
  88. display: inline-block;\
  89. cursor: pointer;\
  90. margin: 0px;\
  91. font-size: 12px;\
  92. line-height: 1;\
  93. font-weight: bold;\
  94. padding: 4px 6px;\
  95. background: -moz-linear-gradient(center bottom , #CCC 0%, #FAFAFA 100%) repeat scroll 0% 0% #F8F8F8;\
  96. border: 1px solid #999;\
  97. border-radius: 2px;\
  98. white-space: nowrap;\
  99. text-shadow: 0px 1px 0px #FFF;\
  100. box-shadow: 0px 1px 0px #FFF inset, 0px -1px 2px #BBB inset;\
  101. color: #333;}');
  102. }
  103.  
  104. // add buttons
  105. btnMake(n, '<b>'+__('B')+'</b>', __('Bold'), '**');
  106. btnMake(n, '<i>'+__('I')+'</i>', __('Italic'), '*');
  107. btnMake(n, '<u>'+__('U')+'</u>', __('Underline'), '<u>','</u>');
  108. btnMake(n, '<s>'+__('S')+'</s>', __('Strikethrough'), '<s>','</s>');
  109. btnMake(n, '&lt;br&gt;', __('Force line break'), '<br>','', true);
  110. btnMake(n, '---', __('Horizontal line'), '\n\n---\n\n', '', true);
  111. btnMake(n, __('URL'), __('Add URL to selected text'),
  112. function(e) {
  113. try {edWrapInTag('[', ']('+prompt(__('URL')+':')+')', edInit(e.target))}
  114. catch(ex) {}
  115. });
  116. btnMake(n, __('Image (https)'), __('Convert selected https://url to inline image'), '!['+__('image')+'](', ')');
  117. if (inForum)
  118. btnMake(n, __('Table'), __('Insert table template'), __('\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n'), '', true);
  119. btnMake(n, __('Code'), __('Apply CODE markdown to selected text'),
  120. function(e){
  121. var ed = edInit(e.target);
  122. if (ed.sel.indexOf('\n') < 0)
  123. edWrapInTag('`', '`', ed);
  124. else
  125. edWrapInTag(((ed.sel1==0) || (ed.text.charAt(ed.sel1-1) == '\n') ? '' : '\n') + '```' + (ed.sel.charAt(0) == '\n' ? '' : '\n'),
  126. (ed.sel.substr(-1) == '\n' ? '' : '\n') + '```' + (ed.text.substr(ed.sel2,1) == '\n' ? '' : '\n'),
  127. ed);
  128. });
  129. }
  130.  
  131. function btnMake(afterNode, label, title, tag1_or_cb, tag2, noWrap) {
  132. var a = document.createElement('a');
  133. a.className = 'Button';
  134. a.innerHTML = label;
  135. a.title = title;
  136. if (inForum)
  137. a.style.setProperty('float','right');
  138. a.addEventListener('click',
  139. typeof(tag1_or_cb) == 'function'
  140. ? tag1_or_cb
  141. : noWrap ? function(e){edInsertText(tag1_or_cb, edInit(e.target))}
  142. : function(e){edWrapInTag(tag1_or_cb, tag2, edInit(e.target))});
  143. var nparent = afterNode.parentNode;
  144. a.textAreaNode = nparent.textAreaNode;
  145. inForum ? nparent.insertBefore(a, nparent.firstElementChild) : nparent.appendChild(a);
  146. }
  147.  
  148. function edInit(btn) {
  149. var ed = {node: btn.textAreaNode || btn.parentNode.textAreaNode};
  150. ed.sel1 = ed.node.selectionStart;
  151. ed.sel2 = ed.node.selectionEnd,
  152. ed.text = ed.node.value;
  153. ed.sel = ed.text.substring(ed.sel1, ed.sel2);
  154. return ed;
  155. }
  156.  
  157. function edWrapInTag(tag1, tag2, ed) {
  158. ed.node.value = ed.text.substr(0, ed.sel1) + tag1 + ed.sel + (tag2?tag2:tag1) + ed.text.substr(ed.sel2);
  159. ed.node.setSelectionRange(ed.sel1 + tag1.length, ed.sel1 + tag1.length + ed.sel.length);
  160. ed.node.focus();
  161. }
  162.  
  163. function edInsertText(text, ed) {
  164. ed.node.value = ed.text.substr(0, ed.sel2) + text + ed.text.substr(ed.sel2);
  165. ed.node.setSelectionRange(ed.sel2 + text.length, ed.sel2 + text.length);
  166. ed.node.focus();
  167. }
  168.  
  169. var __ = (function (l, langs) {
  170. var lang = langs[l] || langs[l.replace(/-.+/, '')];
  171. return lang ? function (text) { return lang[text] || text }
  172. : function (text) { return text }; // No matching language, fallback to english
  173. })(location.pathname.match(/^\/(.+?)\//)[1], {
  174. // Can be full name, or just the beginning part.
  175. 'zh-CN': {
  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': '将所选地址转换为行内图片',
  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. 'ru': {
  196. 'B': 'Ж',
  197. 'I': 'К',
  198. 'U': 'Ч',
  199. 'S': 'П',
  200. 'Bold': 'Жирный',
  201. 'Italic': 'Курсив',
  202. 'Underline': 'Подчеркнутый',
  203. 'Strikethrough': 'Перечеркнутый',
  204. 'Force line break': 'Новая строка',
  205. 'Horizontal line': 'Горизонтальная линия',
  206. 'URL': 'ссылка',
  207. 'Add URL to selected text': 'Добавить ссылку к выделенному тексту',
  208. 'Image (https)': 'Картинка (https)',
  209. 'Convert selected https://url to inline image': 'Преобразовать выделенный https:// адрес в картинку',
  210. 'image': 'картинка', // Default image alt value
  211. 'Table': 'Таблица',
  212. 'Insert table template': 'Вставить шаблон таблицы',
  213. 'Code': 'Код',
  214. 'Apply CODE markdown to selected text': 'Пометить выделенный фрагмент как программный код',
  215.  
  216. '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n':
  217. '\n| заголовок1 | заголовок2 |\n|-------|-------|\n| ячейка1 | ячейка2 |\n| ячейка3 | ячейка4 |\n'
  218. },
  219. 'fr': {
  220. 'B': 'G',
  221. 'I': 'I',
  222. 'U': 'S',
  223. 'S': 'B',
  224. 'Bold': 'Gras',
  225. 'Italic': 'Italique',
  226. 'Underline': 'Souligné',
  227. 'Strikethrough': 'Barré',
  228. 'Force line break': 'Forcer le saut de ligne',
  229. 'Horizontal line': 'Ligne horizontale',
  230. 'URL': 'URL',
  231. 'Add URL to selected text': 'Ajouter URL au texte sélectionné',
  232. 'Image (https)': 'Image (https)',
  233. 'Convert selected https://url to inline image': 'Convertir https://url sélectionnés en images',
  234. 'image': 'image', // Default image alt value
  235. 'Table': 'Tableau',
  236. 'Insert table template': 'Insérer un modèle de table',
  237. 'Code': 'Code',
  238. 'Apply CODE markdown to selected text': 'Appliquer CODE markdown au texte sélectionné',
  239.  
  240. '\n| head1 | head2 |\n|-------|-------|\n| cell1 | cell2 |\n| cell3 | cell4 |\n':
  241. '\n| En-tête 1 | En-tête 2 |\n|-------|-------|\n| cellule 1 | cellule 2 |\n| cellule 3 | cellule 4 |\n'
  242. }
  243. });

QingJ © 2025

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