Error: Failed evaluating JavaScript with error: JavaScript execution returned a result of an unsupported type

If I run below script, Log shows “Error: Failed evaluating JavaScript with error: JavaScript execution returned a result of an unsupported type”

But if I use promise instead of await, then no error is displayed.
let promise = webview.evaluateJavaScript(getData, false);
promise.then(function(msg) {console.log(msg)});

Could you check and show me how to run the script?

// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: purple; icon-glyph: magic;
const url = ""

const webview = new WebView();

await webview.loadURL(url);

var getData = `
  function downloadFromUrlAutomatically2(url){
    return new Promise((resolve, reject) => {
        var xhr = new XMLHttpRequest();

        xhr.onreadystatechange = function() {
            console.log("XHR status %d", xhr.readyState);
            switch ( xhr.readyState ) {
                case 0:
                    console.log( 'XHR uninitialized!' );
                case 1: // sending data
                    console.log( 'XHR loading...' );
                case 2: // waiting
                    console.log( 'XHR loaded.' );
                case 3: // receiveing data
                    console.log( 'XHR interactive... '+xhr.responseText.length+' bytes.' );
                case 4: // receiving done
                    if( xhr.status == 200 || xhr.status == 304 ) {
                        var data = xhr.responseText; // responseXML もあり
                        console.log( 'XHR COMPLETE! :'+data );
                    } else {
                        console.log("XHR Failed. status %d", xhr.status);
                        console.log( 'XHR Failed. HttpStatus: '+xhr.statusText );
                        var msg = "XHR error ";
                        reject(new Error(msg))
        };'GET', url, true);
        xhr.responseType = 'blob';

        xhr.onload = function(ev) {
          if(xhr.status == 200 || xhr.status == 304){
          } else {
const url = ""


let result = await webview.evaluateJavaScript(getData, false);
// let promise = webview.evaluateJavaScript(getData, false);
// promise.then(function(msg) {console.log(msg)});
type or paste code here

Hey! Welcome!

Just as a heads up, we have a separate category for Scriptable:
Please post there the next time you have a question about the app :wink:

You get the error because you’re trying to return a promise from the injected script, but only strings and numbers can be returned (objects are probably encoded as JSON inbetween). Functions too don’t work.

To fix the issue, pass true as second parameter to evaluateJavaScript() and after the Promise has finished that is returned by downloadFromUrlAutomatically2(), call the function completion() with the value that is carried by the Promise.

Thank you! I will use Scriptable for category next time. I noticed it is already Scriptable now :slight_smile:

About calling completion(), it should be like this?

let promise = webview.evaluateJavaScript(getData, true);
promise.then(function(msg) {completion(msg)});

No no, I meant in the injected script:

const url = ""

//                                 ^^^^^^^^^^^^^^^^