-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
196 lines (186 loc) · 5.21 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
window.onload = function () {
document.getElementById('js-warning').remove()
document.getElementsByClassName('file-names')[0].style.display = 'none';
var fileUpload = document.getElementById('prj-input-1');
fileUpload.onchange = function () {
try {
new JSZip()
.loadAsync(fileUpload.files[0], {
checkCRC32: true,
createFolders: true,
})
.then(
function (zip) {
zip.file('OBJECT').async('string').then(content => {
// parse the object
var obj = JSON.parse(content);
// fill the info page
document.getElementById('info-title').innerHTML =
obj.title;
document.title = obj.title + " - myprj"
document.getElementById(
'info-description'
).innerHTML = obj.description;
obj.price = `${obj.priceformat}${obj.price[0]} - ${obj.priceformat}${obj.price[1]}`;
document.getElementById('info-price').innerHTML =
obj.price;
document.getElementById('info-date').innerHTML =
obj.date;
},
function (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error getting info',
alertType: 'alert-danger',
})
});
zip.file('INSTRUCTIONS')
.async('string')
.then(
function (markdown) {
markdown = markdown.replace('<', '<');
//removed - doesn't work, dont know why, dont care enough to fix or optimise.
/*if (getCookie("allowProfanity") = "true"){
content = removeProfanity(content)
}*/
var ins =
document.getElementById(
'_instructions'
);
// parse and purify the markdown file, and write it to the page.
ins.innerHTML = DOMPurify.sanitize(
marked(markdown)
);
// make a outline
document.getElementById(
'_sidebar'
).innerHTML = toc(ins);
// fix the images
var _img = document
.getElementById('_instructions')
.querySelectorAll('img');
imageFixer(_img, zip);
},
function (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error getting instructions',
alertType: 'alert-danger',
});
}
);
images(zip);
},
function (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error reading file',
alertType: 'alert-danger',
});
}
);
_switch('images');
} catch (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error!',
alertType: 'alert-danger',
});
}
};
};
/**
* fixes images
* @param {element array} img - an array of the images that you want fixing
* @param {object} zip - a jszip object to source the files from
*/
function metadata(content){
// parse the object
var obj = JSON.parse(content);
// fill the info page
document.getElementById(
'info-title'
).innerHTML = obj.title;
document.getElementById(
'info-description'
).innerHTML = obj.description;
obj.price = `${obj.priceformat}${obj.price[0]} - ${obj.priceformat}${obj.price[1]}`;
document.getElementById(
'info-price'
).innerHTML = obj.price;
document.getElementById(
'info-date'
).innerHTML = obj.date;
}
function imageFixer(img, zip) {
// give the images an id
for (let i = 0; i < img.length; i++) {
const element = img[i];
element.id = i;
}
img.forEach((element) => {
// the src of the images is a full path, so we change that to just the filename
var filename = element.src.substring(element.src.lastIndexOf('/') + 1);
// if you get a file that doesn't exist, its value is null
if (zip.file('images/' + filename) != null) {
// get the file
zip.file('images/' + filename)
.async('base64')
.then(
function (img) {
// render the image
document.getElementById(element.id).src =
'data:image/png;base64,' + img;
},
function (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error getting instructions',
alertType: 'alert-danger',
});
}
);
} else {
// if the image doesn't exist, set it to alt.png
// this is to prevent people from using external sources like imgur ang github
var alt_images = [
'images/alt-green.png',
'images/alt-blue.png',
'images/alt-red.png',
'images/alt-yellow.png',
];
const randIndex = Math.floor(Math.random() * alt_images.length);
element.src = alt_images[randIndex];
}
});
_switch('instructions');
}
/**
* extracts images from a zip file
* @param {jszip object} zip - the zip files you want extracting
*/
function images(zip) {
zip.folder('images').forEach(function (relativePath, file) {
zip.file('images/' + relativePath)
.async('base64')
.then(
function (img) {
// fill the img page
var html =
'<div class="w-400"><div class="card p-0"><img src="data:image/png;base64,' +
img +
'"></img><div class="content"><h2 class="content-title">' +
file.name.replace('images/', '') +
'</h2></div></div></div>';
document.getElementById('_images_grid').innerHTML += html;
},
function (e) {
halfmoon.initStickyAlert({
content: e,
title: 'error getting images',
alertType: 'alert-danger',
});
}
);
});
}