From 16a10fcc7ded6927c351b6a2d5961a5646ad92f8 Mon Sep 17 00:00:00 2001 From: Sean Zhang Date: Tue, 23 Mar 2021 00:04:03 +0800 Subject: [PATCH 1/5] Create github-analyse-forks.user.js --- github-analyse-forks.user.js | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 github-analyse-forks.user.js diff --git a/github-analyse-forks.user.js b/github-analyse-forks.user.js new file mode 100644 index 0000000..77dc912 --- /dev/null +++ b/github-analyse-forks.user.js @@ -0,0 +1,92 @@ +// ==UserScript== +// @name GitHub Analyse Forks +// @version 0.1.0 +// @description A userscript that analyzes GitHub forks, helps you to find out the worthiest fork. +// @license MIT +// @author Sean Zhang +// @namespace https://github.com/zhangsean +// @include https://github.com/*/network/members +// @run-at document-idle +// @grant GM_xmlhttpRequest +// @icon https://github.githubassets.com/pinned-octocat.svg +// @updateURL https://raw.githubusercontent.com/zhangsean/GitHub-userscripts/master/github-analyse-forks.user.js +// @downloadURL https://raw.githubusercontent.com/zhangsean/GitHub-userscripts/master/github-analyse-forks.user.js +// @supportURL https://github.com/zhangsean/GitHub-userscripts/issues +// ==/UserScript== + +(function() { + 'use strict'; + + const $ = (selector, el) => (el || document).querySelector(selector); + const $$ = (selector, el) => [...(el || document).querySelectorAll(selector)]; + + Date.prototype.format=function(t){var e={"M+":this.getMonth()+1,"d+":this.getDate(),"H+":this.getHours(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(var s in/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),e)new RegExp("("+s+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?e[s]:("00"+e[s]).substr((""+e[s]).length)));return t}; + + const sleep=t=>new Promise(f=>setTimeout(f,t)); + + function init() { + const root = $(".network"); + const repos = $$(".network .repo"); + if (root && repos.length > 0) { + console.log(repos.length); + let a = 0; + repos.forEach((el, i)=>{ + let b = ++a; + // Get fork repo info + let repo = $('a:last-child', el).attributes.href.nodeValue; + // Delay 2 minutes every 300 requests to avoid GitHub limits + sleep(b * 10 + (Math.floor(b / 300) * 120000)).then(()=>{ + console.log('r', b, (new Date()).format('yyyy-MM-dd HH:mm:ss')); + GM_xmlhttpRequest({ + url:`https://github.com${repo}`, + onload:function(xhr){ + // Get latest commit time + let html = xhr.responseText, + i = html.indexOf('datetime'); + if (i > -1) { + let dt = html.substring(i + 10, html.indexOf('"', i + 10)); + dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); + el.innerHTML = el.innerHTML + ' ' + dt + ''; + } + // Get fork compare info + i = html.indexOf('This branch is'); + if (i > -1) { + let compare = html.substring(i, html.indexOf('.', i)); + let e = compare.indexOf('even'); + if (e > -1) { + compare = compare.replace('even', 'even'); + } + let a = compare.indexOf('ahead'); + if (a > -1) { + let num = compare.substring(compare.lastIndexOf('is', a) + 3, compare.lastIndexOf('commit', a) - 1); + compare = compare.replace(num, '' + num + ''); + } + let b = compare.indexOf('behind'); + if (b > -1) { + let d = compare.lastIndexOf(',', b); + if (d == -1) { + d = compare.lastIndexOf('is', b) + 1; + } + let num = compare.substring(d + 2, compare.lastIndexOf('commit', b) - 1); + compare = compare.replace(num, '' + num + ''); + } + el.innerHTML = el.innerHTML + ' ' + compare; + } + let e = html.indexOf('Whoa there!'); + if (e > -1) { + console.log('res', b, 'Whoa there!'); + } else { + console.log('res', b); + } + } + }); + }); + }); + } + } + + if (/members/.test(document.location)) { + document.addEventListener("pjax:end", init); + init(); + } +})(); From f4f72b9b26ec6e187dd463a356700350fc4f54d7 Mon Sep 17 00:00:00 2001 From: Sean Zhang Date: Wed, 24 Mar 2021 17:46:55 +0800 Subject: [PATCH 2/5] Fix latest commit time error. --- github-analyse-forks.user.js | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/github-analyse-forks.user.js b/github-analyse-forks.user.js index 77dc912..b67d296 100644 --- a/github-analyse-forks.user.js +++ b/github-analyse-forks.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name GitHub Analyse Forks -// @version 0.1.0 +// @version 0.1.1 // @description A userscript that analyzes GitHub forks, helps you to find out the worthiest fork. // @license MIT // @author Sean Zhang @@ -24,6 +24,8 @@ const sleep=t=>new Promise(f=>setTimeout(f,t)); + const gm=(u,f)=>GM_xmlhttpRequest({url:u,onload:f}); + function init() { const root = $(".network"); const repos = $$(".network .repo"); @@ -35,18 +37,34 @@ // Get fork repo info let repo = $('a:last-child', el).attributes.href.nodeValue; // Delay 2 minutes every 300 requests to avoid GitHub limits - sleep(b * 10 + (Math.floor(b / 300) * 120000)).then(()=>{ - console.log('r', b, (new Date()).format('yyyy-MM-dd HH:mm:ss')); - GM_xmlhttpRequest({ - url:`https://github.com${repo}`, - onload:function(xhr){ + sleep(b + (Math.floor(b / 100) * 120000)).then(()=>{ + //console.log('r', b, (new Date()).format('yyyy-MM-dd HH:mm:ss')); + gm(`https://github.com${repo}`, (xhr)=>{ // Get latest commit time let html = xhr.responseText, - i = html.indexOf('datetime'); + i = html.indexOf('tree-commit'); if (i > -1) { - let dt = html.substring(i + 10, html.indexOf('"', i + 10)); - dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); - el.innerHTML = el.innerHTML + ' ' + dt + ''; + let link = html.substring(html.lastIndexOf('"', i) + 1, html.indexOf('"', i)); + gm(`https://github.com${link}`, (xhr)=>{ + let html = xhr.responseText, + i = html.indexOf('datetime'); + if (i > -1) { + let dt = html.substring(i + 10, html.indexOf('"', i + 10)); + dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); + el.innerHTML = el.innerHTML + ' ' + dt + ''; + } + let e = html.indexOf('Whoa there!'); + if (e > -1) { + console.log('res', b, 'Whoa there!'); + } + }); + } else { + i = html.indexOf('datetime'); + if (i > -1) { + let dt = html.substring(i + 10, html.indexOf('"', i + 10)); + dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); + el.innerHTML = el.innerHTML + ' ' + dt + ''; + } } // Get fork compare info i = html.indexOf('This branch is'); @@ -75,11 +93,9 @@ let e = html.indexOf('Whoa there!'); if (e > -1) { console.log('res', b, 'Whoa there!'); - } else { - console.log('res', b); } } - }); + ); }); }); } From 38ee04eedcdeeac0b9b517ce37ba5cb9627aa8b4 Mon Sep 17 00:00:00 2001 From: Sean Zhang Date: Tue, 30 Mar 2021 15:54:51 +0800 Subject: [PATCH 3/5] Show days between the latest commit time. --- github-analyse-forks.user.js | 165 ++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 78 deletions(-) diff --git a/github-analyse-forks.user.js b/github-analyse-forks.user.js index b67d296..d1bf861 100644 --- a/github-analyse-forks.user.js +++ b/github-analyse-forks.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name GitHub Analyse Forks -// @version 0.1.1 -// @description A userscript that analyzes GitHub forks, helps you to find out the worthiest fork. +// @version 0.2.0 +// @description A userscript that analyzes GitHub forks, shows compare info between fork repos and parent repo, helps you to find out the worthiest fork. // @license MIT // @author Sean Zhang // @namespace https://github.com/zhangsean @@ -22,87 +22,96 @@ Date.prototype.format=function(t){var e={"M+":this.getMonth()+1,"d+":this.getDate(),"H+":this.getHours(),"h+":this.getHours(),"m+":this.getMinutes(),"s+":this.getSeconds(),"q+":Math.floor((this.getMonth()+3)/3),S:this.getMilliseconds()};for(var s in/(y+)/.test(t)&&(t=t.replace(RegExp.$1,(this.getFullYear()+"").substr(4-RegExp.$1.length))),e)new RegExp("("+s+")").test(t)&&(t=t.replace(RegExp.$1,1==RegExp.$1.length?e[s]:("00"+e[s]).substr((""+e[s]).length)));return t}; - const sleep=t=>new Promise(f=>setTimeout(f,t)); + const sleep=(t)=>new Promise(f=>setTimeout(f,t)); - const gm=(u,f)=>GM_xmlhttpRequest({url:u,onload:f}); + const gm=(u,f)=>GM_xmlhttpRequest({url:u,onload:(xhr)=>{let h=xhr.responseText;let e=h.indexOf('Whoa there!');if(e>-1)f('Whoa Github limits!');f(h);}}); + + const show=(e,b,c=0)=>{e.innerHTML+=" "+b+""}; + + let myDt = 0; // Latest commit time of current repo + + function analyseDate (el, ix, html) { + let i = html.indexOf('datetime'); + if (i > -1) { + let dt = html.substring(i + 10, html.indexOf('"', i + 10)); + dt = new Date(dt); + show(el, dt.format('yyyy-MM-dd HH:mm:ss')); + if (ix == 0) { + myDt = dt + } else { + let df = Math.round((dt - myDt) / (1000 * 60 * 60 * 24), 0); + show(el, df == 0 ? 'same day' : ((df > 0 ? 'ahead' : 'behind') + ' ' + Math.abs(df) + ' day' + (Math.abs(df) > 1 ? 's' : '')), df); + } + } + } + + function analyse(el, ix) { + if(!myDt && ix > 0) { + sleep(200).then(()=>analyse(el, ix)); + return; + } + // Get fork repo info + let repo = $('a:last-child', el).attributes.href.nodeValue; + gm(`https://github.com${repo}`, (html)=>{ + // Meet Github limits + let e = html.indexOf('Whoa'); + if (e > -1) { + show(el, html, 1); + return; + } + // Get latest commit time + let i = html.indexOf('tree-commit'); + if (i > -1) { + let link = html.substring(html.lastIndexOf('"', i) + 1, html.indexOf('"', i)); + gm(`https://github.com${link}`, (html)=>{ + analyseDate (el, ix, html); + }); + } else { + analyseDate (el, ix, html); + } + // Get fork compare info + i = html.indexOf('This branch is'); + if (i > -1) { + let compare = html.substring(i, html.indexOf('.', i)); + let e = compare.indexOf('even'); + if (e > -1) { + compare = compare.replace('even', 'even'); + } + let a = compare.indexOf('ahead'); + if (a > -1) { + let num = compare.substring(compare.lastIndexOf('is', a) + 3, compare.lastIndexOf('commit', a) - 1); + compare = compare.replace(num, '' + num + ''); + } + let b = compare.indexOf('behind'); + if (b > -1) { + let d = compare.lastIndexOf(',', b); + if (d == -1) { + d = compare.lastIndexOf('is', b) + 1; + } + let num = compare.substring(d + 2, compare.lastIndexOf('commit', b) - 1); + compare = compare.replace(num, '' + num + ''); + } + el.innerHTML += ' ' + compare; + } + }); + } function init() { - const root = $(".network"); - const repos = $$(".network .repo"); - if (root && repos.length > 0) { - console.log(repos.length); - let a = 0; - repos.forEach((el, i)=>{ - let b = ++a; - // Get fork repo info - let repo = $('a:last-child', el).attributes.href.nodeValue; - // Delay 2 minutes every 300 requests to avoid GitHub limits - sleep(b + (Math.floor(b / 100) * 120000)).then(()=>{ - //console.log('r', b, (new Date()).format('yyyy-MM-dd HH:mm:ss')); - gm(`https://github.com${repo}`, (xhr)=>{ - // Get latest commit time - let html = xhr.responseText, - i = html.indexOf('tree-commit'); - if (i > -1) { - let link = html.substring(html.lastIndexOf('"', i) + 1, html.indexOf('"', i)); - gm(`https://github.com${link}`, (xhr)=>{ - let html = xhr.responseText, - i = html.indexOf('datetime'); - if (i > -1) { - let dt = html.substring(i + 10, html.indexOf('"', i + 10)); - dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); - el.innerHTML = el.innerHTML + ' ' + dt + ''; - } - let e = html.indexOf('Whoa there!'); - if (e > -1) { - console.log('res', b, 'Whoa there!'); - } - }); - } else { - i = html.indexOf('datetime'); - if (i > -1) { - let dt = html.substring(i + 10, html.indexOf('"', i + 10)); - dt = new Date(dt).format('yyyy-MM-dd HH:mm:ss'); - el.innerHTML = el.innerHTML + ' ' + dt + ''; - } - } - // Get fork compare info - i = html.indexOf('This branch is'); - if (i > -1) { - let compare = html.substring(i, html.indexOf('.', i)); - let e = compare.indexOf('even'); - if (e > -1) { - compare = compare.replace('even', 'even'); - } - let a = compare.indexOf('ahead'); - if (a > -1) { - let num = compare.substring(compare.lastIndexOf('is', a) + 3, compare.lastIndexOf('commit', a) - 1); - compare = compare.replace(num, '' + num + ''); - } - let b = compare.indexOf('behind'); - if (b > -1) { - let d = compare.lastIndexOf(',', b); - if (d == -1) { - d = compare.lastIndexOf('is', b) + 1; - } - let num = compare.substring(d + 2, compare.lastIndexOf('commit', b) - 1); - compare = compare.replace(num, '' + num + ''); - } - el.innerHTML = el.innerHTML + ' ' + compare; - } - let e = html.indexOf('Whoa there!'); - if (e > -1) { - console.log('res', b, 'Whoa there!'); - } - } - ); + const root = $(".network"); + const repos = $$(".network .repo"); + if (root && repos.length > 0) { + console.log('Fork repos', repos.length); + + repos.forEach((el, b)=>{ + // Delay 1 minute every 100 requests to avoid Github limits + sleep(b + (Math.floor(b / 100) * 60000)).then(()=>{ + analyse(el, b); }); }); - } - } - if (/members/.test(document.location)) { - document.addEventListener("pjax:end", init); - init(); + } } + + init(); + })(); From 81044198711c35da46c67c353f038132b9770c51 Mon Sep 17 00:00:00 2001 From: Sean Zhang Date: Wed, 31 Mar 2021 11:43:50 +0800 Subject: [PATCH 4/5] Fix error when triggering Github limits. --- github-analyse-forks.user.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/github-analyse-forks.user.js b/github-analyse-forks.user.js index d1bf861..2174779 100644 --- a/github-analyse-forks.user.js +++ b/github-analyse-forks.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name GitHub Analyse Forks -// @version 0.2.0 +// @version 0.2.1 // @description A userscript that analyzes GitHub forks, shows compare info between fork repos and parent repo, helps you to find out the worthiest fork. // @license MIT // @author Sean Zhang @@ -24,7 +24,7 @@ const sleep=(t)=>new Promise(f=>setTimeout(f,t)); - const gm=(u,f)=>GM_xmlhttpRequest({url:u,onload:(xhr)=>{let h=xhr.responseText;let e=h.indexOf('Whoa there!');if(e>-1)f('Whoa Github limits!');f(h);}}); + const gm=(u,f)=>GM_xmlhttpRequest({url:u,onload:(xhr)=>{let h=xhr.responseText,e=h.indexOf('Whoa there!');f(e==-1?h:'Whoa Github limits!');}}); const show=(e,b,c=0)=>{e.innerHTML+=" "+b+""}; @@ -39,7 +39,7 @@ if (ix == 0) { myDt = dt } else { - let df = Math.round((dt - myDt) / (1000 * 60 * 60 * 24), 0); + let df = Math.round((dt - myDt) / 8640000, 0); show(el, df == 0 ? 'same day' : ((df > 0 ? 'ahead' : 'behind') + ' ' + Math.abs(df) + ' day' + (Math.abs(df) > 1 ? 's' : '')), df); } } @@ -102,11 +102,9 @@ if (root && repos.length > 0) { console.log('Fork repos', repos.length); - repos.forEach((el, b)=>{ + repos.forEach((el, i)=>{ // Delay 1 minute every 100 requests to avoid Github limits - sleep(b + (Math.floor(b / 100) * 60000)).then(()=>{ - analyse(el, b); - }); + sleep(i + (Math.floor(i / 100) * 60000)).then(()=>analyse(el, i)); }); } From 749940d4ff1f478619a93a0fde07fa0ee0f889c7 Mon Sep 17 00:00:00 2001 From: Sean Zhang Date: Tue, 6 Apr 2021 22:13:40 +0800 Subject: [PATCH 5/5] Fix compare days bug and num bug. --- github-analyse-forks.user.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/github-analyse-forks.user.js b/github-analyse-forks.user.js index 2174779..e83a0e6 100644 --- a/github-analyse-forks.user.js +++ b/github-analyse-forks.user.js @@ -1,6 +1,6 @@ // ==UserScript== // @name GitHub Analyse Forks -// @version 0.2.1 +// @version 0.2.3 // @description A userscript that analyzes GitHub forks, shows compare info between fork repos and parent repo, helps you to find out the worthiest fork. // @license MIT // @author Sean Zhang @@ -39,7 +39,7 @@ if (ix == 0) { myDt = dt } else { - let df = Math.round((dt - myDt) / 8640000, 0); + let df = Math.round((dt - myDt) / 86400000, 0); show(el, df == 0 ? 'same day' : ((df > 0 ? 'ahead' : 'behind') + ' ' + Math.abs(df) + ' day' + (Math.abs(df) > 1 ? 's' : '')), df); } } @@ -80,7 +80,7 @@ let a = compare.indexOf('ahead'); if (a > -1) { let num = compare.substring(compare.lastIndexOf('is', a) + 3, compare.lastIndexOf('commit', a) - 1); - compare = compare.replace(num, '' + num + ''); + compare = compare.replace(' ' + num + ' ', ' ' + num + ' '); } let b = compare.indexOf('behind'); if (b > -1) { @@ -89,7 +89,7 @@ d = compare.lastIndexOf('is', b) + 1; } let num = compare.substring(d + 2, compare.lastIndexOf('commit', b) - 1); - compare = compare.replace(num, '' + num + ''); + compare = compare.replace(' ' + num + ' ', ' ' + num + ' '); } el.innerHTML += ' ' + compare; }