Welcome, Guest

Author Topic: Accessing HTML Code  (Read 3367 times)

sadashi

  • Posts: 12
    • View Profile
Accessing HTML Code
« on: May 25, 2014, 11:42:15 PM »
Hi everyone,

Is there a way to have access to the retrieved-web pages code in HTML?

We need this functionality in a project since we need to retrieve certain page related info embedded within HTML tags.

Is there a way to access the HTML source of retrieved web pages from within CoherentUI? Probably through WebKit?

Is there a way to modify code of web pages after retrieving them?

Thanks,

Sadashi

Hristo

  • Administrator
  • *****
  • Posts: 577
    • View Profile
Re: Accessing HTML Code
« Reply #1 on: May 26, 2014, 12:10:21 AM »
Hi sadashi,

You can use JavaScript to modify the displayed content of you webpages.

You can send/receive to data from/to JavaScript to the game using Coherent UI using binding. For more information about binding you can check out our binding sample.

sadashi

  • Posts: 12
    • View Profile
Re: Accessing HTML Code
« Reply #2 on: May 26, 2014, 01:14:12 AM »
Hi Hristo,

Thanks for the swift reply.

I have checked the binding example, as you suggested, it uses Java Script to interact with the HTML webpage.

Yet, what we actually needed is raw HTML page access (Accessing the actual HTML code in text format)

Like when we use a regular web browser, we are able to right click on the page and then on the pop-up menu click on "View Page Source"

Is this possible to do in Coherent UI?

Thanks again for the reply,

Sadashi

Nick

  • Administrator
  • *****
  • Posts: 475
    • View Profile
Re: Accessing HTML Code
« Reply #3 on: May 26, 2014, 01:32:45 AM »
The actual HTML source can be obtained with JavaScript as well - for example
Code: [Select]
document.getElementsByTagName('html')[0].outerHTML
If you need that in your C# code then you can use the binding to transfer the actual data.
R&D Director @CoherentLabs

sadashi

  • Posts: 12
    • View Profile
Re: Accessing HTML Code
« Reply #4 on: June 01, 2014, 10:49:04 PM »
Hi,

Thanks for the reply, I understand and it seems to be working!

Yet, what if the page does not have a Javascript method to retrieve the HTML code.

For instance, the user navigates to google.com, or any random website, can we retrieve the HTML/Javascript code of that page? Just like in a browser when we click "View source code".

I believe CoherentUI has this feature somewhere, since it is based on webkit.

If we know where webkit stores caching/history data..etc  may be I can access the HTML source code of different pages by accessing the browsers local files.

Thanks,

Sadashi

Hristo

  • Administrator
  • *****
  • Posts: 577
    • View Profile
Re: Accessing HTML Code
« Reply #5 on: June 01, 2014, 11:38:49 PM »
Hi sadashi,

In such cases you can use "View.ExecuteScript" API to execute the JavaScript code.

sadashi

  • Posts: 12
    • View Profile
Re: Accessing HTML Code
« Reply #6 on: June 02, 2014, 12:09:07 AM »
Hi Hristo,
 
In such cases you can use "View.ExecuteScript" API to execute the JavaScript code.

Would you mind clarifying please? which Javascript do I have to execute to access a webpage's HTML source code?

Thanks and sorry to ask so many questions,

Sadashi

Hristo

  • Administrator
  • *****
  • Posts: 577
    • View Profile
Re: Accessing HTML Code
« Reply #7 on: June 02, 2014, 07:16:53 AM »
You can use "View.ExecuteScript' to execute JavaScript code.

In your case you can use it first to include the coherent.js (converted to string) in your page as a string

and then you can run

view_.View.ExecuteScript("var pageSource = document.getElementsByTagName('html')[0].outerHTML; engine.call('PageSource', pageSource)");

to get the source of the page

Code: [Select]
using UnityEngine;
using System.Collections;

public class SourceGetter : MonoBehaviour {

const string CoherentJs = ""; // include the string generated from online closure compiler here

CoherentUIView view_;
// Use this for initialization
void Start ()
{
view_ = GetComponent<CoherentUIView> ();
view_.Listener.FinishLoad += (frameId, validatedPath, isMainFrame, statusCode, headers) =>
{
if (isMainFrame)
{
view_.View.ExecuteScript(CoherentJs);
}
};

view_.OnReadyForBindings += (frameId, path, isMainFrame) =>
{
if (isMainFrame)
{
view_.View.BindCall("PageSource", (System.Action<string>)OnPageSourceObtained);
}
};
}

void OnPageSourceObtained(string source)
{
Debug.Log (source);
}

void OnGUI()
{
if (GUI.Button (new Rect (10, 70, 50, 30), "Click"))
{
Debug.Log ("Clicked the button with text");
view_.View.ExecuteScript("var pageSource = document.getElementsByTagName('html')[0].outerHTML; engine.call('PageSource', pageSource)");
}
}

}

sadashi

  • Posts: 12
    • View Profile
Re: Accessing HTML Code
« Reply #8 on: June 02, 2014, 07:54:46 PM »
Hi Hristo,

Thanks for the reply. I tried to run the code and took a closer look at the API references and binding example.

I tried to run the code as requested, which should assign the attribute pagesource the actual HTML page source and prints it with a debug statement. It does not seem to work :(

I think I need to initialize CoherentJs with the code generated from online closure compiler.

How can I obtain this code, in a scenario where the online page(UI) is constantly updated online?

or where the page is developed by a third party where I cant generate the the online closure compiler code? Is there a work-around without having to include the closure compiler code ?

Thanks and sorry to drag this topic this long,

Sadashi,



Hristo

  • Administrator
  • *****
  • Posts: 577
    • View Profile
Re: Accessing HTML Code
« Reply #9 on: June 03, 2014, 12:16:57 AM »
 Hi sadashi,

1. Open the coherent.js, select all the code, copy it and paste it http://closure-compiler.appspot.com/home and click compile.

2. Copy the generated string, replace all the " with \"  and paste as a value of CoherentJs variable

const string CoherentJs = ""; // include the string generated from online closure compiler here

3. run the script

This way the script will work and it will get you the html code of the page.
« Last Edit: June 03, 2014, 12:41:38 AM by Hristo »

sadashi

  • Posts: 12
    • View Profile
Re: Accessing HTML Code
« Reply #10 on: June 05, 2014, 06:07:05 PM »
It works! thank you very much for the help,

Have a great day,

Sadashi