GitHub Issue Highlighter

A userscript that highlights the linked-to comment

当前为 2019-02-17 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub Issue Highlighter
  3. // @version 1.1.1
  4. // @description A userscript that highlights the linked-to comment
  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 GM.addStyle
  11. // @grant GM_addStyle
  12. // @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js?updated=20180103
  13. // @icon https://github.githubassets.com/pinned-octocat.svg
  14. // ==/UserScript==
  15. (() => {
  16. "use strict";
  17.  
  18. // !important needed to override styles added by
  19. // https://github.com/StylishThemes/GitHub-Dark
  20. GM.addStyle(`
  21. .timeline-comment.selected,
  22. .timeline-comment.current-user.selected {
  23. border-color: #4183C4 !important;
  24. }
  25. .timeline-comment.selected .comment:before,
  26. .timeline-comment.current-user.selected:before {
  27. border-right-color: #4183C4 !important;
  28. }
  29. `);
  30.  
  31. const regex = /^#issue(comment)?-\d+/;
  32.  
  33. function init(event) {
  34. if (document.querySelector("#discussion_bucket")) {
  35. let target, indx,
  36. hash = window.location.hash;
  37. // remove "selected" class on hashchange
  38. if (event) {
  39. target = document.querySelectorAll(".timeline-comment");
  40. indx = target.length;
  41. while (indx--) {
  42. target[indx].classList.remove("selected");
  43. }
  44. }
  45. // add "selected" class
  46. if (regex.test(hash)) {
  47. target = document.querySelector(hash.match(regex)[0]);
  48. if (target) {
  49. target.querySelector(".timeline-comment").classList.add("selected");
  50. }
  51. }
  52. }
  53. }
  54.  
  55. window.addEventListener("hashchange", init);
  56. document.addEventListener("pjax:end", init);
  57. init();
  58.  
  59. })();