GitWeb: copy commit reference

Adds a "Copy commit reference" button to every commit page on GitWeb websites.

目前為 2023-10-06 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitWeb: copy commit reference
  3. // @namespace https://andrybak.dev
  4. // @version 1
  5. // @license AGPL-3.0-only
  6. // @author Andrei Rybak
  7. // @description Adds a "Copy commit reference" button to every commit page on GitWeb websites.
  8. // @icon https://repo.or.cz/git-favicon.png
  9. // @homepageURL https://github.com/rybak/copy-commit-reference-userscript
  10. // @supportURL https://github.com/rybak/copy-commit-reference-userscript/issues
  11. // @match https://repo.or.cz/*/commit/*
  12. // @match http://localhost:1234/*a=commit*
  13. // @require https://cdn.jsdelivr.net/gh/rybak/userscript-libs@e86c722f2c9cc2a96298c8511028f15c45180185/waitForElement.js
  14. // @require https://cdn.jsdelivr.net/gh/rybak/copy-commit-reference-userscript@c7f2c3b96fd199ceee46de4ba7eb6315659b34e3/copy-commit-reference-lib.js
  15. // @grant none
  16. // ==/UserScript==
  17.  
  18. /*
  19. * Copyright (C) 2023 Andrei Rybak
  20. *
  21. * This program is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License as published
  23. * by the Free Software Foundation, version 3.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License
  31. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  32. */
  33.  
  34. (function () {
  35. 'use strict';
  36.  
  37. /*
  38. * Implementation for GitWeb
  39. * Documentation:
  40. * - https://git-scm.com/docs/gitweb
  41. * - https://git-scm.com/book/en/v2/Git-on-the-Server-GitWeb
  42. *
  43. * Example URL for userscript testing:
  44. * - https://repo.or.cz/alt-git.git/commit/1f0fc1db8599f87520494ca4f0e3c1b6fabdf997
  45. *
  46. * TODO maybe add support for commitdiff pages, e.g.:
  47. * https://repo.or.cz/alt-git.git/commitdiff/1f0fc1db8599f87520494ca4f0e3c1b6fabdf997
  48. */
  49. class GitWeb extends GitHosting {
  50. getTargetSelector() {
  51. if (document.querySelector('.page_nav_sub')) {
  52. return '.page_nav_sub';
  53. }
  54. return '.page_nav';
  55. }
  56.  
  57. wrapButtonContainer(innerContainer) {
  58. const container = document.createElement('span');
  59. container.append(htmlToElement('<span class="barsep"> | </span>'));
  60. const tab = document.createElement('span');
  61. tab.classList.add('tab');
  62. tab.append(innerContainer);
  63. container.append(tab);
  64. return container;
  65. }
  66.  
  67. addButtonContainerToTarget(target, buttonContainer) {
  68. if (target.classList.contains('page_nav_sub')) {
  69. super.addButtonContainerToTarget(target, buttonContainer);
  70. return;
  71. }
  72. target.insertBefore(buttonContainer, target.querySelector('br'));
  73. }
  74.  
  75. getButtonText() {
  76. // use all lowercase for consistency with the rest of the UI
  77. return "copy commit reference";
  78. }
  79.  
  80. getFullHash() {
  81. /*
  82. * <td>commit</td> is always above <td>parent</td> and <td>tree</td>
  83. * so it's fine to just take the first <td> with CSS class `sha1`.
  84. */
  85. const cell = document.querySelector('.title_text .object_header td.sha1');
  86. return cell.innerText;
  87. }
  88.  
  89. getDateIso(hash) {
  90. /*
  91. * <td>author</td> is always above <td>committer</td>
  92. * so it's fine to just take the first <td> with CSS class `sha1`.
  93. */
  94. const cell = document.querySelector('.title_text .object_header .datetime');
  95. const s = cell.innerText;
  96. const d = new Date(s);
  97. return d.toISOString().slice(0, 'YYYY-MM-DD'.length);
  98. }
  99.  
  100. getCommitMessage(hash) {
  101. return document.querySelector('.page_body').innerText;
  102. }
  103. }
  104.  
  105. CopyCommitReference.runForGitHostings(new GitWeb());
  106. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址