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 = "https://tokentoken.com/blog/wp-content/uploads/2018/08/cropped-512-round-192x192.png"

const webview = new WebView();

await webview.loadURL(url);

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

        console.log(url);
        xhr.onreadystatechange = function() {
            console.log("XHR status %d", xhr.readyState);
            switch ( xhr.readyState ) {
                case 0:
                    console.log( 'XHR uninitialized!' );
                    break;
                case 1: // sending data
                    console.log( 'XHR loading...' );
                    break;
                case 2: // waiting
                    console.log( 'XHR loaded.' );
                    break;
                case 3: // receiveing data
                    console.log( 'XHR interactive... '+xhr.responseText.length+' bytes.' );
                    break;
                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))
                    }
                    break;
            }
        };

        xhr.open('GET', url, true);
        xhr.responseType = 'blob';

        xhr.onload = function(ev) {
          if(xhr.status == 200 || xhr.status == 304){
              console.log("resolve");
            resolve(xhr.response);
          } else {
          }
        };
        xhr.send();
    });
}
const url = "https://tokentoken.com/blog/wp-content/uploads/2018/08/cropped-512-round-192x192.png"

downloadFromUrlAutomatically2(url);
`;

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

Hey! Welcome!

Just as a heads up, we have a separate category for Scriptable: https://talk.automators.fm/c/scriptable/13
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)});
webview.present();

No no, I meant in the injected script:

const url = "https://tokentoken.com/blog/wp-content/uploads/2018/08/cropped-512-round-192x192.png"

downloadFromUrlAutomatically2(url).then(completion)
//                                 ^^^^^^^^^^^^^^^^