-
Notifications
You must be signed in to change notification settings - Fork 3
/
builder.js
159 lines (128 loc) · 4.8 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
(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 isSandboxed = Boolean(currentValue["sandboxed"]);
const description = document.createElement('p');
description.classList.add('desc');
description.textContent = currentValue["description"]
extension.appendChild(img);
extension.appendChild(title);
extension.appendChild(description);
if(currentValue["docsURL"]) {
const docsURL = document.createElement('a');
docsURL.textContent = 'Documentation';
docsURL.href = currentValue["docsURL"];
docsURL.classList.add('docsBtn');
extension.appendChild(docsURL);
}
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');
const downloadBtn = document.createElement('button');
copyURLBtn.classList.add('extension-button');
copyURLBtn.dataset.copy = 'url';
copyCodeBtn.classList.add('extension-button');
copyCodeBtn.dataset.copy = 'code';
downloadBtn.classList.add('extension-button');
downloadBtn.dataset.copy = 'download';
copyURLBtn.textContent = 'Copy URL';
copyCodeBtn.textContent = 'Copy Code';
downloadBtn.textContent = 'Download';
btnHolder.appendChild(copyURLBtn);
btnHolder.appendChild(copyCodeBtn);
btnHolder.appendChild(downloadBtn);
if(isSandboxed) {
const tryBtn = document.createElement('button');
tryBtn.classList.add('extension-button');
tryBtn.dataset.copy = 'try';
tryBtn.textContent = 'Try it out!';
btnHolder.appendChild(tryBtn);
tryBtn.addEventListener('click', () => {
window.location.href = `https://turbowarp.org/editor?extension=${window.location.href}/${currentValue['url']}`;
})
}
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);
})
downloadBtn.addEventListener('click', async function() {
const link = document.createElement('a');
link.href = currentValue['url'];
link.download = `${(currentValue["title"]. toLowerCase()).replace(/[^a-zA-Z0-9]/g, '-')}.js`;
document.body.appendChild(link)
try {
link.click();
} catch (error) {
throw new Error('Failed to download file!');
};
link.remove();
});
extension.appendChild(lineBreak);
extension.appendChild(btnHolder);
extensions.appendChild(extension);
});})()