Greasy Fork镜像 支持简体中文。

GitHub First Commit

Add a link to a GitHub repo's first commit

目前為 2018-05-15 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub First Commit
  3. // @description Add a link to a GitHub repo's first commit
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @namespace https://github.com/chocolateboy/userscripts
  7. // @version 2.4.0
  8. // @license GPL: http://www.gnu.org/copyleft/gpl.html
  9. // @include https://github.com/
  10. // @include https://github.com/*
  11. // @require https://code.jquery.com/jquery-3.3.1.min.js
  12. // @require https://cdn.rawgit.com/pie6k/jquery.initialize/16342abd3d411a20d35390f3e4c966ceb37ec43e/jquery.initialize.min.js
  13. // @grant GM_log
  14. // ==/UserScript==
  15.  
  16. // XXX note: the unused grant is a workaround for a Greasemonkey bug:
  17. // https://github.com/greasemonkey/greasemonkey/issues/1614
  18.  
  19. const COMMIT_BAR = 'div.commit-tease.js-details-container'
  20.  
  21. const FIRST_COMMIT =
  22. `<span id="first-commit">
  23. &nbsp;|&nbsp;<a id="first-commit-link" style="cursor: pointer" class="message">First commit</a>
  24. </span>`
  25.  
  26. // this function extracts the URL of the repo's first commit and navigates to it.
  27. // it is based on code by several developers, a list of whom can be found here:
  28. // https://github.com/FarhadG/init#contributors
  29. //
  30. // XXX it doesn't work on private repos. a way to do that can be found here,
  31. // but it requires an authentication token:
  32. // https://gist.github.com/simonewebdesign/a70f6c89ffd71e6ba4f7dcf7cc74ccf8
  33. function openFirstCommit (user, repo) {
  34. return fetch(`https://api.github.com/repos/${user}/${repo}/commits`)
  35. // the `Link` header has additional URLs for paging.
  36. // parse the original JSON for the case where no other pages exist
  37. .then(res => Promise.all([res.headers.get('link'), res.json()]))
  38.  
  39. .then(([link, commits]) => {
  40. if (link) {
  41. // the link header contains two URLs and has the following
  42. // format (wrapped for readability):
  43. //
  44. // <https://api.github.com/repositories/1234/commits?page=2>;
  45. // rel="next",
  46. // <https://api.github.com/repositories/1234/commits?page=9>;
  47. // rel="last"
  48.  
  49. // extract the URL of the last page (commits are ordered in
  50. // reverse chronological order, like the CLI, so the oldest
  51. // commit is on the last page)
  52. const lastPage = link.match(/^.+?<([^>]+)>;/)[1]
  53.  
  54. // fetch the last page of results
  55. return fetch(lastPage).then(res => res.json())
  56. }
  57.  
  58. // if there's no link, we know we're on the only page
  59. return commits
  60. })
  61.  
  62. // get the last commit and extract the target URL
  63. .then(commits => commits[commits.length - 1].html_url)
  64.  
  65. // navigate there
  66. .then(url => location.href = url)
  67. }
  68.  
  69. // add the "First commit" link as the last child of the commit bar
  70. function addLink () {
  71. const $commitBar = $(this)
  72. const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]')
  73. .attr('content')
  74. .split('/')
  75.  
  76. // the "First commit" link already exists when navigating to a repo's
  77. // homepage via the back button. however, resurrecting the link in this way
  78. // causes its onclick event handler to be unregistered (XXX why?), so we
  79. // still need to re-attach it
  80. let $firstCommit = $commitBar.find('#first-commit')
  81.  
  82. if (!$firstCommit.length) {
  83. $firstCommit = $(FIRST_COMMIT)
  84. $commitBar.append($firstCommit)
  85. }
  86.  
  87. const $link = $firstCommit.find('#first-commit-link')
  88.  
  89. $link.on('click', event => {
  90. $link.text('Loading...')
  91. openFirstCommit(user, repo)
  92. return false
  93. })
  94. }
  95.  
  96. // the commit bar (div.commit-tease) is statically defined in the HTML
  97. // for users who aren't logged in. for logged-in users, it's loaded dynamically
  98. // via an <include-fragment> custom element:
  99. //
  100. // https://github.com/github/include-fragment-element
  101. //
  102. // jquery.initialize fires the callback immediately if the element already exists,
  103. // so it handles both cases
  104. $.initialize(COMMIT_BAR, addLink)

QingJ © 2025

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