GitHub My Issues

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

当前为 2021-01-04 提交的版本,查看 最新版本

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