-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbookmarklet.js
76 lines (63 loc) · 2.67 KB
/
bookmarklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
function bytesToSize(bytes) {
var sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
for (var i = 0; i < sizes.length; i++) {
if (bytes <= 1024) {
return bytes + ' ' + sizes[i];
} else {
bytes = parseFloat(bytes / 1024).toFixed(2);
}
};
return bytes + ' PB';
}
function fetchRepoDetails(url, pathArray, rateLimited, withoutToken) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function () {
var resp = null;
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) resp = JSON.parse(xmlHttp.responseText);
else if (!rateLimited && xmlHttp.status === 403) return fetchRepoDetails(url, pathArray, true, true);
if (!resp) {
if (xmlHttp.status === 403) {
return alert('Rate limit happened you\'re done for the day, you\'ll ' +
' be able to use it normally tomorrow');
}
var githubPage = ['pulls', 'issues', 'marketplace', 'explore', 'settings',
'contact', 'community', 'about', 'features'].indexOf(pathArray[1]) > -1;
if (pathArray.length > 2 && !githubPage) {
var promptMsg = 'Repository not found. If you want this bookmarklet to work for ' +
'private or private organisation repositories click OK or press ENTER key to read the guide.';
if (window.confirm(promptMsg)) {
window.open('https://syncwithtech.org/github-repos-size-creation-date/#privateorganizationrepositories');
}
} else {
alert('Not a valid GitHub repository page');
}
return;
}
var date = new Date(resp.created_at);
var dateArr = date.toString().split(' ').splice(1, 3);
dateArr[1] += ',';
var text = 'Created on: ' + dateArr.join(' ') +
'\nSize: ' + bytesToSize(resp.size * 1024);
alert(text);
}
};
xmlHttp.open('GET', url);
/**
* Uncomment and replace abc with your token in the next line to
* make this bookmarklet work for private or private organization respositories
*
*/
/* xmlHttp.setRequestHeader('Authorization', 'token abc'); */
xmlHttp.send();
}
function evaluate() {
if (window.location.host !== 'github.com') {
alert('Not a valid GitHub page.');
return
}
var pathArray = window.location.pathname.split('/');
var url = 'https://api.github.com/repos' + pathArray.slice(0, 3).join('/');
fetchRepoDetails(url, pathArray);
}
evaluate();