-
Notifications
You must be signed in to change notification settings - Fork 0
/
LicenseSelector.js
130 lines (121 loc) · 4.19 KB
/
LicenseSelector.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
codeInput.registerTemplate("syntax-highlighted", codeInput.templates.hljs(hljs, []));
var directoryOutput;
var dirObj = new Object();
var fileExtRegEx = /(?:\.([^.]+))?$/;
var codeBlock = document.getElementById('codeBlock')
var codeBlockText = codeBlock.firstChild;
var preview = document.getElementById("previewArea");
var licRoot = 'https://api.github.com/repos/alexankitty/GB-LicenseEmbeds/contents/Licenses';
var timeout = null;
(async () => {
if(!inIframe()) {
document.body.classList.add("background");
}
try{
let getcommit = await fetch("https://api.github.com/repos/alexankitty/GB-LicenseEmbeds/commits/main")
let commit = await getcommit.json();
let commitCheck = localStorage.getItem("commitSha")
if(commit.sha == commitCheck) {
dirObj = localStorage.getItem("dirObj");
if(!dirObj){
await directoryWalk(licRoot);
localStorage.setItem("dirObj", JSON.stringify(dirObj));
}
else{
dirObj = JSON.parse(dirObj);
console.log("Cache checked passed, loading data.");
}
}
else {
await directoryWalk(licRoot);
localStorage.setItem("dirObj", JSON.stringify(dirObj));
localStorage.setItem("commitSha", commit.sha)
}
}
catch(e){
let error = document.getElementById("error");
error.innerText = `An error has occurred, please try again later. (In an hour) ${e}`
}
await buildSelect();
codeBlockText.addEventListener('keyup', function (e) {
clearTimeout(timeout);
timeout = setTimeout(function () {
preview.innerHTML = codeBlockText.value;
}, 1000);
});
})()
async function directoryWalk(url){
const response = await fetch(url);
const data = await response.json();
directoryOutput = data;
for(let file of data){
if(file.type === "dir"){
await directoryWalk(`${url}/${file.name}`)
}
if(file.type === "file"){
if(fileExtRegEx.exec(file.name)){
let dirPath = file.path.replace("Licenses/", "")
dirObj[dirPath] = file;
}
}
}
}
async function buildSelect(){
selection = document.getElementById("selection")
let array = new Array();
for(let key in dirObj){
array.push(key);
}
array.sort();
for(let i = 0; i < array.length; i++){
let parentFolder = array[i].replace(dirObj[array[i]].name, "")
parentFolder = parentFolder.substring(0, parentFolder.length - 1)
let optGroup = document.getElementById(parentFolder);
if(!optGroup){
optGroup = document.createElement("optgroup");
optGroup.id = parentFolder;
optGroup.label = parentFolder;
selection.appendChild(optGroup)
}
let option = document.createElement("option");
option.value = array[i];
optionName = dirObj[array[i]].name.replace(fileExtRegEx.exec(array[i])[0], "");
option.innerText = optionName;
optGroup.appendChild(option)
}
selection.onchange()
}
async function clipboardCopy(){
navigator.clipboard.writeText(codeBlockText.value);
showSnackBar("HTML copied.");
}
async function onSelect(value){
try{
url = dirObj[value].download_url;
let data = await fetch(url);
let htmlContent = await data.text();
codeBlockText.value = htmlContent;
preview.innerHTML = htmlContent;
codeBlock.firstChild.dispatchEvent(new Event('input'));
}
catch(e){
let error = document.getElementById("error");
error.innerText = `An error has occurred, please try again later. (In an hour) ${e}`
}
}
async function showSnackBar(value){
// Get the snackbar DIV
var x = document.getElementById("snackbar");
x.innerText = value;
// Add the "show" class to DIV
x.className = "show";
// After 3 seconds, remove the show class from DIV
setTimeout(function(){ x.className = x.className.replace("show", ""); }, 3000);
}
function inIframe () {
try {
return window.self !== window.top;
} catch (e) {
return true;
}
}