GitWeb: copy commit reference

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

当前为 2023-10-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitWeb: copy commit reference
  3. // @namespace https://andrybak.dev
  4. // @version 2
  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@49d1a667c213261b504fc52524536d0b5d0c10e0/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 barSeparator = document.createElement('span');
  59. barSeparator.append(document.createTextNode(' | '));
  60. /*
  61. * CSS class is from 09deab1 (gitweb: enclose ' · ' and ' | ' in span tags, 2015-04-12)
  62. * https://repo.or.cz/git/gitweb.git/commit/09deab16f1feac32142bd6db4cd15294a26915a5
  63. */
  64. barSeparator.classList.add('barsep');
  65. const container = document.createElement('span');
  66. container.append(barSeparator);
  67. const tab = document.createElement('span');
  68. tab.classList.add('tab');
  69. tab.append(innerContainer);
  70. container.append(tab);
  71. return container;
  72. }
  73.  
  74. addButtonContainerToTarget(target, buttonContainer) {
  75. if (target.classList.contains('page_nav_sub')) {
  76. super.addButtonContainerToTarget(target, buttonContainer);
  77. return;
  78. }
  79. target.insertBefore(buttonContainer, target.querySelector('br'));
  80. }
  81.  
  82. getButtonText() {
  83. // use all lowercase for consistency with the rest of the UI
  84. return "copy commit reference";
  85. }
  86.  
  87. getFullHash() {
  88. /*
  89. * <td>commit</td> is always above <td>parent</td> and <td>tree</td>
  90. * so it's fine to just take the first <td> with CSS class `sha1`.
  91. */
  92. const cell = document.querySelector('.title_text .object_header td.sha1');
  93. return cell.innerText;
  94. }
  95.  
  96. getDateIso(hash) {
  97. /*
  98. * <td>author</td> is always above <td>committer</td>
  99. * so it's fine to just take the first <td> with CSS class `sha1`.
  100. */
  101. const cell = document.querySelector('.title_text .object_header .datetime');
  102. const s = cell.innerText;
  103. const d = new Date(s);
  104. return d.toISOString().slice(0, 'YYYY-MM-DD'.length);
  105. }
  106.  
  107. getCommitMessage(hash) {
  108. return document.querySelector('.page_body').innerText;
  109. }
  110. }
  111.  
  112. CopyCommitReference.runForGitHostings(new GitWeb());
  113. })();

QingJ © 2025

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