Need to load outputTextElem text from textarea as a docx document on button click. But the existing method will not work:
const downloadBtn = document.querySelector("#download-btn");
downloadBtn.addEventListener("click", async (e) => {
const outputText = outputTextElem.value;
const outputLanguage =
outputLanguageDropdown.querySelector(".selected").dataset.value;
if (outputText) {
try {
const result = await mammoth.convertText({ arrayBuffer: outputText }, {
ignoreEmptyParagraphs: true
});
const blob = new Blob([result.value], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.download = `translated-to-${outputLanguage}.docx`;
a.href = url;
a.click();
} catch (error) {
console.error("Error converting text to DOCX:", error);
}
}
});
……
<script src="https://cdnjs.cloudflare.com/ajax/libs/mammoth/1.4.2/mammoth.browser.min.js"></script>
An error occurs that the type cannot find mammoth.convertText, although everything is connected in index.html as indicated above:
script.js:216 Error converting text to DOCX: TypeError: mammoth.convertText is not a function
at HTMLButtonElement.<anonymous> (script.js:205:36)
What is the problem?