Github Comment Enhancer

Enhances Github comments

当前为 2014-05-18 提交的版本,查看 最新版本

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

QingJ © 2025

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