Source language: Translate to:

Howto: Extract data from iframe?

General questions about NeoAppBuilder - our rapid application development tool for building HTML5, web and mobile apps.

Moderator: Neosoft Support

Howto: Extract data from iframe?

Postby lmsoftware » Sat Jul 18, 2015 2:18 am

Is there any way to extract data from the iframe and put it into a variable?

For example

cnn.com -> iframe -> [html]

I really hope NeoAppBuilder will become a bit more friendly like neobook is, because otherwise some people could never build apps like they did in neobook.
lmsoftware
 
Posts: 9
Joined: Sun Apr 01, 2012 4:45 am

Re: Howto: Extract data from iframe?

Postby Neosoft Support » Sat Jul 18, 2015 12:15 pm

You can try something like this:

BeginJS
$App.VarName = IFrame1.document.body.innerHTML;
EndJS

or another approach without using an iFrame:

BeginJS
$.get("http://www.neoappbuilder.com", function( content ) {
$App.VarName = content;
});
EndJS

Replace VarName above with the name of your variable.

However, this probably won't work with cnn.com or most other website due to browser security restrictions. These types of requests are subject to the same origin policy which means you cannot successfully retrieve data from a different domain, subdomain, port, or protocol.

I really hope NeoAppBuilder will become a bit more friendly like neobook is, because otherwise some people could never build apps like they did in neobook.


Unfortunately, web and mobile apps are much more complicated than desktop apps. Making NeoAppBuilder friendly while also maintaining the ability to do complex things is a challenge. I'm sure having a help file would make things easier too.

Do you find using the property list to edit objects too different from NeoBook's property dialogs?
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5605
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Howto: Extract data from iframe?

Postby lmsoftware » Sat Jul 18, 2015 1:54 pm

Hi Support,

Thanks for you answer.

I really like the brower extract function of Neobook because you can get data of a website ans save it on your system.
But if there is a great help file this would certainly help.

I just tested your code:

BeginJS
$.get("myfile.txt", function( content ) {
$App.VarData = content;
});
EndJS

IsVarEmpty [VarData] [IsItEmpty]

This works, as long as the URL is within the same path as you wrote.

There seems to be some kind of pause. I know how to check if a variable is empty, but how to handle the pause (check if data could get downloaded) before to check if variable is empty?
lmsoftware
 
Posts: 9
Joined: Sun Apr 01, 2012 4:45 am

Re: Howto: Extract data from iframe?

Postby Neosoft Support » Sun Jul 19, 2015 1:31 pm

I really like the browser extract function of Neobook because you can get data of a website ans save it on your system.


This is much simpler under Windows than it is inside a browser due to security restrictions.

There seems to be some kind of pause. I know how to check if a variable is empty, but how to handle the pause (check if data could get downloaded) before to check if variable is empty?


JavaScript won't wait for the get function to return so your IsVarEmpty action is likely executing before the get request is complete. What you can do is put whatever code you want to execute after the get request inside the get function like this:

BeginJS
$.get("myfile.txt", function( content ) {
$App.VarData = content;
EndJS

.put your code here
AlertBox "Results" "[VarData]" ""

BeginJS
});
EndJS
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5605
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Howto: Extract data from iframe?

Postby lmsoftware » Tue Jul 21, 2015 3:38 am

Thanks that works.

I am trying to find a way to check if there is an internet connection.

I have this:

BeginJS
window.onload = checkInternetConnection;
function checkInternetConnection() {
$App.Internet = navigator.onLine;
}
EndJS

If [Internet] == true
SetVar [VarName] "Yes, there is an internet connection."
Else
SetVar [VarName] "No, there is no internet connection."
EndIf

Unfortunately it doesn't work. Var Internet is empty after the check. Someone knows what goes wrong here?
lmsoftware
 
Posts: 9
Joined: Sun Apr 01, 2012 4:45 am

Re: Howto: Extract data from iframe?

Postby Gaev » Tue Jul 21, 2015 5:48 am

Imsoftware:

Var Internet is empty after the check.

1) According to this page ...
Browsers implement this property differently.

If the browser doesn't support navigator.onLine the above example will always come out as false/undefined.

... so, a javascript variable with value of undefined might be translated (by $App.Internet) as "empty"

Also, according to this page, there are issues related to the use of this code in Firefox (desktop) and IE v8.

2) It is never a good idea to name variables with generic names like Internet ... as such names may conflict with reserved system variable names ... try something like xxInternetState or yyConnectionState.

3) Tell us what happens with this code (when you run it in the different Browsers on your computer) ...
Code: Select all
BeginJS
window.onload = checkInternetConnection;
function checkInternetConnection() {
jsConnectionState = navigator.onLine;
alert("jsConnectionState = " + jsConnectionState);
}
EndJS
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Howto: Extract data from iframe?

Postby lmsoftware » Wed Jul 29, 2015 4:02 am

Just tried different codes.

I just don't understand why it is impossible to get some data of a page or file.

For example:

http://www.example.com/textfile.txt

Isn't there anyway to get the data from textfile.txt and store it in a variable even when the app is running on a mobile phone?
lmsoftware
 
Posts: 9
Joined: Sun Apr 01, 2012 4:45 am

Re: Howto: Extract data from iframe?

Postby Gaev » Wed Jul 29, 2015 7:40 am

Imsoftware:

I just don't understand why it is impossible to get some data of a page or file.
For example:
http://www.example.com/textfile.txt

Did you understand Dave's earlier response ? i.e. ...
However, this probably won't work with cnn.com or most other website due to browser security restrictions. These types of requests are subject to the same origin policy which means you cannot successfully retrieve data from a different domain, subdomain, port, or protocol.

... in other words, unless you have a "Web App" that is located/invoked from the same http://www.example.com/ domain, you will be prevented from reading its content.

Note that I have no problem reading the content of a file on my c: drive when I run the App (in Firefox) while Developing on my Windows PC ... but similar file on my website can only be read if the App is also located/invoked from the same domain.

Just tried different codes.

If you need help with something, try and provide as much information about your attempts as possible i.e. post the exact code you tried to run ... and the responses you experienced.
User avatar
Gaev
 
Posts: 3738
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: Howto: Extract data from iframe?

Postby Neosoft Support » Wed Jul 29, 2015 4:08 pm

The security issue at work here is called the "Same-Origin Policy". Info below:

https://www.w3.org/Security/wiki/Same_Origin_Policy

http://security.stackexchange.com/questions/8264/why-is-the-same-origin-policy-so-important

If you own the website where the content is located, you may be able to configure the server to allow "Cross-Origin Resource Sharing" (CORS) which allows sites to grant limited exceptions to same-origin policy. Info below:

http://enable-cors.org/server.html

http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy

We used CORS to get NeoAppBuilder's FormSubmit example to work.
NeoSoft Support
Neosoft Support
NeoSoft Team
 
Posts: 5605
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Re: Howto: Extract data from iframe?

Postby PaulTomo » Fri Jul 31, 2015 9:48 am

lmsoftware

Just tried different codes.

I just don't understand why it is impossible to get some data of a page or file.

For example:

http://www.example.com/textfile.txt

Isn't there anyway to get the data from textfile.txt and store it in a variable even when the app is running on a mobile phone?


Not sure if I'm understanding what you wish to do but I have been able to save text to a variable and then display it all or just a single line of the files text on a mobile phone or via web browser from a text file saved on my own webspace.

Here is an example: you load the whole file in to an array via JS and then extract the line you want, I picked this at random from a file with over 100 lines. But can also display the whole of the file from the variable [TextVar]

Code: Select all
...Get text file and split each line into an array
BeginJS
 var req = new XMLHttpRequest();
  req.open('GET', 'http://www.mydomain.co.uk/MyFile.txt', false);
  req.send(null);
  if (req.readyState==4) //alert(req.responseText);
  $rootScope.TextVar = req.responseText;
  $rootScope.TextArray = $rootScope.TextVar.split("\r\n");
EndJS
...Count lines in text file
ArrayLen [TextArray] [ItemsNum]

...Pick a line at random
RandomEx 1 [ItemsNum] [Pos]
...Display random text in an alert box
ArrayCopy [TextArray] [Pos] 1 [Letter]
AlertBox "Text Saved To Variable" "The Random Text = [Letter]" ""


It may not be what you need but give this a try and see what you think.
User avatar
PaulTomo
 
Posts: 62
Joined: Tue Apr 28, 2009 1:15 am
Location: UK


Return to General NeoAppBuilder Discussion

Who is online

Users browsing this forum: No registered users and 1 guest