Greasy Fork镜像 支持简体中文。

GitHub First Commit

Add a link to a GitHub repo's first commit

目前為 2018-02-17 提交的版本,檢視 最新版本

  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.2.1
  8. // @license GPL: http://www.gnu.org/copyleft/gpl.html
  9. // @include https://github.com/*/*
  10. // @require https://code.jquery.com/jquery-3.3.1.min.js
  11. // @require https://cdn.rawgit.com/eclecto/jQuery-onMutate/79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  12. // @grant GM_log
  13. // ==/UserScript==
  14.  
  15. // XXX note: the unused grant is a workaround for a Greasemonkey bug:
  16. // https://github.com/greasemonkey/greasemonkey/issues/1614
  17.  
  18. const COMMIT_BAR = 'div.commit-tease.js-details-container > span.float-right'
  19.  
  20. const FIRST_COMMIT =
  21. `<span id="first-commit">
  22. |&nbsp;
  23. <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
  50. const lastPage = link.match(/^.+?<([^>]+)>;/)[1]
  51.  
  52. // fetch the last page of results
  53. return fetch(lastPage).then(res => res.json())
  54. }
  55.  
  56. // if no link, we know we're on the only page
  57. return commits
  58. })
  59.  
  60. // get the last commit and extract the target URL
  61. .then(commits => commits[commits.length - 1].html_url)
  62.  
  63. // navigate there
  64. .then(url => location.href = url)
  65. }
  66.  
  67. // add the "First commit" link as the last child of the commit bar
  68. function addLink ($commitBar) {
  69. const [user, repo] = $('meta[name="octolytics-dimension-repository_network_root_nwo"]')
  70. .attr('content')
  71. .split('/')
  72.  
  73. // the "First commit" link already exists when navigating to a repo's
  74. // homepage via the back button. however, resurrecting the link in this way
  75. // causes its onclick event handler to be unregistered (XXX why?), so we
  76. // still need to re-attach it
  77. let $firstCommit = $commitBar.find('#first-commit')
  78.  
  79. if (!$firstCommit.length) {
  80. $firstCommit = $(FIRST_COMMIT)
  81. $commitBar.append($firstCommit)
  82. }
  83.  
  84. const $link = $firstCommit.find('#first-commit-link')
  85.  
  86. $link.on('click', event => {
  87. $link.text('Loading...')
  88. openFirstCommit(user, repo)
  89. return false
  90. })
  91. }
  92.  
  93. // the commit bar (div.commit-tease) is statically defined in the HTML
  94. // for users who aren't logged in. for logged in users, it's loaded dynamically
  95. // via an <include-fragment> custom element:
  96. //
  97. // https://github.com/github/include-fragment-element
  98. //
  99. // jQuery-onMutate fires the callback immediately if the element already exists,
  100. // so it handles both cases
  101.  
  102. // #js-repo-pjax-container is only created on repo homepages
  103. // see here for more details: https://github.com/Mottie/GitHub-userscripts/wiki/How-to
  104. $('#js-repo-pjax-container').onCreate(COMMIT_BAR, addLink, true /* multi */)

QingJ © 2025

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