GitWeb: copy commit reference

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

安裝腳本?
作者推薦腳本

您可能也會喜歡 Sourcehut: copy commit reference

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

QingJ © 2025

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