-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
62 lines (58 loc) · 2.44 KB
/
script.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
// Function to create and download a file
function download(data, filename, type) {
const file = new Blob([data], {type: type});
if (window.navigator.msSaveOrOpenBlob) // IE10+
window.navigator.msSaveOrOpenBlob(file, filename);
else { // Other
let a = document.createElement("a"),
url = URL.createObjectURL(file);
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
setTimeout(function() {
document.body.removeChild(a);
window.URL.revokeObjectURL(url);
}, 0);
}
}
// Main function
function commentsStrip(object){
const RE_BLOCKS = new RegExp([
/\/(\*)[^*]*\*+(?:[^*\/][^*]*\*+)*\//.source, // $1: Multiline comment
/\/(\/)[^\n]*$/.source, // $2: One line comment
/"(?:[^"\\]*|\\[\S\s])*"|'(?:[^'\\]*|\\[\S\s])*'|`(?:[^`\\]*|\\[\S\s])*`/.source,
/(?:[$\w\)\]]|\+\+|--)\s*\/(?![*\/])/.source, // Division operator
/\/(?=[^*\/])[^[/\\]*(?:(?:\[(?:\\.|[^\]\\]*)*\]|\\.)[^[/\\]*)*?\/[gim]*/.source
].join('|'),
'gm'
);
// Check for browser support FILE API
if (window.File && window.FileReader && window.Blob) {
let content = ''; // Variable to write the contents of the file
const file = object.files[0]; // the first element of the file array
const reader = new FileReader();
// If the file is loaded successfully, write the content to the variable
reader.onload = function(){
content = reader.result;
// Delete comments
let a = content.replace(RE_BLOCKS, function (match, mlc, slc) {
return mlc ? ' ' : // Multiline comment (space)
slc ? '' : // Single/multi-line
match;
});
// Return the function to download the finished document
return(download(a, file.name, file.type));
}
// As simple text
reader.readAsText(file);
}else{
alert("The required File APIs are not supported by your browser!");
}
}
// Work with page elements
const selector = document.getElementById("imgUpload");
selector.addEventListener("change", () => {
if (selector.files && selector.files[0]) {
commentsStrip(selector);
}})