2009年4月7日火曜日

AIRが持つHTMLレンダリングエンジンを遊ぶ



記事を書いた頃のAIRのバージョンは古いみたいでAPIが変わっていた。

それをさらに更新してくれているFlex3でAIRブラウザという記事にたどり着き、

ようやくサンプルを手元で動かすことができました。


しかし、日本語が文字化けするのは何でだろう?

全てが文字化けするわけではないところが微妙~

FlexHTML.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init()"
showStatusBar="false"
paddingBottom="2" paddingLeft="2" paddingRight="2" paddingTop="2">
<mx:Script>
<![CDATA[
private const DEFAULT_HOME:String = "http://www.google.com";

private const THUMBNAIL_WIDTH:uint = 800;
private const THUMBNAIL_HEIGHT:uint = 600;

private function init():void {
var initialBounds:Rectangle
= new Rectangle(
(Screen.mainScreen.bounds.width / 2 - 500),
(Screen.mainScreen.bounds.height / 2 - 400), 1000, 800);
this.nativeWindow.bounds = initialBounds;
htmlPage.addEventListener(Event.LOCATION_CHANGE, onLocationChange);
htmlPage.addEventListener(Event.COMPLETE, onHTMLComplete);
locationInput.text = DEFAULT_HOME;
changeLocation();
}
private function onLocationChange(event:Event):void {
locationInput.text = event.target.location;
}
private function changeLocation():void {
var url:String = locationInput.text;
htmlPage.location = url;
}

private var thumbnailCache:Object;

private function onHTMLComplete(event:Event):void {
thumbnailCache = {};
var links:Object = htmlPage.htmlLoader.window.document.links;
for(var i:uint = 0; i < links.length; ++i){
links[i].addEventListener("mouseover", thumbnailLoadFunc(links[i]));
links[i].addEventListener("mouseout", thumbnailHideFunc(links[i]));
links[i].addEventListener("click", thumbnailHideFunc(links[i]));
}
}
private function thumbnailHideFunc(link:Object):Function {
return function(mouseEvent:Object):void {
var thumbnail:Object = thumbnailCache[link.href];
if(!thumbnail){
return;
}
htmlPage.removeChild(thumbnail);
};
}
private function thumbnailLoadFunc(link:Object):Function {
return function(mouseEvent:Object):void {
var thumbnail:Object = thumbnailCache[link.href];
if(!thumbnail){
thumbnail = new Sprite();
var html:HTMLLoader = new HTMLLoader();
html.load(new URLRequest(link.href));
html.scaleX = .5;
html.scaleY = .5;
html.width = THUMBNAIL_WIDTH;
html.height = THUMBNAIL_HEIGHT;
thumbnail.addChild(html);
thumbnailCache[link.href] = thumbnail;
}
thumbnail.x = mouseEvent.x;
thumbnail.y = mouseEvent.y;
htmlPage.addChild(thumbnail);
};
}
]]>
</mx:Script>
<mx:VBox height="100%" width="100%">
<mx:HBox height="20" width="100%">
<mx:TextInput id="locationInput" width="100%"/>
<mx:Button label="Go"/>
</mx:HBox>
<mx:HTML id="htmlPage" width="100%" height="100%"/>
</mx:VBox>
</mx:WindowedApplication>

0 件のコメント:

コメントを投稿