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