handle JS errors (#6)
This commit is contained in:
		
							parent
							
								
									b2e4f1bc64
								
							
						
					
					
						commit
						a577f55387
					
				
					 2 changed files with 28 additions and 14 deletions
				
			
		|  | @ -22,19 +22,23 @@ | ||||||
|     <h1> |     <h1> | ||||||
|       <a href="https://gpx.thermokar.st">gpx.thermokar.st</a> |       <a href="https://gpx.thermokar.st">gpx.thermokar.st</a> | ||||||
|     </h1> |     </h1> | ||||||
|     <span> |     <p> | ||||||
|       This client-side tool is for merging |       This client-side tool is for merging | ||||||
|       <a href="https://www.topografix.com/gpx.asp">GPX files</a>. |       <a href="https://www.topografix.com/gpx.asp">GPX files</a>. | ||||||
|       Please note, this has only been tested on GPX files produced by |       Please note, this has only been tested on GPX files produced by | ||||||
|       <a href="https://www.garmin.com">Garmin</a> and |       <a href="https://www.garmin.com">Garmin</a> and | ||||||
|       <a href="https://www.strava.com">Strava</a> - your mileage may vary. |       <a href="https://www.strava.com">Strava</a> - your mileage may vary. | ||||||
|       Source: |     </p> | ||||||
|  |     <form> | ||||||
|  |       <input id="gpxInput" type="file" multiple accept="text/gpx,.gpx"> | ||||||
|  |     </form> | ||||||
|  |     <hr> | ||||||
|  |     <p> | ||||||
|  |       <small> | ||||||
|          <a href="https://github.com/thermokarst/gpx-web-utils"> |          <a href="https://github.com/thermokarst/gpx-web-utils"> | ||||||
|           https://github.com/thermokarst/gpx-web-utils</a> |           https://github.com/thermokarst/gpx-web-utils</a> | ||||||
|     </span> |       </small> | ||||||
|     <form> |     </p> | ||||||
|       <input id="gpxInput" type="file" multiple> |  | ||||||
|     </form> |  | ||||||
|     <script src="./bootstrap.js"></script> |     <script src="./bootstrap.js"></script> | ||||||
|   </body> |   </body> | ||||||
| </html> | </html> | ||||||
|  |  | ||||||
							
								
								
									
										20
									
								
								www/index.js
									
										
									
									
									
								
							
							
						
						
									
										20
									
								
								www/index.js
									
										
									
									
									
								
							|  | @ -1,27 +1,37 @@ | ||||||
| import * as gpx from "gpx-web-utils"; | import * as gpx from "gpx-web-utils"; | ||||||
| 
 | 
 | ||||||
| const inputElement = document.getElementById("gpxInput"); | const inputElement = document.getElementById("gpxInput"); | ||||||
|  | const loadingElement = document.createElement("span"); | ||||||
| 
 | 
 | ||||||
|  | loadingElement.innerHTML = "<strong>processing...</strong>"; | ||||||
|  | inputElement.value = ""; | ||||||
| inputElement.addEventListener("change", readFiles, false); | inputElement.addEventListener("change", readFiles, false); | ||||||
| 
 | 
 | ||||||
| function readFiles() { | function readFiles() { | ||||||
|   if (inputElement.files.length < 2) { alert("open two or more files"); return; } |   if (inputElement.files.length < 2) { alert("open two or more files"); return; } | ||||||
| 
 | 
 | ||||||
|  |   inputElement.replaceWith(loadingElement); | ||||||
|   const files = Array.from(inputElement.files); |   const files = Array.from(inputElement.files); | ||||||
|   const promises = files.map(f => f.text()); |   const promises = files.map(f => f.text()); | ||||||
| 
 | 
 | ||||||
|   Promise.all(promises).then(gpxes => { |   Promise.all(promises).then(gpxes => { | ||||||
|  |     try { | ||||||
|       const merged = gpx.merge(gpxes); |       const merged = gpx.merge(gpxes); | ||||||
|       writeOutput(merged); |       writeOutput(merged); | ||||||
|  |     } catch { | ||||||
|  |       alert("there was a problem, please check the console."); | ||||||
|  |     } finally { | ||||||
|       inputElement.value = ""; |       inputElement.value = ""; | ||||||
|  |       loadingElement.replaceWith(inputElement); | ||||||
|  |     } | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function writeOutput(file) { | function writeOutput(file) { | ||||||
|     const blob = new Blob([file], {type: "text/gpx"}); |     const blob = new Blob([file], {type: "text/gpx"}); | ||||||
|     const a = document.createElement("a"); |     const anchorElement = document.createElement("a"); | ||||||
|     a.href = URL.createObjectURL(blob); |     anchorElement.href = URL.createObjectURL(blob); | ||||||
|     a.download = "merged.gpx"; |     anchorElement.download = "merged.gpx"; | ||||||
|     a.click(); |     anchorElement.click(); | ||||||
|     URL.revokeObjectURL(a.href); |     URL.revokeObjectURL(anchorElement.href); | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 GitHub
							GitHub