Github Comment Enhancer

Enhances Github comments

目前为 2014-06-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @id Github_Comment_Enhancer@https://github.com/jerone/UserScripts
  3. // @name Github Comment Enhancer
  4. // @namespace https://github.com/jerone/UserScripts
  5. // @description Enhances Github comments
  6. // @author jerone
  7. // @copyright 2014+, jerone (http://jeroenvanwarmerdam.nl)
  8. // @license GNU GPLv3
  9. // @homepage https://github.com/jerone/UserScripts/tree/master/Github_Comment_Enhancer
  10. // @homepageURL https://github.com/jerone/UserScripts/tree/master/Github_Comment_Enhancer
  11. // @version 1.4
  12. // @grant none
  13. // @run-at document-end
  14. // @include https://github.com/*/*/issues/*
  15. // @include https://github.com/*/*/pull/*
  16. // @include https://github.com/*/*/commit/*
  17. // @include https://github.com/*/*/wiki/*
  18. // @include https://gist.github.com/*
  19. // ==/UserScript==
  20. /* global unsafeWindow */
  21.  
  22. (function() {
  23.  
  24. String.format = function(string) {
  25. var args = Array.prototype.slice.call(arguments, 1, arguments.length);
  26. return string.replace(/{(\d+)}/g, function(match, number) {
  27. return typeof args[number] !== "undefined" ? args[number] : match;
  28. });
  29. };
  30.  
  31. // Source: https://github.com/gollum/gollum/blob/9c714e768748db4560bc017cacef4afa0c751a63/lib/gollum/public/gollum/javascript/editor/langs/markdown.js
  32. var MarkDown = {
  33. "function-bold": {
  34. search: /^(\s*)([\s\S]*?)(\s*)$/g,
  35. replace: "$1**$2**$3"
  36. },
  37. "function-italic": {
  38. search: /^(\s*)([\s\S]*?)(\s*)$/g,
  39. replace: "$1_$2_$3"
  40. },
  41. "function-strikethrough": {
  42. search: /^(\s*)([\s\S]*?)(\s*)$/g,
  43. replace: "$1~~$2~~$3"
  44. },
  45.  
  46. "function-h1": {
  47. search: /(.+)([\n]?)/g,
  48. replace: "# $1$2",
  49. forceNewline: true
  50. },
  51. "function-h2": {
  52. search: /(.+)([\n]?)/g,
  53. replace: "## $1$2",
  54. forceNewline: true
  55. },
  56. "function-h3": {
  57. search: /(.+)([\n]?)/g,
  58. replace: "### $1$2",
  59. forceNewline: true
  60. },
  61. "function-h4": {
  62. search: /(.+)([\n]?)/g,
  63. replace: "#### $1$2",
  64. forceNewline: true
  65. },
  66. "function-h5": {
  67. search: /(.+)([\n]?)/g,
  68. replace: "##### $1$2",
  69. forceNewline: true
  70. },
  71. "function-h6": {
  72. search: /(.+)([\n]?)/g,
  73. replace: "###### $1$2",
  74. forceNewline: true
  75. },
  76.  
  77. "function-link": {
  78. exec: function(txt, selText, commentForm, next) {
  79. var selTxt = selText.trim(),
  80. isUrl = selTxt && /(?:https?:\/\/)|(?:www\.)/.test(selTxt),
  81. href = window.prompt("Link href:", isUrl ? selTxt : ""),
  82. text = window.prompt("Link text:", isUrl ? "" : selTxt);
  83. if (href) {
  84. next(String.format("[{0}]({1}){2}", text || href, href, (/\s+$/.test(selText) ? " " : "")));
  85. }
  86. }
  87. },
  88. "function-image": {
  89. exec: function(txt, selText, commentForm, next) {
  90. var selTxt = selText.trim(),
  91. isUrl = selTxt && /(?:https?:\/\/)|(?:www\.)/.test(selTxt),
  92. href = window.prompt("Image href:", isUrl ? selTxt : ""),
  93. text = window.prompt("Image text:", isUrl ? "" : selTxt);
  94. if (href) {
  95. next(String.format("![{0}]({1}){2}", text || href, href, (/\s+$/.test(selText) ? " " : "")));
  96. }
  97. }
  98. },
  99.  
  100. "function-ul": {
  101. search: /(.+)([\n]?)/g,
  102. replace: "* $1$2",
  103. forceNewline: true
  104. },
  105. "function-ol": {
  106. exec: function(txt, selText, commentForm, next) {
  107. var repText = "";
  108. if (!selText) {
  109. repText = "1. ";
  110. } else {
  111. var lines = selText.split("\n"),
  112. hasContent = /[\w]+/;
  113. for (var i = 0; i < lines.length; i++) {
  114. if (hasContent.test(lines[i])) {
  115. repText += String.format("$0. $1\n", i + 1, lines[i]);
  116. }
  117. }
  118. }
  119. next(repText);
  120. }
  121. },
  122. "function-checklist": {
  123. search: /(.+)([\n]?)/g,
  124. replace: "* [ ] $1$2",
  125. forceNewline: true
  126. },
  127.  
  128. "function-code": {
  129. exec: function(txt, selText, commentForm, next) {
  130. var rt = selText.indexOf("\n") > -1 ? "$1\n```\n$2\n```$3" : "$1`$2`$3";
  131. next(selText.replace(/^(\s*)([\s\S]*?)(\s*)$/g, rt));
  132. }
  133. },
  134. "function-blockquote": {
  135. search: /(.+)([\n]?)/g,
  136. replace: "> $1$2",
  137. forceNewline: true
  138. },
  139. "function-hr": {
  140. append: "\n***\n",
  141. forceNewline: true
  142. },
  143. "function-table": {
  144. append: "\n" +
  145. "| Head | Head | Head |\n" +
  146. "| :--- | :--: | ---: |\n" +
  147. "| Cell | Cell | Cell |\n" +
  148. "| Cell | Cell | Cell |\n",
  149. forceNewline: true
  150. },
  151.  
  152. "function-clear": {
  153. exec: function(txt, selText, commentForm, next) {
  154. commentForm.value = "";
  155. next("");
  156. }
  157. },
  158.  
  159. "function-snippets-useragent": {
  160. exec: function(txt, selText, commentForm, next) {
  161. next("`" + navigator.userAgent + "`");
  162. }
  163. }
  164. };
  165.  
  166. var editorHTML = (function() {
  167. return '<div id="gollum-editor-function-buttons" style="float: left;">' +
  168. ' <div class="button-group">' +
  169. ' <a href="#" id="function-bold" class="minibutton function-button" title="Bold">' +
  170. ' <b style="font-weight: bolder;">B</b>' +
  171. ' </a>' +
  172. ' <a href="#" id="function-italic" class="minibutton function-button" title="Italic">' +
  173. ' <em>i</em>' +
  174. ' </a>' +
  175. ' <a href="#" id="function-strikethrough" class="minibutton function-button" title="Strikethrough">' +
  176. ' <s>S</s>' +
  177. ' </a>' +
  178. ' </div>' +
  179.  
  180. ' <div class="button-group">' +
  181. ' <div class="select-menu js-menu-container js-select-menu">' +
  182. ' <span class="minibutton select-menu-button icon-only js-menu-target" title="Headers" style="padding:0 20px 0 7px; width:auto; border-bottom-right-radius:3px; border-top-right-radius:3px;">' +
  183. ' <span class="js-select-button">h#</span>' +
  184. ' </span>' +
  185. ' <div class="select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container" style="top: 26px;">' +
  186. ' <div class="select-menu-modal" style="width:auto;">' +
  187. ' <div class="select-menu-header">' +
  188. ' <span class="select-menu-title">Choose header</span>' +
  189. ' <span class="octicon octicon-remove-close js-menu-close"></span>' +
  190. ' </div>' +
  191. ' <div class="button-group">' +
  192. ' <a href="#" id="function-h1" class="minibutton function-button js-navigation-item js-menu-close" title="Header 1">' +
  193. ' <b class="select-menu-item-text js-select-button-text">h1</b>' +
  194. ' </a>' +
  195. ' <a href="#" id="function-h2" class="minibutton function-button js-navigation-item js-menu-close" title="Header 2">' +
  196. ' <b class="select-menu-item-text js-select-button-text">h2</b>' +
  197. ' </a>' +
  198. ' <a href="#" id="function-h3" class="minibutton function-button js-navigation-item js-menu-close" title="Header 3">' +
  199. ' <b class="select-menu-item-text js-select-button-text">h3</b>' +
  200. ' </a>' +
  201. ' <a href="#" id="function-h4" class="minibutton function-button js-navigation-item js-menu-close" title="Header 4">' +
  202. ' <b class="select-menu-item-text js-select-button-text">h4</b>' +
  203. ' </a>' +
  204. ' <a href="#" id="function-h5" class="minibutton function-button js-navigation-item js-menu-close" title="Header 5">' +
  205. ' <b class="select-menu-item-text js-select-button-text">h5</b>' +
  206. ' </a>' +
  207. ' <a href="#" id="function-h6" class="minibutton function-button js-navigation-item js-menu-close" title="Header 6">' +
  208. ' <b class="select-menu-item-text js-select-button-text">h6</b>' +
  209. ' </a>' +
  210. ' </div>' +
  211. ' </div>' +
  212. ' </div>' +
  213. ' </div>' +
  214. ' </div>' +
  215.  
  216. ' <div class="button-group">' +
  217. ' <a href="#" id="function-link" class="minibutton function-button" title="Link">' +
  218. ' <span class="octicon octicon-link"></span>' +
  219. ' </a>' +
  220. ' <a href="#" id="function-image" class="minibutton function-button" title="Image">' +
  221. ' <span class="octicon octicon-file-media"></span>' +
  222. ' </a>' +
  223. ' </div>' +
  224. ' <div class="button-group">' +
  225. ' <a href="#" id="function-ul" class="minibutton function-button" title="Unordered List">' +
  226. ' <span class="octicon octicon-list-unordered"></span>' +
  227. ' </a>' +
  228. ' <a href="#" id="function-ol" class="minibutton function-button" title="Ordered List">' +
  229. ' <span class="octicon octicon-list-ordered"></span>' +
  230. ' </a>' +
  231. ' <a href="#" id="function-checklist" class="minibutton function-button" title="Task List">' +
  232. ' <span class="octicon octicon-checklist"></span>' +
  233. ' </a>' +
  234. ' </div>' +
  235.  
  236. ' <div class="button-group">' +
  237. ' <a href="#" id="function-code" class="minibutton function-button" title="Code">' +
  238. ' <span class="octicon octicon-code"></span>' +
  239. ' </a>' +
  240. ' <a href="#" id="function-blockquote" class="minibutton function-button" title="Blockquote">' +
  241. ' <span class="octicon octicon-quote"></span>' +
  242. ' </a>' +
  243. ' <a href="#" id="function-hr" class="minibutton function-button" title="Horizontal Rule">' +
  244. ' <span class="octicon octicon-horizontal-rule"></span>' +
  245. ' </a>' +
  246. ' <a href="#" id="function-table" class="minibutton function-button" title="Table">' +
  247. ' <span class="octicon octicon-three-bars"></span>' +
  248. ' </a>' +
  249. ' </div>' +
  250.  
  251. ' <div class="button-group">' +
  252. ' <div class="select-menu js-menu-container js-select-menu">' +
  253. ' <span class="minibutton select-menu-button js-menu-target" title="Snippets" style="padding:0 20px 0 7px; width:auto; border-bottom-right-radius:3px; border-top-right-radius:3px;">' +
  254. ' <span class="octicon octicon-pin"></span>' +
  255. ' </span>' +
  256. ' <div class="select-menu-modal-holder js-menu-content js-navigation-container js-active-navigation-container">' +
  257. ' <div class="select-menu-modal">' +
  258. ' <div class="select-menu-header">' +
  259. ' <span class="select-menu-title">Snippets</span>' +
  260. ' <span class="octicon octicon-remove-close js-menu-close"></span>' +
  261. ' </div>' +
  262. ' <div class="select-menu-filters">' +
  263. ' <div class="select-menu-text-filter">' +
  264. ' <input type="text" placeholder="Filter snippets..." class="js-filterable-field js-navigation-enable" id="context-snippets-filter-field">' +
  265. ' </div>' +
  266. ' </div>' +
  267. ' <div class="select-menu-list">' +
  268. ' <div data-filterable-for="context-snippets-filter-field">' +
  269. ' <a href="#" id="function-snippets-useragent" class="function-button select-menu-item js-navigation-item" title="Add UserAgent">' +
  270. ' <span class="select-menu-item-text js-select-button-text">Add UserAgent</span>' +
  271. ' </a>' +
  272. ' </div>' +
  273. ' <div class="select-menu-no-results">Nothing to show</div>' +
  274. ' </div>' +
  275. ' </div>' +
  276. ' </div>' +
  277. ' </div>' +
  278. ' </div>' +
  279.  
  280. '</div>' +
  281.  
  282. '<div class="button-group" style="float:right;">' +
  283. ' <a href="#" id="function-clear" class="minibutton function-button" title="Clear">' +
  284. ' <span class="octicon octicon-circle-slash"></span>' +
  285. ' </a>' +
  286. '</div>';
  287. })();
  288.  
  289. // Source: https://github.com/gollum/gollum/blob/9c714e768748db4560bc017cacef4afa0c751a63/lib/gollum/public/gollum/javascript/editor/gollum.editor.js#L516
  290. function executeAction(definitionObject, commentForm) {
  291. var txt = commentForm.value,
  292. selPos = {
  293. start: commentForm.selectionStart,
  294. end: commentForm.selectionEnd
  295. },
  296. selText = txt.substring(selPos.start, selPos.end),
  297. repText = selText,
  298. reselect = true,
  299. cursor = null;
  300.  
  301. // execute replacement function;
  302. if (definitionObject.exec) {
  303. definitionObject.exec(txt, selText, commentForm, function(repText) {
  304. replaceFieldSelection(commentForm, repText);
  305. });
  306. return;
  307. }
  308.  
  309. // execute a search;
  310. var searchExp = new RegExp(definitionObject.search || /([^\n]+)/gi);
  311.  
  312. // replace text;
  313. if (definitionObject.replace) {
  314. var rt = definitionObject.replace;
  315. repText = repText.replace(searchExp, rt);
  316. repText = repText.replace(/\$[\d]/g, "");
  317. if (repText === "") {
  318. cursor = rt.indexOf("$1");
  319. repText = rt.replace(/\$[\d]/g, "");
  320. if (cursor === -1) {
  321. cursor = Math.floor(rt.length / 2);
  322. }
  323. }
  324. }
  325.  
  326. // append if necessary;
  327. if (definitionObject.append) {
  328. if (repText === selText) {
  329. reselect = false;
  330. }
  331. repText += definitionObject.append;
  332. }
  333.  
  334. if (repText) {
  335. if (definitionObject.forceNewline === true && (selPos.start > 0 && txt.substr(Math.max(0, selPos.start - 1), 1) !== "\n")) {
  336. repText = "\n" + repText;
  337. }
  338. replaceFieldSelection(commentForm, repText, reselect, cursor);
  339. }
  340. }
  341.  
  342. // Source: https://github.com/gollum/gollum/blob/9c714e768748db4560bc017cacef4afa0c751a63/lib/gollum/public/gollum/javascript/editor/gollum.editor.js#L708
  343. function replaceFieldSelection(commentForm, replaceText, reselect, cursorOffset) {
  344. var txt = commentForm.value,
  345. selPos = {
  346. start: commentForm.selectionStart,
  347. end: commentForm.selectionEnd
  348. };
  349.  
  350. var selectNew = true;
  351. if (reselect === false) {
  352. selectNew = false;
  353. }
  354.  
  355. var scrollTop = null;
  356. if (commentForm.scrollTop) {
  357. scrollTop = commentForm.scrollTop;
  358. }
  359.  
  360. commentForm.value = txt.substring(0, selPos.start) + replaceText + txt.substring(selPos.end);
  361. commentForm.focus();
  362.  
  363. // Gist Github requires that the comment form change event be triggered to update the preview;
  364. unsafeWindow.$(commentForm).trigger("change");
  365.  
  366. if (selectNew) {
  367. if (cursorOffset) {
  368. commentForm.setSelectionRange(selPos.start + cursorOffset, selPos.start + cursorOffset);
  369. } else {
  370. commentForm.setSelectionRange(selPos.start, selPos.start + replaceText.length);
  371. }
  372. }
  373.  
  374. if (scrollTop) {
  375. commentForm.scrollTop = scrollTop;
  376. }
  377. }
  378.  
  379. function isWiki() {
  380. return /\/wiki\//.test(location.href);
  381. }
  382.  
  383. var functionButtonClick = function(e) {
  384. e.preventDefault();
  385. executeAction(MarkDown[this.id], this.commentForm);
  386. return false;
  387. };
  388.  
  389. function addToolbar() {
  390. if (isWiki()) {
  391. // Override existing language with improved & missing functions and remove existing click events;
  392. unsafeWindow.$.GollumEditor.defineLanguage("markdown", MarkDown);
  393. unsafeWindow.$(".function-button:not(#function-help)").unbind("click");
  394.  
  395. // Remove existing click events when changing languages;
  396. document.getElementById("wiki_format").addEventListener("change", function() {
  397. unsafeWindow.$(".function-button:not(#function-help)").unbind("click");
  398.  
  399. Array.forEach(document.querySelectorAll(".comment-form-textarea .function-button"), function(button) {
  400. button.removeEventListener("click", functionButtonClick);
  401. });
  402. });
  403. }
  404.  
  405. Array.forEach(document.querySelectorAll(".comment-form-textarea,.js-comment-field"), function(commentForm) {
  406. if (commentForm.classList.contains("GithubCommentEnhancer")) { return; }
  407. commentForm.classList.add("GithubCommentEnhancer");
  408.  
  409. var gollumEditor;
  410. if (isWiki()) {
  411. gollumEditor = document.getElementById("gollum-editor-function-bar");
  412. var temp = document.createElement("div");
  413. temp.innerHTML = editorHTML;
  414. temp.firstChild.appendChild(document.getElementById("function-help")); // restore the help button;
  415. gollumEditor.replaceChild(temp.querySelector("#gollum-editor-function-buttons"), document.getElementById("gollum-editor-function-buttons"));
  416. Array.forEach(temp.children, function(elm) {
  417. elm.style.position = "absolute";
  418. elm.style.right = "30px";
  419. elm.style.top = "0";
  420. commentForm.parentNode.insertBefore(elm, commentForm);
  421. });
  422. temp = null;
  423. } else {
  424. gollumEditor = document.createElement("div");
  425. gollumEditor.innerHTML = editorHTML;
  426. gollumEditor.id = "gollum-editor-function-bar";
  427. gollumEditor.style.border = "0 none";
  428. gollumEditor.style.height = "26px";
  429. gollumEditor.style.margin = "10px 0";
  430. gollumEditor.style.paddingBottom = "10px";
  431. gollumEditor.classList.add("active");
  432. commentForm.parentNode.insertBefore(gollumEditor, commentForm);
  433. }
  434.  
  435. Array.forEach(gollumEditor.parentNode.querySelectorAll(".function-button"), function(button) {
  436. if (button.classList.contains("minibutton")) {
  437. button.style.padding = "0px";
  438. button.style.textAlign = "center";
  439. button.style.width = "30px";
  440. button.firstElementChild.style.marginRight = "0px";
  441. }
  442. button.commentForm = commentForm; // remove event listener doesn't accept `bind`;
  443. button.addEventListener("click", functionButtonClick);
  444. });
  445. });
  446. }
  447.  
  448. // init;
  449. addToolbar();
  450.  
  451. // on pjax;
  452. unsafeWindow.$(document).on("pjax:success", addToolbar);
  453.  
  454. // on page update;
  455. unsafeWindow.$.pageUpdate(function() {
  456. window.setTimeout(function() {
  457. addToolbar();
  458. }, 1);
  459. });
  460.  
  461. })();