Cgit: copy commit reference

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

安装此脚本
作者推荐脚本

您可能也喜欢GitWeb: copy commit reference

安装此脚本
  1. // ==UserScript==
  2. // @name Cgit: copy commit reference
  3. // @namespace https://andrybak.dev
  4. // @version 5
  5. // @license AGPL-3.0-only
  6. // @author Andrei Rybak
  7. // @description Adds a "Copy commit reference" button to every commit page on Cgit websites.
  8. // @icon https://git.zx2c4.com/cgit/plain/cgit.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://git.kernel.org/pub/scm/*/commit/*
  12. // @match https://git.zx2c4.com/*/commit/*
  13. // @match https://git.savannah.gnu.org/cgit/*/commit/*
  14. // @match https://cgit.freebsd.org/*/commit/*
  15. // @require https://cdn.jsdelivr.net/gh/rybak/userscript-libs@e86c722f2c9cc2a96298c8511028f15c45180185/waitForElement.js
  16. // @require https://cdn.jsdelivr.net/gh/rybak/copy-commit-reference-userscript@d4905c247fedb40932bf07eebf3bf4c838e8d3d7/copy-commit-reference-lib.js
  17. // @grant none
  18. // ==/UserScript==
  19.  
  20. /*
  21. * Copyright (C) 2023-2024 Andrei Rybak
  22. *
  23. * This program is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License as published
  25. * by the Free Software Foundation, version 3.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License
  33. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  34. */
  35.  
  36. (function () {
  37. 'use strict';
  38.  
  39. /*
  40. * Implementation for cgit.
  41. * Documentation:
  42. * - https://git.zx2c4.com/cgit/about/
  43. *
  44. * Example URLs for testing:
  45. * - https://git.kernel.org/pub/scm/git/git.git/commit/?h=main&id=1f0fc1db8599f87520494ca4f0e3c1b6fabdf997
  46. * - https://git.kernel.org/pub/scm/git/git.git/commit/?h=v2.42.0&id=43c8a30d150ecede9709c1f2527c8fba92c65f40
  47. */
  48. class Cgit extends GitHosting {
  49. getTargetSelector() {
  50. return 'body > #cgit > .content > table.commit-info > tbody > tr:nth-child(3) td.sha1, body > #cgit > .content > table.commit-info > tbody > tr:nth-child(3) td.oid';
  51. }
  52.  
  53. wrapButtonContainer(innerContainer) {
  54. const container = document.createElement('span');
  55. container.append(" (", innerContainer, ")");
  56. return container;
  57. }
  58.  
  59. getButtonText() {
  60. // use all lowercase for consistency with the rest of the UI
  61. return "copy commit reference";
  62. }
  63.  
  64. getFullHash() {
  65. const commitAnchor = document.querySelector('body > #cgit > .content > table.commit-info > tbody > tr:nth-child(3) td.sha1 a, body > #cgit > .content > table.commit-info > tbody > tr:nth-child(3) td.oid a');
  66. return commitAnchor.innerText;
  67. }
  68.  
  69. getDateIso(hash) {
  70. const authorDateCell = document.querySelector('body > #cgit > .content > table.commit-info > tbody > tr:nth-child(1) td:nth-child(3)');
  71. return authorDateCell.innerText.slice(0, 'YYYY-MM-DD'.length);
  72. }
  73.  
  74. getCommitMessage(hash) {
  75. const subjElem = document.querySelector('body > #cgit > .content > .commit-subject');
  76. /*
  77. * Sometimes `subjElem` contains a `<span class="decoration">`,
  78. * most notably, on tagged commits. Avoid including the contents
  79. * of this <span> tag by just taking the text of the first node,
  80. * which is a text node.
  81. */
  82. const subj = subjElem.childNodes[0].textContent;
  83. const body = document.querySelector('body > #cgit > .content > .commit-msg').innerText;
  84. /*
  85. * Even though vast majority will only need `subj`, gather everything and
  86. * let downstream code handle paragraph splitting.
  87. */
  88. return subj + '\n\n' + body;
  89. }
  90. }
  91.  
  92. CopyCommitReference.runForGitHostings(new Cgit());
  93. })();

QingJ © 2025

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