-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
executable file
·62 lines (57 loc) · 1.56 KB
/
github.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
let textdecoder;
if('TextDecoder' in window) {
textdecoder = new TextDecoder('utf-8');
} else {
alert("This browser isn't supported!");
}
let gh_cache = JSON.parse(window.localStorage?.getItem('gh_cache') || '{}');
if(Object.keys(gh_cache).length == 0) {
// Updates the cache time as soon as a value is fetched
gh_cache.date = new Date().valueOf();
}else{
// Clear the cache after 48 hours
if(new Date().valueOf() - (1000 * 60 * 60 * 48) > gh_cache.date) {
gh_cache = {'date': new Date().valueOf()};
}
}
class GHNode {
constructor(url) {
this.url = url;
return this;
}
async fetch() {
if(this.url in gh_cache){
this.data = gh_cache[this.url];
}else{
let response = await fetch(this.url);
this.data = await response.json();
// Store successful fetch in cache
gh_cache[this.url] = this.data;
window.localStorage?.setItem('gh_cache', JSON.stringify(gh_cache));
}
return this;
}
}
class Tree extends GHNode {
constructor(url) {
super(url);
return this;
}
async get_tree(name) {
let treedata = this.data.tree.find(tree => tree.path == name && tree.type == 'tree');
return await new Tree(treedata.url).fetch();
}
async get_file(name) {
let filedata = this.data.tree.find(tree => tree.path == name && tree.type == 'blob');
return await new File(filedata.url).fetch();
}
}
class File extends GHNode {
constructor(url) {
super(url);
return this;
}
get content() {
return textdecoder.decode(Uint8Array.from(atob(this.data.content), c => c.charCodeAt(0)));
}
}