How do you get text from webviewer?

How does the property Webviewer.WebViewString work?
Taifun

thanks
it looks easy to send normal text, but in my case how can i setwebviewstring = to my 3 text file
(i’m not expert of html)

Give your object tags an id each
( e.g. < object id = "mytext1" data = "file1.txt" > )
then in a javascript script get the contents of each object from the id
(e.g. var txt1 = document.getelementById("mytext1").innerHTML)
push each of these into an array
(e.g. (var output = []; output. push(txt1); )
then assign “output” to the webviewstring
(e.g. window.AppInventor.setWebViewString(output); )

The script needs to run after the page has loaded so put it at the bottom of your html, below the body:
< /body>
< script>
script contents;
< /script>
< /html>

You then get this back in the app as a list

Probably much easier to just use the file component to open the text files ? (assuming they are in the assets)

Thanks TimAI2
i’m playing with your tips and it’s surely what i need.
But i faced the first problem when i found that
document.getElementById(“mytext1”).innerHTML
returns an empty string even if i can see the contents of < object id = “mytext1”… in the browser

Probably not “innerHTML”…

and not sure if you can return the content of an <object>

Where are your text files ?

on my server

By trying and retrying I have seen that I can do

< p id=“container” >
< object data=“file1.txt” > < / object >
< object data=“file2.txt” > < / object >
< / p >
< / body >

< script >
var txt = document.getElementById(“container”).innerHTML;
document.write(txt);
< / script >

And this seems to be good, anyway I need to work more on this as I see in chrome debugger that the variable txt contains html code "<object data="file1…
and not the text files, but strangely document.write(txt) gives the text files. I will know after my tests if this affects the webviewerstring or not.
I will let you know.

Good find!

What you may need to do is load the objects with the text files but have them hidden:

style=“display:none;”

then set them to variables and display the file contents in another element - then grab it

???

I have a bit of time now, will have a play…

nice! i put the style in < p and it worked

what should i enter in the HTML of the server to use
window.AppInventor.setWebViewString(“hello from Javascript”)
? any .js file?

You didn’t say, but my guess is you are calling your html and files from a server (not locally on the app - it will not work!)

This being the case, something like this will work for you. You have to let the objects load before doing anything (hence the button) but you can use onload events to automate

<!DOCTYPE html>
<html>
<meta name=“viewport” content=“width=device-width, initial-scale=1.0”>

<head>
	<title>Object Grab</title>
	
</head>

<body>
	<object id ="file1" data = "file1.txt" ></object><br>
	<object id ="file2" data = "file2.txt" ></object><br>
	<object id ="file3" data = "file3.txt" ></object><br>
	<button onclick="getData()">Get Data</button>
</body>

<script>

function getData() {
var wvs1 = document.getElementById('file1').contentDocument.body.childNodes[0].innerText;
var wvs2 = document.getElementById('file2').contentDocument.body.childNodes[0].innerText;
var wvs3 = document.getElementById('file3').contentDocument.body.childNodes[0].innerText;
window.AppInventor.setWebViewString("[[" + wvs1 + "],[" + wvs2 + "],[" + wvs3+ "]]");
}

</script>

</html>

You can play around with the webviewstring output to your liking :wink:

The order and geometry of your project tell me the experience and the passion you put in what you do :slight_smile: compliments!
Yes in this story the device must get some data from the server, sorry i was not clear, the html must run on the server.
I have so much to learn that’s why I’m sure I’m missing something important.
In fact if I load this html file in my browser, chrome, i get an error in getData function: Uncaught TypeError: Cannot read property ‘body’ on null on the first line …body.childNodes…
and also TypeError: Cannot read property ‘setWebViewString’ of undefined
Please help.

Try removing the above:
document.getElementById('file1').contentDocument.body.innerText;

Of course this will only run in a webviewer in the app. If you want to test in another browser, replace the webviewstring line with an alert:

//window.AppInventor.setWebViewString("[[" + wvs1 + "],[" + wvs2 + "],[" + wvs3+ "]]");
alert("[[" + wvs1 + "],[" + wvs2 + "],[" + wvs3+ "]]");

unfortunately it doesn’t work :frowning:
but before going on struggling with this issue i need to know if there is any way to use webviewstring from my AI2 App, pointing to the html file on a server; is jquery.1.8.0.min.js compatible with a browser for pc?

Yes, you can use online files in the webviewer with a webviewstring.

jQuery should work, I never use jQuery - anywhere.

Surprised you can’t get it to work.

  • You have the text files in the same directory as the html file ?
  • They are named exactly the same as in the html?
  • You have the object “id’s” set correctly in the html and the script ?
  • You have this: window.AppInventor.setWebViewString() correct ?
  • Did you test in your computer browser with alert() ?
  • What is the content like in your text files? Could be something there…

I did all of that, also for me it looks strange. Also i don’t have specific competences with html so i will do it in winsock style as i already did for other routines because any single thing keeps me blocked for hours googling.
Sorry so much for disturbing all, thanks again!!!

Thanks for the help

my question is very similar to this topic, I plan to use a screen for a webviewer to open a website (that i have control over). When some operations on the website by the app user is done, the site will output sth saying "successful". I need to pass such text to the app inventor app. From the discussion in this topic, it sounds that i should be able to do it, but the topic creater appeared to have discontinued on this path. Im interested in giving it a try, but never used webviewstring.

From puravidaapps.com:

It seems to be straightforward, will give it a try. Thanks.