-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCcodesList.html
89 lines (77 loc) · 2.28 KB
/
GCcodesList.html
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
<html><head>
<title>Convert GPX to list of GC codes with semicolon or space separator</title>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.9.1/jszip.min.js"></script>
<script>
// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
// Great success! All the File APIs are supported.
} else {
alert('The File APIs are not fully supported in this browser.');
}
var output = [];
var error = [];
var spaceSeparator;
// Closure to capture the file information.
function handleZipFile(f) {
getZipFilesContent(f).then(dd => {
processXML(dd);
}).catch(e => {
console.log(e);
});
}
async function getZipFilesContent(data) {
const zipContent = [];
const promises = [];
const zip = (await JSZip.loadAsync(data));
zip.forEach(async (relativePath, file) => {
const promise = file.async('string');
promises.push(promise);
zipContent.push({
file: relativePath,
content: await promise
});
});
await Promise.all(promises);
return zipContent[0].content;
}
function processXML(dd) {
var re = /<name>(GC[A-Z0-9]*)<\/name>/g, match;
while (match = re.exec(dd)) {
output.push(match[1]);
}
updateView(output);
}
function updateView() {
var sep = ";";
if (spaceSeparator) {
sep = " ";
}
document.getElementById('list').innerHTML = output.join(sep);
document.getElementById('error').innerHTML = '<ul>' + error.join('') + '</ul>';
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
spaceSeparator = document.getElementById('space').checked;
if (files[0].name.endsWith(".zip")) {
handleZipFile(files[0]);
} else {
var reader = new FileReader();
reader.onload = function(evt) {
processXML(this.result);
};
reader.readAsText(files[0]);
}
}
$(document).ready(function(){
document.getElementById('files').addEventListener('change', handleFileSelect, false);
});
</script>
</head>
<body>
<h3>Convert GPX to list of GC codes with semicolon or space separator</h3> <br/>
<input type="file" id="files" name="files[]"> Space separator?: <input id="space" type="checkbox"> <br>
<textarea id="list" rows="20" cols="80"></textarea>
<div id="error"></div>
</body></html>