Greasy Fork is available in English.

Markdown toolbar for GF and USO

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

Version vom 28.07.2016. Aktuellste Version

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