Graphite GitHub button

Add a button to go from app.graphite.dev to github.com

目前為 2023-07-27 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Graphite GitHub button
  3. // @description Add a button to go from app.graphite.dev to github.com
  4. // @match https://app.graphite.dev/*
  5. // @version 0.2
  6. // @run-at document-start
  7. // @icon data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
  8. // @grant none
  9. // @license MIT
  10. // @namespace https://app.graphite.dev
  11. // ==/UserScript==
  12.  
  13. const PATH_REGEX = /^\/github\/pr\/(\w+)\/(\w+)\/(\d+).*$/;
  14. const SELECTOR =
  15. '[class^="PullRequestTitleBar_container_"] > div:nth-child(1) > div:nth-child(2)';
  16.  
  17. const addButton = (toolbar) => {
  18. const [_, org, repo, pr] = window.location.pathname.match(PATH_REGEX);
  19. const gitHubLink = `https://github.com/${org}/${repo}/pull/${pr}`;
  20.  
  21. const anchorEl = document.createElement("a");
  22. anchorEl.setAttribute("href", gitHubLink);
  23. anchorEl.setAttribute("style", "background: #f0f0f3; padding: 6px; border-radius: 4px;");
  24. anchorEl.appendChild(document.createTextNode("Open in GitHub"));
  25.  
  26. toolbar.appendChild(anchorEl);
  27. };
  28.  
  29. const toolbarObserver = new MutationObserver((_, observer) => {
  30. const toolbar = document.querySelector(SELECTOR);
  31. if (toolbar) {
  32. observer.disconnect();
  33. addButton(toolbar);
  34. }
  35. });
  36.  
  37. let lastPathname;
  38. const routeChangeObserver = new MutationObserver(() => {
  39. const { pathname } = window.location;
  40.  
  41. if (pathname !== lastPathname) {
  42. lastPathname = pathname;
  43.  
  44. if (pathname.match(PATH_REGEX)) {
  45. toolbarObserver.observe(document.body, {
  46. childList: true,
  47. subtree: true,
  48. });
  49. }
  50. }
  51. });
  52.  
  53. routeChangeObserver.observe(document.body, { childList: true, subtree: true });