GitHub: sort by recently updated

Adds 2 links to sort by "recently updated" (issues & PR)

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

  1. // ==UserScript==
  2. // @name GitHub: sort by recently updated
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.5.3
  5. // @description Adds 2 links to sort by "recently updated" (issues & PR)
  6. // @author Achernar
  7. // @match https://github.com/*
  8. // @run-at document-end
  9. // @grant none
  10. // @noframes
  11. // ==/UserScript==
  12.  
  13. (function() {
  14.  
  15. 'use strict';
  16. var E=document.getElementById("js-repo-pjax-container");
  17. if (!E) { E=document.querySelector('.application-main main'); }
  18. if (!E) return;
  19.  
  20. var TO, obs=new MutationObserver(cb), config = { attributes: false, childList: true, subtree: false};
  21. obs.observe(E, config);
  22.  
  23. function cb(mutL,o) {
  24. for(var mut of mutL) {
  25. if (mut.type == 'childList') {
  26. if (TO) clearTimeout(TO);
  27. TO=setTimeout(addLink,0);
  28. }
  29. }
  30. }
  31.  
  32. function addLink() {
  33. var e=E.querySelector('nav.js-repo-nav, nav'), user;
  34.  
  35. function aLink(e,q,st,st0) {
  36. if (!e) return;
  37. if (e.id) return;
  38. var astyle=((st0!=undefined) && st0) || '', style='', url=e.href || e.parentNode.href, Q=url.indexOf('?')>=0;
  39. url+=(Q?'':'?q=')+(q?'+'+escape(q):'')+(Q?'':'+is%3Aopen')+'+sort%3Aupdated-desc';
  40. if ((url == location.href)) style+=( ((st!=undefined) && st) || 'background-color:#EEEEEE;');
  41. e.innerHTML+='<a style="color:inherit; text-decoration:inherit;'+astyle+'" href="'+url+'"> <span'+(style?' style="'+style+'"':'')+'>(r)</span> </a>';
  42. e.id="addedModifiedLink";
  43. }
  44.  
  45. user=document.head.querySelector(':scope meta[name="user-login"]');
  46. if (e) {
  47. aLink(e.querySelector(':scope span a[data-selected-links~="repo_issues"] span[itemprop="name"], :scope li a[data-selected-links~="repo_issues"] span[itemprop="name"], :scope li a[data-selected-links~="repo_issues"] span[data-content]'),'is:issue');
  48. aLink(e.querySelector(':scope span a[data-selected-links~="repo_pulls"] span[itemprop="name"], :scope li a[data-selected-links~="repo_pulls"] span[itemprop="name"], :scope li a[data-selected-links~="repo_pulls"] span[data-content]'),'is:pr');
  49. let aria=e.attributes['aria-label'], c, RE;
  50. if (aria && ['Issues','Pull Requests'].includes(aria.value) ) {
  51. if (!e.id) {
  52. RE=new RegExp('\\+(author|assignee|mentions)%3A'+user.content);
  53. e.id='addedCommenter';
  54. c=e.firstElementChild.cloneNode(true);
  55. c.innerText='Commenter';
  56. c.title=aria.value+' you commented';
  57. c.attributes['aria-label'].value=aria.value+' you commented';
  58. c.removeAttribute('aria-current');
  59. c.id='commenter';
  60. let u=c.href.replace(RE,'+commenter%3A'+user.content);
  61. if (u.startsWith(location.origin)) u=u.substr(location.origin.length);
  62. c.href=u;
  63. c.dataset.selectedLinks='dashboard_commented '+u;
  64. c.classList.remove('selected');
  65. e.appendChild(c);
  66. setTimeout(addLink,0);
  67. if (aria.value=='Pull Requests') e.innerHTML+='<style>.subnav-search-input-wide {width: 450px;}</style>';
  68. }
  69. else {
  70. let cmt='+commenter%3A'+user.content, sel=e.getElementsByClassName('selected');
  71. RE=new RegExp('\\+commenter%3A'+user.content,'g');
  72. for (let c,i=0; c=e.children[i]; i++) {
  73. if (!c.href) continue;
  74. if ( (c.id=='commenter') && !sel.length) c.classList.add('selected');
  75. let u=c.href.replace(RE, '');
  76. if (u.startsWith(location.origin)) u=u.substr(location.origin.length);
  77. c.href=u+(c.id=='commenter'?cmt:'');
  78. c.dataset.selectedLinks=c.dataset.selectedLinks.replace(RE, '')+(c.id=='commenter'?cmt:'');
  79. }
  80. }
  81. }
  82. }
  83. if (user) {
  84. aLink(document.querySelector('nav[aria-label="Global"] a[href="/pulls"]'), 'is:open+is:pr+author:'+user.content+'+archived:false', ' ','font-size:0.8em;');
  85. aLink(document.querySelector('nav[aria-label="Global"] a[href="/issues"]'), 'is:open+is:issue+author:'+user.content+'+archived:false', ' ','font-size:0.8em;');
  86. }
  87. }
  88.  
  89. addLink();
  90.  
  91. })();