JS running in external playground is not working in WeWeb

I have a script working in JSFiddle, but it throws an error in WeWeb.

<body>
  <!-- Insert this script at the bottom of the HTML, but before you use any Firebase services -->
  <script type="module">
    import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.0/firebase-app.js'
    // Add Firebase products that you want to use
    import { getStorage, ref, uploadBytesResumable, getDownloadURL } from 'https://www.gstatic.com/firebasejs/10.12.0/firebase-storage.js';
     // TODO: Replace the following with your app's Firebase project configuration
    const firebaseConfig = {apiKey: "XXXXXX",
  authDomain: "harold-staging-v2.firebaseapp.com",
  projectId: "harold-staging-v2",
  storageBucket: "harold-staging-v2.appspot.com",
    };
    // Initialize Firebase
    const app = initializeApp(firebaseConfig);
const storage = getStorage();
// Create the file metadata
/** @type {any} */
const metadata = {
  contentType: 'image/jpeg'
};
const base64String = 'iVBORw0KGgoAAAANSUhEUgAAADIAAAA0CAMAAAD/uJueAAAAV1BMVEUAAAAQEBDu7u68vLwJCQn6+vobGxv39/f8/Pz4+PhmZmazs7POzs4pKSnn5+ehoaHDw8NycnJAQECHh4eUlJQ2NjZYWFj09PTh4eHX19eqqqpLS0t7e3vPBgSsAAACXUlEQVRIx6WWfRttIAzAlwzzUjgiju//OW/hUOFenrt/PFq/ttpWg/S1wPohRKS/zXMmrAiJrpH6HiLUsukEOcjQAEDVRTcMDVKZCfmqXxDkzIxA3I94RaDO40Vf4I6QNWJF1RcMlu2mznfHKFLbGFTFyTfk1a7V9EPG5DcIScg4BLASNwR5DA7j+Yb1QWybWZAJHEm4w1BZubpuRzp3GKoabeBs7HBUnupzg4Aqx7owUpfl19fMO/LxFZAkLDbCkgSeIrfS/A/SPUXm98hxYvI9Mj1FusuE+atMO1KyZ0TMd6R+iuxpOeRP99LbUoYXRjYz1spjI8bMYBG3Jv8pialLeBHILTLGyuFXkjfVeVY797HrGVDUOlcFlurkvsDhSPVKEJDet5KbE8zkhfcklPMLTugl2vshOAxlQ3E4H9cGOaJiM+h0fm1k86NxIuMitoCwCFI0Gclcp98bpCqzTHzDgM8p4sSczAR0PFFy6jdNLmWzzmNN8TmmMLMXEhehMDvIsrT/rez7CenwvUCUJmcD3loGyS6vpK/Q/eW1RDbHwkgwtjiwxCth57o0yT8Ey/U8V61Mh65VDW8D4xGtJeabYZ9RLCGPRNn4UWK/EktP1/i8vtNzWEnNsL+VYV0ujyLp6pTUtL/IImBim244BbnTa3K6i+jDgm0iBnFhs9ddGOFfb82c177lthi8HsbmdyRbF4q9n1YKDDqlpbWJeKMurjRW5YVwWiLw+iESdZe3FVstmMeyavOO69TroSBsPzCNdMknKeVU1KOO6NSowUUrtb36mCHRfdf3Rv4AT8EqV9CZODMAAAAASUVORK5CYII=';
// Upload file and metadata to the object 'images/mountains.jpg'
const storageRef = ref(storage, 'images/testing.jpg'); // Provide a filename here
const uploadTask = uploadBytesResumable(storageRef, base64String, metadata);
// Listen for state changes, errors, and completion of the upload.
uploadTask.on('state_changed',
  (snapshot) => {
    // Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
    const progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
    console.log('Upload is ' + progress + '% done');
    switch (snapshot.state) {
      case 'paused':
        console.log('Upload is paused');
        break;
      case 'running':
        console.log('Upload is running');
        break;
    }
  },
  (error) => {
    // A full list of error codes is available at
    // https://firebase.google.com/docs/storage/web/handle-errors
    switch (error.code) {
      case 'storage/unauthorized':
        // User doesn't have permission to access the object
        break;
      case 'storage/canceled':
        // User canceled the upload
        break;
      // ...
      case 'storage/unknown':
        // Unknown error occurred, inspect error.serverResponse
        break;
    }
  },
  () => {
    // Upload completed successfully, now we can get the download URL
    getDownloadURL(uploadTask.snapshot.ref).then((downloadURL) => {
      console.log('File available at', downloadURL);
    });
  }
);
  </script>
</body>

What do I change to make it work in WeWeb?

It says Unexpected token ‘<’

And if I remove the body and script tags, it say Cannot use import statement outside a module

I’m not sure if you wanna expose your API keys in the front-end. My best guess is the module - import aspect of the code. Try looking for an NPM package, or a CDN.

use the js code in a js action from a workflow and replace the import statements with dynamic imports

@Broberto yes you’re right. We need to hide it. Will look at hiding the api keys.

Thanks @dorilama. It’s working. But I’m not able to return the uploaded URL for further actions.

I tried many different ways [all shown in the screenshot], but none worked.

Even the console.log(‘Completed’) is not visible, but console.log(‘File available at’, downloadURL); works fine.

to use data in other steps you need to return it from the top level. js actions are actually the body of an async function, if there is some async work you need to await it and then return the data.

Any pointers on how to do that? Chatgpt hasn’t been of much help.