JSFiddle - React, Tailwind, and code Playground

by thelifeisyours

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.0.0/jszip.js"></script>
<input id="kmlFile" type="file" accept=".km*"/>
<button id="stripDate">Strip Date From KLM Name</button>

JavaScript

const kmlFile = document.querySelector("#kmlFile");
const stripButton = document.querySelector("#stripDate");


stripButton.addEventListener('click', () => {
	let zip = new JSZip();
  
  readFileAsync(kmlFile.files[0]).then((file) => {
    zip.loadAsync(file)
      .then(function(unziped) {
      	readFileAsync(unziped.files[0])
  			.then((file) => stripDate(file));
      });
  });
});

function stripDate(kmlData) {
  kmlData.replace(/(?!<name>\d\d\d\d-\d\d-\d\d) \d\d:\d\d:\d\d(?=<\/name>)/gm, '');
  
  let download = document.createElement('a');
  download.href = `data:text/plain;charset=utf-8,${encodeURIComponent(kmlData)}`;
  download.download = kmlFile.files[0].name;
  download.click();
}

function readFileAsync(file) {
  return new Promise((resolve, reject) => {
    let reader = new FileReader();

    reader.onload = (e) => {
      resolve(e.target.result);
    };

    reader.onerror = reject;

    reader.readAsBinaryString(file);
  })
}