Context:
My overall goal is to import a bunch of multiple-choice questions and the corresponding answers from a website into a Google Form. Since I'll need to do this more often I thought I could write a script to make the process more efficient. So I wrote a short JS script for execution in the website's console. It generates and returns a code made for Google Apps Script which then creates a form with all of the questions.
Now I need to automatically translate the form's content (during GAS code generation). I want to use DeepL and since it has no way of translating a website, I decided to use DeepL API in my JS code. Unfortunately, I'm still pretty new to JavaScript - and that's most likely the actual problem... (Now I'm asking myself if my approach has any positive effect on the efficiency I mentioned before... 🤔)
The issue:
When executing my JS code, I only get the following error message:
Uncaught
SyntaxError: Cannot use import statement outside a module
The error appears in the first line of my code:
import * as deepl from "deepl-node";
The project directory contains a downloaded copy of the website as an HTML file (so that I can use the API). I execute JS code in that HTML file (I open it in Chrome from localhost). And of course, I have also installed deepl-node (and node.js).
Now I need to know how to fix this. Is it impossible to import the API and use it locally?
From my code:
import * as deepl from "deepl-node";const DEEPL_API_AUTH_KEY = "<my API key> ";const translator = new deepl.Translator(DEEPL_API_AUTH_KEY);const DEFAULT_POINTS = 2;const qAndA = [];let questionId = 0;for (const e of document.querySelectorAll("h3")) { qAndA.push(["\n\n// QUESTION "+ questionId +"\nquestion"+ questionId +" = form.addMultipleChoiceItem();\nquestion"+ questionId +'.setTitle("'+ ( await translator.translateText(e.innerHTML, "en", "de", { splitSentences: "nonewlines", preserveFormatting: true, }) ).text +'");\nquestion'+ questionId +".setChoices([", ]); // Creates sub-array for each question, content: question title // and so on... for (let i = 0; i < qAndA.length; i++) { qAndA[i] = qAndA[i].join(""); } const gasCode = qAndA.join(""); console.log(gasCode);
<!-- Basic structure of the website: --><ol><li><h3>First question???</h3><ul><li>Option A</li><li>Option B</li><li>Option C</li></ul></li><li><h3>Second question???</h3><ul><li>Option A</li><li>Option B</li><li>Option C</li><li>Option D</li></ul></li><!-- and so on... --></ol>
(I know it hurts to read this, but I thought I could just do it quick and dirty and never need to open the code again but that didn't turn out that well...)
Thank you so much in advance!