AddNewIssueButton

Adds an issue creation button to GitHub repository pages.

  1. // ==UserScript==
  2. // @name AddNewIssueButton
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.0.1
  5. // @description Adds an issue creation button to GitHub repository pages.
  6. // @author zcf0508
  7. // @match https://github.com/*/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=github.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. const repoData = window.location.pathname.split('/');
  17. const repoName = repoData[2];
  18. const userName = repoData[1];
  19. const repo = `${userName}/${repoName}`;
  20.  
  21. const createButton = () => {
  22. const button = document.createElement('button');
  23. button.textContent = 'Add Issue';
  24. button.className = 'btn btn-primary';
  25. button.addEventListener('click', () => {
  26. location.href = `https://github.com/${repo}/issues/new`;
  27. });
  28. return button;
  29. };
  30.  
  31. const appendButton = () => {
  32. if (repoName && userName) {
  33. let parent = document.querySelector('.AppHeader-actions');
  34. let issuesTab = document.querySelector('#issues-tab');
  35. if (parent && issuesTab) {
  36. parent.appendChild(createButton());
  37. }
  38. }
  39. };
  40.  
  41. // Use MutationObserver to ensure the button is added when the DOM changes
  42. const observer = new MutationObserver((mutationsList, observer) => {
  43. for (let mutation of mutationsList) {
  44. if (mutation.type === 'childList') {
  45. appendButton();
  46. observer.disconnect(); // Disconnect after adding the button
  47. break;
  48. }
  49. }
  50. });
  51.  
  52. observer.observe(document.body, { childList: true, subtree: true });
  53.  
  54. // Fallback to DOMContentLoaded in case the DOM is already loaded
  55. document.addEventListener('DOMContentLoaded', appendButton);
  56. })();