GitHub My Issues

Add a link to issues you've contributed to on GitHub

目前為 2020-09-24 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name GitHub My Issues
  3. // @description Add a link to issues you've contributed to on GitHub
  4. // @author chocolateboy
  5. // @copyright chocolateboy
  6. // @version 1.0.0
  7. // @namespace https://github.com/chocolateboy/userscripts
  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.5.1.slim.min.js
  12. // @require https://cdn.jsdelivr.net/gh/eclecto/jQuery-onMutate@79bbb2b8caccabfc9b9ade046fe63f15f593fef6/src/jquery.onmutate.min.js
  13. // @grant GM_log
  14. // @inject-into auto
  15. // ==/UserScript==
  16.  
  17. // XXX note: the unused grant is a workaround for a Greasemonkey bug:
  18. // https://github.com/greasemonkey/greasemonkey/issues/1614
  19.  
  20. // value of the ID attribute for the "My Issues" link. used to identify an
  21. // existing link so it can be removed on pjax page loads
  22. const ID = 'my-issues'
  23.  
  24. // selector for the "Issues" link which we clone the "My Issues" link from and
  25. // append to
  26. const ISSUES = '[aria-label="Global"] a[href="/issues"]'
  27.  
  28. // text for the "My Issues" link
  29. const MY_ISSUES = 'My Issues'
  30.  
  31. // meta tag selector for the `<user>/<repo>` identifier on full pages
  32. const PAGE_REPO = 'octolytics-dimension-repository_nwo'
  33.  
  34. // meta tag selector for the `/<user>/<repo>` identifier on pjax pages
  35. const PJAX_REPO = '[data-pjax="#js-repo-pjax-container"]'
  36.  
  37. // meta tag selector for the name of the logged-in user
  38. const SELF = 'user-login'
  39.  
  40. // meta tag selector for the username on a profile page
  41. const USER = 'profile:username'
  42.  
  43. // helper function which extracts a value from a meta tag
  44. function meta (name, key = 'name') {
  45. const quotedName = JSON.stringify(name)
  46. return $(`meta[${key}=${quotedName}]`).attr('content')
  47. }
  48.  
  49. // handler which adds the "My Issues" link. called either a) on page load (full)
  50. // or b) pjax load (partial)
  51. function main (type) {
  52. const self = meta(SELF)
  53. const $issues = $(ISSUES)
  54.  
  55. // if we're here via a pjax load, there may be an existing "My Issues" link
  56. // from a previous page load: remove it
  57. $(`#${ID}`).remove()
  58.  
  59. // console.log(`XXX page (${type}):`, location.href)
  60.  
  61. if (self && $issues.length === 1) {
  62. let path = '/issues', query = `involves:${self}`, prop
  63.  
  64. if (prop = meta(PAGE_REPO)) { // user/repo
  65. path = `/${prop}/issues`
  66. } else if (prop = $(PJAX_REPO).attr('href')) { // /user/repo
  67. path = `${prop}/issues`
  68. } else if (prop = meta(USER, 'property')) { // user
  69. if (prop === self) { // own homepage
  70. // user:<self> involves:<self> is:open archived:false
  71. query = [`user:${prop}`, query, 'is:open', 'archived:false']
  72. } else { // other user's homepage
  73. // user:<user> involves:<self>
  74. query = [`user:${prop}`, query]
  75. }
  76.  
  77. query = query.join('+')
  78. }
  79.  
  80. const href = `${path}?q=${escape(query)}`
  81. const $link = $issues.clone()
  82. .attr({ href, 'data-hotkey': 'g I', id: ID })
  83. .text(MY_ISSUES)
  84.  
  85. $issues.after($link)
  86. }
  87. }
  88.  
  89. // navigation between pages on GitHub is a mixture of full-page requests and
  90. // partial requests (pjax [1]). we detect the latter by detecting the
  91. // modification of the page's TITLE element.
  92. //
  93. // in the pjax case, we need to take care not to keep adding "My Issues" links.
  94. //
  95. // [1] https://github.com/defunkt/jquery-pjax
  96.  
  97. $('html > head > title').onText(() => main('pjax'), true /* multi */)
  98. main('page')