GitHub: sort by recently updated

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

当前为 2020-05-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name GitHub: sort by recently updated
  3. // @namespace https://github.com/Procyon-b
  4. // @version 0.5.2
  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. //console.log('A child node has been added or removed.',mut);
  27. if (TO) clearTimeout(TO);
  28. TO=setTimeout(addLink,0);
  29. }
  30. }
  31. }
  32.  
  33. function addLink() {
  34. var e=E.querySelector('nav'), user;
  35.  
  36. function aLink(e,q,st,st0) {
  37. if (!e) return;
  38. if (e.id) return;
  39. var astyle=((st0!=undefined) && st0) || '', style='', url=e.href || e.parentNode.href, Q=url.indexOf('?')>=0;
  40. url+=(Q?'':'?q=')+(q?'+'+escape(q):'')+(Q?'':'+is%3Aopen')+'+sort%3Aupdated-desc';
  41. if ((url == location.href)) style+=( ((st!=undefined) && st) || 'background-color:#EEEEEE;');
  42. e.innerHTML+='<a style="color:inherit; text-decoration:inherit;'+astyle+'" href="'+url+'"> <span'+(style?' style="'+style+'"':'')+'>(r)</span> </a>';
  43. e.id="addedModifiedLink";
  44. }
  45.  
  46. user=document.head.querySelector(':scope meta[name="user-login"]');
  47. if (e) {
  48. 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"]'),'is:issue');
  49. 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"]'),'is:pr');
  50. let aria=e.attributes['aria-label'], c, RE;
  51. if (aria && ['Issues','Pull Requests'].includes(aria.value) ) {
  52. if (!e.id) {
  53. RE=new RegExp('\\+(author|assignee|mentions)%3A'+user.content);
  54. e.id='addedCommenter';
  55. c=e.firstElementChild.cloneNode(true);
  56. c.innerText='Commenter';
  57. c.title=aria.value+' you commented';
  58. c.attributes['aria-label'].value=aria.value+' you commented';
  59. c.removeAttribute('aria-current');
  60. c.id='commenter';
  61. let u=c.href.replace(RE,'+commenter%3A'+user.content);
  62. if (u.startsWith(location.origin)) u=u.substr(location.origin.length);
  63. c.href=u;
  64. c.dataset.selectedLinks='dashboard_commented '+u;
  65. c.classList.remove('selected');
  66. e.appendChild(c);
  67. setTimeout(addLink,0);
  68. if (aria.value=='Pull Requests') e.innerHTML+='<style>.subnav-search-input-wide {width: 450px;}</style>';
  69. }
  70. else {
  71. let cmt='+commenter%3A'+user.content, sel=e.getElementsByClassName('selected');
  72. RE=new RegExp('\\+commenter%3A'+user.content,'g');
  73. for (let c,i=0; c=e.children[i]; i++) {
  74. if (!c.href) continue;
  75. if ( (c.id=='commenter') && !sel.length) c.classList.add('selected');
  76. let u=c.href.replace(RE, '');
  77. if (u.startsWith(location.origin)) u=u.substr(location.origin.length);
  78. c.href=u+(c.id=='commenter'?cmt:'');
  79. c.dataset.selectedLinks=c.dataset.selectedLinks.replace(RE, '')+(c.id=='commenter'?cmt:'');
  80. }
  81. }
  82. }
  83. }
  84. if (user) {
  85. aLink(document.querySelector('nav[aria-label="Global"] a[href="/pulls"]'), 'is:open+is:pr+author:'+user.content+'+archived:false', ' ','font-size:0.8em;');
  86. aLink(document.querySelector('nav[aria-label="Global"] a[href="/issues"]'), 'is:open+is:issue+author:'+user.content+'+archived:false', ' ','font-size:0.8em;');
  87. }
  88. }
  89.  
  90. addLink();
  91.  
  92. })();