Greasy Fork镜像 支持简体中文。

Markdown toolbar for GreasyFork and UserStyles.org

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

目前為 2015-03-09 提交的版本,檢視 最新版本

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

QingJ © 2025

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