Save variable from Webview

Hallo @Andreas_Hegenberg

I have some trouble to save a string value to BTT variable from textarea in a WebView button, maybe there is a small syntax issue, I have tried a couple of variations (with GPT help), nothing worked so far (see html code) ? Or maybe 'set_string_variable' is broken somehow ...? ( get_string_variable worked fine...)

any hints would be welcome,
Christian

the log shows "success"

<!DOCTYPE html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <title>Editor</title>
  <style>
    html, body {
      margin: 0;
      padding: 0;
      background: #000;
      color: #fff;
      font-family: monospace;
      font-size: 12px;
    }

    #editor {
      width: 1200px;
      height: 700px;
      background: #111;
      color: #fff;
      padding: 10px;
      margin: 20px auto;
      display: block;
      border: none;
      resize: none;
    }

    #saveBtn {
      display: block;
      margin: 10px auto;
      padding: 10px 20px;
      background: #333;
      color: #fff;
      border: 1px solid #666;
      cursor: pointer;
    }

    #saveBtn:hover {
      background: #444;
    }
  </style>
</head>
<body onload="BTTInitialize()">

<textarea id="editor" placeholder="Text eingeben..."></textarea>
<button id="saveBtn" onclick="BTTSave()">Speichern</button>

<script>
  async function BTTInitialize() {
    setTimeout(async () => {
      await loadtext();
    }, 100);
  }

  async function BTTSave() {
    setTimeout(async () => {
      await savetext();
    }, 100);
  }

  async function loadtext() {
    const text = await get_string_variable({ variableName: "FlipText" });
    document.getElementById("editor").value = text || "";
  }

  async function savetext() {
    try {
      const rawText = document.getElementById("editor").value;
      const escapedText = JSON.stringify(rawText);

      console.log("Rohtext:", rawText);
      console.log("JSON-escaped Text:", escapedText);

      // does not work - test with a simple string
      const result = await set_string_variable({ variableName: "FlipText", to: "Test" });
      
      // does not work
      // const result = await set_string_variable({ variableName: "FlipText", to: rawText });
      // does not work
      // const result = await set_string_variable({ variableName: "FlipText", to: escapedText });

      console.log("set_string_variable Erfolg:", result);
      alert("Text saved!");
    } catch (error) {
      console.error("Error during save Variable:", error);
      alert("Error during save");
    }
  }


</script>

</body>
</html>

your code is working fine here:

What is not working for you?

//edit: one thing that is incorrect in your code is this:

<body onload="BTTInitialize()">

Remove the onload, BTTInitialize will be called automatically

I removed the "onload..." and it works now...

 const result = await set_string_variable({ variableName: "FlipText", to: escapedText });

great and thanks a lot,
Christian