Greasy Fork 支持简体中文。

GitHub Issue Add Details

A userscript that adds a button to insert a details block into comments

目前為 2024-02-17 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub Issue Add Details
  3. // @version 1.0.12
  4. // @description A userscript that adds a button to insert a details block into comments
  5. // @license MIT
  6. // @author Rob Garrison
  7. // @namespace https://github.com/Mottie
  8. // @match https://github.com/*
  9. // @run-at document-idle
  10. // @grant none
  11. // @require https://greasyfork.org/scripts/28721-mutations/code/mutations.js?version=1108163
  12. // @require https://greasyfork.org/scripts/398877-utils-js/code/utilsjs.js?version=1079637
  13. // @require https://greasyfork.org/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
  14. // @icon https://github.githubassets.com/pinned-octocat.svg
  15. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  16. // ==/UserScript==
  17.  
  18. /* global $ $$ on make */
  19. (() => {
  20. "use strict";
  21.  
  22. const icon = `
  23. <svg class="octicon" style="pointer-events:none" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
  24. <path d="M15.5 9h-7C8 9 8 8.6 8 8s0-1 .5-1h7c.5 0 .5.4.5 1s0 1-.5 1zm-5-5c-.5 0-.5-.4-.5-1s0-1 .5-1h5c.5 0 .5.4.5 1s0 1-.5 1h-5zM0 2h8L4 7 0 2zm8.5 10h7c.5 0 .5.4.5 1s0 1-.5 1h-7c-.5 0-.5-.4-.5-1s0-1 .5-1z"/>
  25. </svg>`,
  26.  
  27. detailsBlock = [
  28. // start details block
  29. "<details>\n<summary>Title</summary>\n\n<!-- leave a blank line above -->\n",
  30. // selected content/caret will be placed here
  31. "\n</details>\n"
  32. ];
  33.  
  34. // Add insert details button
  35. const addDetailsButton = () => {
  36. const button = make({
  37. el: "button",
  38. className: "ghad-details btn-link toolbar-item btn-octicon no-underline tooltipped tooltipped-n",
  39. attrs: {
  40. "aria-label": "Add a details/summary block",
  41. tabindex: "-1",
  42. type: "button"
  43. },
  44. html: icon
  45. });
  46. $$(".toolbar-commenting").forEach(el => {
  47. if (el && !$(".ghad-details", el)) {
  48. const btn = $("md-quote", el);
  49. btn.before(button.cloneNode(true));
  50. }
  51. });
  52. };
  53.  
  54. const addBindings = () => {
  55. window.rangyInput.init();
  56. on($("body"), "click", event => {
  57. const { target } = event;
  58. if (target?.classList.contains("ghad-details")) {
  59. event.preventDefault();
  60. const form = target.closest(".previewable-comment-form");
  61. const textarea = $(".comment-form-textarea", form);
  62. setTimeout(() => {
  63. textarea.focus();
  64. window.rangyInput.surroundSelectedText(
  65. textarea,
  66. detailsBlock[0], // prefix
  67. detailsBlock[1] // suffix
  68. );
  69. }, 100);
  70. return false;
  71. }
  72. });
  73. };
  74.  
  75. on(document, "ghmo:container, ghmo:comments", addDetailsButton);
  76.  
  77. addDetailsButton();
  78. addBindings();
  79.  
  80. })();