GitHub Issue Add Details

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

As of 2021-07-21. See the latest version.

  1. // ==UserScript==
  2. // @name GitHub Issue Add Details
  3. // @version 1.0.9
  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. // @include https://github.com/*
  9. // @run-at document-idle
  10. // @grant none
  11. // @require https://greatest.deepsurf.us/scripts/28721-mutations/code/mutations.js?version=952601
  12. // @require https://greatest.deepsurf.us/scripts/28239-rangy-inputs-mod-js/code/rangy-inputs-modjs.js?version=181769
  13. // @icon https://github.githubassets.com/pinned-octocat.svg
  14. // @supportURL https://github.com/Mottie/GitHub-userscripts/issues
  15. // ==/UserScript==
  16. (() => {
  17. "use strict";
  18.  
  19. const icon = `
  20. <svg class="octicon" style="pointer-events:none" xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 16 16">
  21. <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"/>
  22. </svg>`,
  23.  
  24. detailsBlock = [
  25. // start details block
  26. "<details>\n<summary>Title</summary>\n\n<!-- leave a blank line above -->\n",
  27. // selected content/caret will be placed here
  28. "\n</details>\n"
  29. ];
  30.  
  31. // Add insert details button
  32. function addDetailsButton() {
  33. const button = document.createElement("button");
  34. button.type = "button";
  35. button.className = "ghad-details toolbar-item tooltipped tooltipped-n";
  36. button.setAttribute("aria-label", "Add a details/summary block");
  37. button.setAttribute("tabindex", "-1");
  38. button.innerHTML = icon;
  39. [...document.querySelectorAll(".toolbar-commenting")].forEach(el => {
  40. if (el && !$(".ghad-details", el)) {
  41. const btn = $("[aria-label='Add a task list']", el);
  42. btn.parentNode.insertBefore(button.cloneNode(true), btn.nextSibling);
  43. }
  44. });
  45. }
  46.  
  47. function addBindings() {
  48. window.rangyInput.init();
  49. $("body").addEventListener("click", event => {
  50. const target = event.target;
  51. if (target && target.classList.contains("ghad-details")) {
  52. let textarea = target.closest(".previewable-comment-form");
  53. textarea = $(".comment-form-textarea", textarea);
  54. textarea.focus();
  55. window.rangyInput.surroundSelectedText(
  56. textarea,
  57. detailsBlock[0], // prefix
  58. detailsBlock[1] // suffix
  59. );
  60. return false;
  61. }
  62. });
  63. }
  64.  
  65. function $(str, el) {
  66. return (el || document).querySelector(str);
  67. }
  68.  
  69. document.addEventListener("ghmo:container", addDetailsButton);
  70. document.addEventListener("ghmo:comments", addDetailsButton);
  71.  
  72. addDetailsButton();
  73. addBindings();
  74.  
  75. })();