Pregunta

how can i read page source from StageWebView in Flex 4.6?

i cant use HttpRequest to read source because my url link to another Unique url(for everyone) contain session and need to login, so i think i have one way to get page source using StageWebView,but i cant find any method or variable in StageWebView Class.

import mx.events.FlexEvent;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
import mx.rpc.http.HTTPService;

private var webView:StageWebView;
private var httpReq:HTTPService;
private var urlVar:URLVariables;
private var session:String;

protected function creationCompleteHandler(event:FlexEvent):void
{
    httpReq = new HTTPService();
    httpReq.addEventListener(ResultEvent.RESULT, httpResult);
    httpReq.addEventListener(FaultEvent.FAULT, httpFault);
    httpReq.method = "POST";
    httpReq.resultFormat = "text";


    urlVar = new URLVariables();
    urlVar.UserName = "test";
    urlVar.Password = "test";
}

protected function httpResult(e:ResultEvent):void
{
    trace(e.result);
    trace("Done!");
}

protected function httpFault(e:FaultEvent):void
{
    trace("Error!");
}

protected function addedToStageHandler(event:Event):void
{
    webView = new StageWebView();
    webView.stage = this.stage;
    webView.viewPort = new Rectangle(0,0,stage.stageWidth,stage.stageHeight/2);
    webView.loadURL("{URL}");
}

Thank You.

¿Fue útil?

Solución

@Brian thank.

i wrote a tricky code to get html source code from StageWebView.

there is my code:

private var webView:StageWebView;
private var htmlSource:String;

protected function addedToStageHandler(event:Event):void
{
    webView = new StageWebView();
    webView.addEventListener(Event.COMPLETE,onWebViewComplete);
    webView.stage = this.stage;
    webView.viewPort = new Rectangle(0,0,stage.stageWidth,stage.stageHeight/2);
    webView.loadURL("URL HERE");
}

protected function onWebViewComplete(event:Event):void
{
    getHtmlSource();
}

private function getHtmlSource():void
{
    var tempTitle:String = webView.title;
    webView.loadURL("javascript:document.title = document.getElementsByTagName('html')[0].innerHTML;");
    htmlSource = webView.title;
    webView.loadURL("javascript:document.title ='" + tempTitle + "';");
    trace("Title is "+webView.title)
    trace(htmlSource);
}

enjoy! :)

Otros consejos

The communication between Flex and StageWebView is really primitive; you really only get a notification that the browser location is changing.

Look into using the FlexCapacitor Webview. It has a getSource() function you can call (and simplifies management of the StageWebView, too).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top