forked from BludIsAnLemon/lemons-gallery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.js
115 lines (93 loc) · 3.43 KB
/
builder.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
(async function() {
function copyText(str) {
navigator.clipboard.writeText(str);
}
const defaultList = [
{
"title": "Hello World",
"img": null,
"description": "An extension example",
"url": 'extensions/hello-world.js',
"creator": [
{"name": "GarboMuffin", "url": "https://scratch.mit.edu/users/GarboMuffin/"}
]
}
];
async function getList() {
try {
return await fetch('extensions.json')
.then(response => response.json())
.then(response => {
return response
})
} catch (uselessError) {
throw new Error('Failed to fetch!');
return defaultList;
}
};
let extList = await getList();
const extensions = document.getElementById('extensions');
extList.forEach(currentValue => {
const extension = document.createElement('div');
const title = document.createElement('h2');
title.textContent = currentValue["title"];
const img = document.createElement('img');
if(!currentValue["img"] || currentValue["img"] == null) {
img.src = "./images/unknown-banner.png"
} else {
img.src = currentValue["img"];
}
const description = document.createElement('p');
description.classList.add('desc');
description.textContent = currentValue["description"]
extension.appendChild(img);
extension.appendChild(title);
extension.appendChild(description);
const creators = currentValue["creator"];
const creatorText = document.createElement('p');
creatorText.textContent = `Creator(s):`
extension.appendChild(creatorText);
creators.forEach(currentValue => {
const creator = document.createElement('a');
creator.textContent = `${currentValue["name"]} `;
creator.href = currentValue["url"] ? currentValue["url"] : '#' ;
extension.appendChild(creator);
extension.appendChild(document.createElement('br'));
extension.appendChild(document.createElement('br'));
})
extension.classList.add('extension');
const btnHolder = document.createElement('span');
btnHolder.classList.add('extension-buttons');
const copyURLBtn = document.createElement('button');
const copyCodeBtn = document.createElement('button');
copyURLBtn.classList.add('extension-button');
copyURLBtn.dataset.copy = 'url';
copyCodeBtn.classList.add('extension-button');
copyCodeBtn.dataset.copy = 'code';
copyURLBtn.textContent = 'Copy URL';
copyCodeBtn.textContent = 'Copy Code';
btnHolder.appendChild(copyURLBtn);
btnHolder.appendChild(copyCodeBtn);
const lineBreak = document.createElement('br');
copyURLBtn.addEventListener('click', function() {
copyText(`${window.location.href}/${currentValue['url']}`);
})
async function getCode() {
try {
return await fetch(currentValue['url'])
.then(response => response.text())
.then(response => {
return response;
})
} catch (uselessError) {
return 'Failed to fetch code!'
}
}
copyCodeBtn.addEventListener('click', async function() {
const code = await getCode()
copyText(code);
})
extension.appendChild(lineBreak);
extension.appendChild(btnHolder);
extensions.appendChild(extension);
});})()