const exportButton = document.getElementById("export-text"); const imageElement = document.getElementById("background-image"); const checkJSON = document.getElementById("checkJSON"); const checkTEXT = document.getElementById("checkText"); let fileUrl = imageElement.src; let fileName = fileUrl.slice(fileUrl.lastIndexOf("/") + 1, fileUrl.length); let fileTitle = fileName.slice(0, fileName.indexOf(".")) || "export"; console.log(document.getElementById("transcription-panel")); exportButton.addEventListener("click", (e) => { if (checkJSON.value) exportJSON(); if (checkTEXT.value) exportText(); }); function exportText() { let text = document.querySelector("ol").innerText; let title = fileTitle + ".txt"; download(text, title, "text/plain;charset=utf-8"); } function exportJSON() { let title = fileTitle + ".json"; download(JSON.stringify(labelsObj), title, "application/json"); } // https://stackoverflow.com/questions/13405129/javascript-create-and-save-file // Thank you Kanchu! // Function to download data to a file function download(data, filename, type) { var file = new Blob([data], { type: type }); if (window.navigator.msSaveOrOpenBlob) // IE10+ window.navigator.msSaveOrOpenBlob(file, filename); else { // Others var 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); } }