GitHub My Issues

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

当前为 2021-03-08 提交的版本,查看 最新版本

  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.1.2
  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://cdn.jsdelivr.net/npm/cash-dom@8.1.0/dist/cash.min.js
  12. // @grant GM_log
  13. // ==/UserScript==
  14.  
  15. /*
  16. * value of the ID attribute for the "My Issues" link. used to identify an
  17. * existing link so it can be removed on pjax page loads
  18. */
  19. const ID = 'my-issues'
  20.  
  21. /*
  22. * selector for the "Issues" link which we clone the "My Issues" link from and
  23. * append to
  24. */
  25. const ISSUES = '[aria-label="Global"] a[href="/issues"]'
  26.  
  27. /*
  28. * text for the "My Issues" link
  29. */
  30. const MY_ISSUES = 'My Issues'
  31.  
  32. /*
  33. * meta-tag selector for the `<user>/<repo>` identifier on full pages
  34. */
  35. const PAGE_REPO = 'octolytics-dimension-repository_nwo'
  36.  
  37. /*
  38. * selector for the `/<user>/<repo>` identifier on pjax pages
  39. */
  40. const PJAX_REPO = '[data-pjax="#js-repo-pjax-container"]'
  41.  
  42. /*
  43. * meta-tag selector for the name of the logged-in user
  44. */
  45. const SELF = 'user-login'
  46.  
  47. /*
  48. * meta-tag selector for the username on a profile page
  49. */
  50. const USER = 'profile:username'
  51.  
  52. /*
  53. * helper function which extracts a value from a meta tag
  54. */
  55. function meta (name, key = 'name') {
  56. const quotedName = JSON.stringify(name)
  57. return $(`meta[${key}=${quotedName}]`).attr('content')
  58. }
  59.  
  60. /*
  61. * add the "My Issues" link
  62. */
  63. function run () {
  64. const self = meta(SELF)
  65. const $issues = $(ISSUES)
  66.  
  67. // if we're here via a pjax load, there may be an existing "My Issues" link
  68. // from a previous page load. we can't reuse it as the event handlers may no
  69. // longer work, so we just replace it
  70. $(`#${ID}`).remove()
  71.  
  72. if (!self || $issues.length !== 1) {
  73. return
  74. }
  75.  
  76. let prop, query = `involves:${self}`, path = '/issues'
  77.  
  78. if (prop = meta(PAGE_REPO)) { // user/repo
  79. path = `/${prop}/issues`
  80. } else if (prop = $(PJAX_REPO).attr('href')) { // /user/repo
  81. path = `${prop}/issues`
  82. } else if (prop = meta(USER, 'property')) { // user
  83. let queries
  84.  
  85. if (prop === self) { // own homepage
  86. // user:<self> involves:<self> is:open archived:false
  87. queries = [`user:${prop}`, query, 'is:open', 'archived:false']
  88. } else { // other user's homepage
  89. // user:<user> involves:<self>
  90. queries = [`user:${prop}`, query]
  91. }
  92.  
  93. query = queries.join('+')
  94. }
  95.  
  96. const href = `${path}?q=${escape(query)}`
  97. const $link = $issues.clone()
  98. .attr({ href, 'data-hotkey': 'g I', id: ID })
  99. .text(MY_ISSUES)
  100.  
  101. $issues.after($link)
  102. }
  103.  
  104. $(document).on('pjax:end', run) // run on pjax page loads
  105. $(run) // run on full page loads