<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Jozef Chúťka&#039;s blog &#187; JavaScript</title>
	<atom:link href="http://blog.yoz.sk/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.yoz.sk</link>
	<description>My life, my work</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:40:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Authorizing Facebook Applications in AIR</title>
		<link>http://blog.yoz.sk/2010/09/authorizing-facebook-applications-in-air/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=authorizing-facebook-applications-in-air</link>
		<comments>http://blog.yoz.sk/2010/09/authorizing-facebook-applications-in-air/#comments</comments>
		<pubDate>Tue, 21 Sep 2010 12:43:53 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[AIR]]></category>
		<category><![CDATA[Facebook]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[HTMLLoader]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[NativeWindow]]></category>
		<category><![CDATA[NativeWindowInitOptions]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=2338</guid>
		<description><![CDATA[I was asked to make AIR compatibile version of FacebookOAuthGraph class. It was a nice little challenge for me, where I learn some new things about AIR. E.g. how easy it is to define the JavaScript callback directly from ActionScipt (HTMLLoader.window.methodName)&#8230; There are more ways to make authorization process work for you, I have chosen [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/airfacebook.jpg" alt="" title="airfacebook" width="200" height="100" class="alignleft size-full wp-image-2281" /></p>
<p>I was asked to make AIR compatibile version of <a href="http://blog.yoz.sk/facebookoauthgraph/">FacebookOAuthGraph</a> class. It was a nice little challenge for me, where I learn some new things <a href="http://livedocs.adobe.com/flex/3/html/help.html?content=ProgrammingHTMLAndJavaScript_06.html">about AIR</a>. E.g. how easy it is to define the JavaScript callback directly from ActionScipt (HTMLLoader.window.methodName)&#8230; There are more ways to make authorization process work for you, I have chosen the one with popup window. In order to make FacebookOAuthGraph work for AIR, the short extending is required, instead of creating popup window from JavaScript, popup is created by HTMLLoader.createRootWindow (no ExternalInterface required)&#8230; </p>
<p><span id="more-2338"></span></p>
<p>Lets start with the main AIR Application file:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;s:WindowedApplication xmlns:fx=&quot;http://ns.adobe.com/mxml/2009&quot;
                       xmlns:s=&quot;library://ns.adobe.com/flex/spark&quot;
                       xmlns:mx=&quot;library://ns.adobe.com/flex/mx&quot;
                       applicationComplete=&quot;init()&quot;&gt;
    &lt;s:layout&gt;
        &lt;s:VerticalLayout /&gt;
    &lt;/s:layout&gt;

    ... here comes the same content as in:
    &quot;http://blog.yoz.sk/2010/05/facebook-graph-api-and-oauth-2-and-flash/&quot;
    ...
    just change redirectURI to:
    &quot;http://blog.yoz.sk/examples/FacebookOAuthGraph/aircallback.html&quot;
    ...

&lt;/s:WindowedApplication&gt;</pre>
<p>Do not bother with callback.html (you do not need any, if you do, create an empty .html file) and make your Facebook class look something like this:</p>
<pre class="brush: as3; title: ; notranslate">package
{
    import flash.display.NativeWindowInitOptions;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.html.HTMLLoader;
    import flash.net.URLRequest;

    public class Facebook extends FacebookOAuthGraph
    {
        public var loader:HTMLLoader;

        public function Facebook()
        {
            super();
        }

        override public function connect():void
        {
            var options:NativeWindowInitOptions = new NativeWindowInitOptions();
            var boundaries:Rectangle = new Rectangle(200,200,500,500);
            loader = HTMLLoader.createRootWindow(true, options, true, boundaries);
            loader.load(new URLRequest(authorizationURL));
            loader.addEventListener(Event.LOCATION_CHANGE, onLocationChange);
        }

        private function onLocationChange(event:Event):void
        {
            var hash:String = locationExtractHash(loader.location);
            var error:String = locationExtractError(loader.location);
            if(hash)
                confirmConnection(hash);
            if(hash || error)
                destroyLoader();
        }

        protected function locationExtractHash(url:String):String
        {
            var index:int = url.indexOf(&quot;#&quot;);
            if(index &lt;= -1)
                return null;
            var hash:String = url.substr(index + 1);
            return hashToToken(hash) ? hash : null;
        }

        protected function locationExtractError(url:String):String
        {
            var index:int = url.indexOf(&quot;?&quot;);
            if(index &lt;= -1)
                return null;
            var query:String = url.substr(index + 1);
            var variables:URLVariables = new URLVariables(query);
            return variables.hasOwnProperty(&quot;error&quot;) ? variables.error : null;
        }

        private function destroyLoader():void
        {
            loader.removeEventListener(Event.LOCATION_CHANGE, onLocationChange);
            loader.removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
            loader.stage.nativeWindow.close();
            loader = null;
        }
    }
}</pre>
<p>How does this works? You do not need any callback.html, just generate some redirect_uri that correctly targets your domain, it can be anything, even 404.html, or asdf-foo-bar.html file that does not exist. Once the Facebook, redirects you back to the redirect_uri, the url contains access_token within url hash</p>
<pre class="brush: plain; title: ; notranslate">http://domain/whatever-or-404.html#access_token=123</pre>
<p>&#8230; now, if it does, it is parsed by locationExtractHash(), if it does not, the url contains error (user clicked Cancel button), the url contains error:</p>
<pre class="brush: plain; title: ; notranslate">http://domain/whatever-or-404.html?error_reason=user_denied&amp;error=access_denied&amp;error_description=The+user+denied+your+request.</pre>
<p>In case of error locationExtractError() parses and error. In both cases (error or access_token), the HTMLLoader window is closed and token is handled via confirmConnection(). Yeah, this way you can authorize <strong>!ANY!</strong> existing facebook application for your AIR app.</p>
<h2>Depricated version using JavaScript in callback</h2>
<p>Keep the changes in main AIR Application file (see above), and make small changes in aircallback.html, in fact it got even simplier comparing to the original:</p>
<pre class="brush: xml; title: ; notranslate">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;sk&quot; lang=&quot;sk&quot; dir=&quot;ltr&quot;&gt;
&lt;head&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	&lt;!--
		confirmFacebookConnection(window.location.hash);
	//--&gt;
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;p&gt;You may now close this window.&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>&#8230; finally some method changes in FacebookOAuthGraph class:</p>
<pre class="brush: as3; title: ; notranslate">package
{
    import flash.display.NativeWindowInitOptions;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.html.HTMLLoader;
    import flash.net.URLRequest;

    import sk.yoz.net.FacebookOAuthGraph;

    public class Facebook extends FacebookOAuthGraph
    {
        public var loader:HTMLLoader;

        public function Facebook()
        {
            super();
        }

        override public function connect():void
        {
            var options:NativeWindowInitOptions = new NativeWindowInitOptions();
            var boundaries:Rectangle = new Rectangle(200,200,500,500);
            loader = HTMLLoader.createRootWindow(true, options, true, boundaries);
            loader.load(new URLRequest(authorizationURL));
            loader.addEventListener(Event.HTML_DOM_INITIALIZE, onDOMInit);
        }

        private function onDOMInit(event:Event):void
        {
            loader.window[jsConfirm] = confirmConnection;
        }

        override public function confirmConnection(hash:String):void
        {
            super.confirmConnection(hash);
            loader.stage.nativeWindow.close();
        }
    }
}</pre>
<p>What it does is, it pushes JavaScript callback into HTMLLoader that is called via aircallback.html JavaScript&#8230;</p>
<p>Where to go from here:</p>
<ul>
<li><a href="http://livedocs.adobe.com/flex/3/html/help.html?content=ProgrammingHTMLAndJavaScript_06.html">Making ActionScript objects available to JavaScript</a></li>
<li><a href="http://www.akbarsait.com/blog/index.cfm/2009/6/1/Creating-Windows-in-HTML-and-JavaScript-AIR-Applications">Creating Windows in HTML and JavaScript AIR Applications</a></li>
<li><a href="http://stackoverflow.com/questions/573472/how-to-access-an-iframe-in-an-adobe-air-application">How to access an iframe in an Adobe AIR application?</a></li>
<li><a href="http://blog.swfjunkie.com/2010/08/tweetr-tutorials-pinless-oauth-in-air/">Tweetr Tutorials: Pinless OAuth in AIR</a></li>
<li><a href="http://blog.flash-core.com/?p=214">AIR and authentification on Facebook</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/09/authorizing-facebook-applications-in-air/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
		<item>
		<title>Quick tip: ExternalInterface.call is synchronous!</title>
		<link>http://blog.yoz.sk/2010/02/quick-tip-externalinterface-call-is-synchronous/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=quick-tip-externalinterface-call-is-synchronous</link>
		<comments>http://blog.yoz.sk/2010/02/quick-tip-externalinterface-call-is-synchronous/#comments</comments>
		<pubDate>Fri, 12 Feb 2010 16:21:01 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ExternalInterface]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1067</guid>
		<description><![CDATA[It is a little wonder to me how flash player communicates with JavaScript. Calls are fully synchronous, it works as those (fp and js) were fully integrated into each other. To simulate this lets test some simple flow: Following flex application runs simple JavaScript: and results are logged into TextArea: This means that ExternalInterface.call() is [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/synchronous.jpg" alt="" title="synchronous" width="200" height="100" class="alignleft size-full wp-image-1081" /></p>
<p>It is a little wonder to me how flash player communicates with JavaScript. Calls are fully synchronous, it works as those (fp and js) were fully integrated into each other. To simulate this lets test some simple flow:</p>
<p><span id="more-1067"></span></p>
<div style="clear:both;"></div>
<pre class="brush: plain; title: ; notranslate">Flash Player calls JavaScript
    JavaScript calls FlashPlayer
        FlashPlayer do some stuff and finishes
    JavaScript finishes
FlashPlayer continues</pre>
<p>Following flex application runs simple JavaScript:</p>
<pre class="brush: jscript; title: ; notranslate">document.getElementById(&quot;flash&quot;).universalCallback();
return &quot;synchronous return&quot;;</pre>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
    applicationComplete=&quot;init()&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    [Bindable]
    private var lastResult:String = &quot;&quot;;

    private function init():void
    {
        ExternalInterface.addCallback(&quot;universalCallback&quot;, universalCallback);
    }

    private function runScript():void
    {
        var script:String = 'document.getElementById(&quot;flash&quot;).universalCallback();' +
                'return &quot;synchronous return&quot;;';

        var result:String = ExternalInterface.call(&quot;function(){&quot; + script + &quot;}&quot;);
        lastResult += result + &quot;\n&quot;;
    }

    private function universalCallback(... rest):void
    {
        lastResult += &quot;universalCallback&quot; + &quot;\n&quot;;
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:Button click=&quot;runScript()&quot; label=&quot;Run&quot;/&gt;
&lt;mx:TextArea width=&quot;100%&quot; height=&quot;100%&quot; text=&quot;{lastResult}&quot; /&gt;
&lt;/mx:Application&gt;</pre>
<p>and results are logged into TextArea:</p>
<pre class="brush: plain; title: ; notranslate">universalCallback
synchronous return</pre>
<p>This means that ExternalInterface.call() is synchronous and waits until called JavaScript finishes. Just after that, flash player moves on the next line. This interesting behaviour makes flash player to hold (but not to freeze) on one line while it is able to continue on different execution (universalCallback()). </p>
<p>Notice, JavaScript to ActionScript data can be sent via ExternalInterface.addCallback() or simple by synchronous return:</p>
<pre class="brush: as3; title: ; notranslate">var result:String = ExternalInterface.call(&quot;function(){&quot; + script + &quot;}&quot;);</pre>
<p>however, you can not use eval method this way!</p>
<pre class="brush: as3; title: ; notranslate">var result:String = ExternalInterface.call(&quot;eval&quot;, script);</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/02/quick-tip-externalinterface-call-is-synchronous/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Full JavaScript Access From ActionScript (update)</title>
		<link>http://blog.yoz.sk/2010/02/full-javascript-access-from-actionscript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=full-javascript-access-from-actionscript</link>
		<comments>http://blog.yoz.sk/2010/02/full-javascript-access-from-actionscript/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 10:10:55 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[allowscriptaccess]]></category>
		<category><![CDATA[eval]]></category>
		<category><![CDATA[ExternalInterface]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=1005</guid>
		<description><![CDATA[Have you ever tought about accessing DOM from ActionScript? In fact, you can do it and even far more. You can create and call JavaScript methods and objects, access cookies, change styles&#8230; All you need is correct AllowScriptAccess parameter within your flash object. No framework needed here, no hacks, ExternalInterface takes care. Following application runs [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://blog.yoz.sk/wp-content/uploads/fullJavascriptAccess.png" alt="" title="fullJavascriptAccess" width="200" height="100" class="alignleft size-full wp-image-1047" /></p>
<p>Have you ever tought about accessing <a href="http://cs.wikipedia.org/wiki/Document_Object_Model">DOM</a> from ActionScript? In fact, you can do it and even far more. You can create and call JavaScript methods and objects, access cookies, change styles&#8230; All you need is correct <a href="http://kb2.adobe.com/cps/164/tn_16494.html">AllowScriptAccess</a> parameter within your flash object. No framework needed here, no hacks, ExternalInterface takes care.</p>
<p><span id="more-1005"></span></p>
<p style="clear:both;">Following application runs JavaScript from textarea:</p>
<p><iframe width="100%" height="300" style="border:none;" src="http://blog.yoz.sk/examples/fullJavascriptAccess/"></iframe></p>
<p>Application. Line 16 does all the fun <img src='http://blog.yoz.sk/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
    applicationComplete=&quot;init()&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    [Bindable]
    private var lastResult:String = &quot;&quot;;

    private function init():void
    {
        ExternalInterface.addCallback(&quot;universalCallback&quot;, universalCallback);
    }

    private function runScript():void
    {
        ExternalInterface.call(&quot;eval&quot;, script.text);
    }

    private function universalCallback(... rest):void
    {
        lastResult = String(rest[0]);
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:HBox width=&quot;100%&quot; height=&quot;100%&quot;&gt;
    &lt;mx:TextArea id=&quot;script&quot; width=&quot;100%&quot; height=&quot;100%&quot;&gt;
        &lt;mx:text&gt;
            &lt;![CDATA[
function myFunction(){
    var flash = document.getElementById(&quot;flash&quot;);
    var data = document.childNodes[1].innerHTML;
    flash.universalCallback(data);
}

myFunction();
alert(document.getElementsByTagName('title')[0].innerHTML);
            ]]&gt;
        &lt;/mx:text&gt;
    &lt;/mx:TextArea&gt;
    &lt;mx:TextArea width=&quot;100%&quot; height=&quot;100%&quot; text=&quot;{lastResult}&quot; /&gt;
&lt;/mx:HBox&gt;

&lt;mx:Button click=&quot;runScript()&quot; label=&quot;Run&quot;/&gt;
&lt;/mx:Application&gt;</pre>
<p>HTML template</p>
<pre class="brush: xml; title: ; notranslate">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;en&quot; lang=&quot;en&quot; dir=&quot;ltr&quot;&gt;
&lt;head&gt;
    &lt;title&gt;Full Javascript Access&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;application/xml; charset=utf-8&quot; /&gt;
    &lt;meta http-equiv=&quot;Cache-Control&quot; content=&quot;no-cache&quot; /&gt;
    &lt;meta http-equiv=&quot;expires&quot; content=&quot;1&quot; /&gt;
    &lt;meta http-equiv=&quot;pragma&quot; content=&quot;no-cache&quot; /&gt; 

    &lt;meta name=&quot;author&quot; content=&quot;http://studio.yoz.sk&quot; /&gt;
    &lt;meta name=&quot;description&quot; content=&quot;Full Javascript Access&quot; /&gt;
    &lt;meta name=&quot;robots&quot; content=&quot;all&quot; /&gt; 

    &lt;script type=&quot;text/javascript&quot; src=&quot;js/swfobject.js&quot;&gt;&lt;/script&gt;
    &lt;script type=&quot;text/javascript&quot;&gt;
    &lt;!--
        var flashvars = {}

        var params = {
            allowscriptaccess: &quot;always&quot;
        };
        var attributes = {
            id: &quot;flash&quot;,
            name: &quot;flash&quot;
        };
        swfobject.embedSWF(&quot;Tests.swf&quot;, &quot;alternative&quot;, &quot;100%&quot;, &quot;100%&quot;, &quot;10.0.0&quot;,
            &quot;flash/expressInstall.swf&quot;, flashvars, params, attributes);
    //--&gt;
    &lt;/script&gt;

    &lt;style type='text/css'&gt;
    &lt;!--
        body {margin:0px;overflow:hidden;}
        html, body, object, embed {width:100%;height:100%;outline:none;}
    --&gt;
    &lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;div id=&quot;alternative&quot;&gt;
        &lt;a href=&quot;http://www.adobe.com/go/getflashplayer&quot;&gt;
            &lt;img src=&quot;http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif&quot; alt=&quot;Get Adobe Flash player&quot; /&gt;
        &lt;/a&gt;
    &lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>No additional nothing here just:</p>
<ul>
<li>line 20: allowscriptaccess: &#8220;always&#8221; (&#8220;sameDomain&#8221; may also be used)</li>
<li>line 24: tip &#8211; add name attribute with swfobject so ExternalInterface.objectID will get correct value</li>
</ul>
<p><a href="http://stackoverflow.com/questions/86513/why-is-using-javascript-eval-function-a-bad-idea">Considerations of using eval</a>:</p>
<ul>
<li>Improper use of eval opens up your code for injection attacks</li>
<li>Debugging can be more challenging (no line numbers, etc.)</li>
<li>eval&#8217;d code executes more slowly (no opportunity to compile/cache eval&#8217;d code)</li>
</ul>
<p>Update (Feb 11, 2010): You can use either eval call:</p>
<pre class="brush: as3; title: ; notranslate">ExternalInterface.call(&quot;eval&quot;, script.text);</pre>
<p>or wrap your script into anonymous function:</p>
<pre class="brush: as3; title: ; notranslate">ExternalInterface.call(&quot;function(){&quot; + script.text + &quot;}&quot;);</pre>
<p>Personally, I do not see any difference in execution, it seems to be the same, maybe some performance testing would clarify it. What do you think?</p>
<p>Where to go from here:</p>
<ul>
<li><a href="http://cookbooks.adobe.com/post_Inject_JavaScript_code_in_html_wrapper_from_Flex_A-17157.html">JavaScript injecting by Abdul Qabiz</a></li>
<li><a href="http://cookbooks.adobe.com/post_Embed_of_JavaScript_code_in_the_Flex_application-14530.html?w=rel">Embed of JavaScript code in the Flex application</a></li>
<li><a href="http://code.google.com/p/jsinterface/">JSInterface &#8211; JavaScript API for ActionScript 3</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2010/02/full-javascript-access-from-actionscript/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>FB.Connect.showPermissionDialog</title>
		<link>http://blog.yoz.sk/2009/10/fb-connect-showpermissiondialog/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=fb-connect-showpermissiondialog</link>
		<comments>http://blog.yoz.sk/2009/10/fb-connect-showpermissiondialog/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 13:01:08 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[Facebook]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[facebook api]]></category>
		<category><![CDATA[solution]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=385</guid>
		<description><![CDATA[If your facebook iframe applications need extra permissions granted, you can use Facebook JavaScript Api to generate modal window over your content to request permissions from users. For this purpose I created a simple FB_test_perm() function. It takes 4 arguments: api_key &#8211; your application api key xd_receiver &#8211; path to xd_receiver.htm file (Cross-Domain Receiver Page) [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.yoz.sk/wp-content/uploads/facebookpermissions.png"><img src="http://blog.yoz.sk/wp-content/uploads/facebookpermissions-200x176.png" alt="facebookpermissions" title="facebookpermissions" width="200" height="176" class="alignleft size-medium wp-image-450" /></a></p>
<p>If your facebook iframe applications need extra permissions granted, you can use Facebook JavaScript Api to generate modal window over your content to request permissions from users. For this purpose I created a simple FB_test_perm() function. It takes 4 arguments:</p>
<ul style="float:left;">
<li><strong>api_key</strong> &#8211; your application api key</li>
<li><strong>xd_receiver</strong> &#8211; path to xd_receiver.htm file (<a href="http://wiki.developers.facebook.com/index.php/Cross_Domain_Communication_Channel">Cross-Domain Receiver Page</a>)</li>
<li><strong>permission</strong> &#8211; <a href="http://wiki.developers.facebook.com/index.php/Extended_permission">publish_stream, read_stream &#8230;</a></li>
<li><strong>callback</strong> &#8211; your function with boolean return (true if permission granted, false if not granted)</li>
</ul>
<div style="clear:both;"></div>
<p><span id="more-385"></span></p>
<p>facebook_permission_test.js</p>
<pre class="brush: jscript; title: ; notranslate">function FB_test_perm(api_key, xd_receiver, permission, callback)
{
	FB.Bootstrap.requireFeatures([&quot;Connect&quot;], function()
	{
		FB.Facebook.init(api_key, xd_receiver);
		FB.Connect.requireSession(function()
		{
			FB.Facebook.apiClient.users_hasAppPermission(permission, function(result)
			{
				if (result != 0)
					return callback(true);

				FB.Connect.showPermissionDialog(permission, function(result)
				{
					return callback(result == null ? false : (result.indexOf(permission) != -1));
				}, true, null);
			});
		});
	});
}</pre>
<p>index.html</p>
<pre class="brush: xml; title: ; notranslate">&lt;html&gt;
&lt;body&gt;
&lt;script src=&quot;http://static.ak.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;facebook_permission_test.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
	function FB_perm_result(value){
		alert(value ? &quot;granted&quot; : &quot;not granted&quot;);
	}
	FB_test_perm(&quot;***&quot;, &quot;xd_receiver.htm&quot;, &quot;publish_stream&quot;, FB_perm_result);
&lt;/script&gt;
&lt;!-- Note: Include this div markup as a workaround for a known bug in this release on IE where you may get a &quot;operation aborted&quot; error --&gt;
&lt;div id=&quot;FB_HiddenIFrameContainer&quot; style=&quot;display:none; position:absolute; left:-100px; top:-100px; width:0px; height: 0px;&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
<p>xd_receiver.htm</p>
<pre class="brush: xml; title: ; notranslate">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot;
   &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; &gt;
&lt;head&gt;
    &lt;title&gt;Cross-Domain Receiver Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;script src=&quot;http://static.ak.facebook.com/js/api_lib/v0.4/XdCommReceiver.js?v2&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>If you can not get your facebook showPermissionDialog popup opened, make sure you set Canvas Callback URL, Connect URL, Base domain, Connect Preview URL correctly:</p>
<p><a href="http://blog.yoz.sk/wp-content/uploads/Clipboard011.png"><img src="http://blog.yoz.sk/wp-content/uploads/Clipboard011-143x200.png" alt="Clipboard01" title="Clipboard01" width="143" height="200" class="alignnone size-medium wp-image-386" /></a></p>
<p>Make sure you embed flash in transparent / opaque wmode and iframe size (width, height) is enough to display facebook modal window.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/10/fb-connect-showpermissiondialog/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Flex IFrame &#8211; Web browser in flash (update)</title>
		<link>http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=flex-iframe-web-browser-in-flash</link>
		<comments>http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 10:45:21 +0000</pubDate>
		<dc:creator>Jozef Chúťka</dc:creator>
				<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Flash / Flex]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[Canvas]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[FlexIFrame]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[IFrame]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.yoz.sk/?p=139</guid>
		<description><![CDATA[You can not have a web browser inside your web flash applications right? Well, you can! This solution uses html iframe to generate fake build in browser. So the behaviour of the browser in flash is the same as the one you are previewing this flash in (it is the same one). The best part [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://blog.yoz.sk/wp-content/uploads/Clipboard02.png"><img src="http://blog.yoz.sk/wp-content/uploads/Clipboard02-200x196.png" alt="Clipboard02" title="Clipboard02" width="200" height="196" class="alignleft size-medium wp-image-370" /></a></p>
<p>You can not have a web browser inside your web flash applications right? Well, you can! This solution uses html iframe to generate fake build in browser. So the behaviour of the browser in flash is the same as the one you are previewing this flash in (it is the same one). The best part of it is, you can manipulate with the position and size of this browser directly from flash. <a href="http://classes.yoz.sk/sk/yoz/html/IFrame.as">sk.yoz.html.IFrame</a> solution was inspired by <a href="http://www.netthreads.co.uk/">Alistair Rutherford, www.netthreads.co.uk</a>.</p>
<ul style="float: left;">
<li><a href="http://classes.yoz.sk/sk/yoz/html/IFrame.as">sk.yoz.html.IFrame</a></li>
<li>FlexIFrame.js</li>
<li>Application</li>
<li>Application html</li>
</ul>
<p><span id="more-139"></span></p>
<p style="clear:both;">Flex Iframe application</p>
<p><iframe src="http://blog.yoz.sk/examples/flexIframe/" width="100%" height="350" style="border:none;"></iframe></p>
<p>Core of this thing is <a href="http://classes.yoz.sk/sk/yoz/html/IFrame.as">sk.yoz.html.IFrame</a> class. It extends Canvas that is used to inherit properties and methods for positioning and sizing. There is one in order to make it work &#8211; every IFrame element must have id!</p>
<pre class="brush: as3; title: ; notranslate">package sk.yoz.html
{
    import flash.events.Event;
    import flash.external.ExternalInterface;
    import flash.geom.Point;

    import mx.containers.Canvas;

    public class IFrame extends Canvas
    {
        public var methodResize:String = &quot;FlexIFrame.resize&quot;;
        public var methodMove:String = &quot;FlexIFrame.move&quot;;
        public var methodNavigate:String = &quot;FlexIFrame.navigate&quot;;
        public var methodVisibility:String = &quot;FlexIFrame.visibility&quot;;

        public var positionChanged:Boolean = false;
        public var sizeChanged:Boolean = false;

        private var _autoResize:Boolean = false;
        private var _url:String = '';

        public function IFrame()
        {
            super();
            addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler)
        }

        override public function set id(value:String):void
        {
            super.id = value;
        }

        protected function addedToStageHandler(event:Event):void
        {
            positionChanged = true;
            sizeChanged = true;
            invalidateProperties();
        }

        public function set autoResize(value:Boolean):void
        {
            _autoResize = value;
            if(value)
                addEventListener(Event.RESIZE, autoResizeHandler);
            else
                removeEventListener(Event.RESIZE, autoResizeHandler);
        }

        public function get autoResize():Boolean
        {
            return _autoResize;
        }

        protected function autoResizeHandler(event:Event):void
        {
            positionChanged = true;
            sizeChanged = true;
            invalidateProperties();
        }

        override protected function commitProperties():void
        {
            super.commitProperties();

            if(positionChanged)
            {
                var point:Point = localToGlobal(new Point(0, 0));
                callJS(methodMove, point.x, point.y);
                positionChanged = false;
            }

            if(sizeChanged)
            {
                callJS(methodResize, width, height);
                sizeChanged = false;
            }
        }

        public function set url(value:String):void
        {
            _url = value;
            callJS(methodNavigate, value)
        }

        public function get url():String
        {
            return _url;
        }

        protected function callJS(method:String, ... values):void
        {
            if(!id)
                throw new Error(&quot;IFrame id is not defined&quot;);
            var args:Array = [method, id];
            try
            {
                ExternalInterface.call.apply(ExternalInterface, args.concat(values));
            }
            catch(error:Error)
            {
                trace(error.message);
            }
        }

        override public function set visible(value:Boolean):void
        {
            super.visible = value;
            callJS(methodVisibility, value);
        }

        override public function set width(value:Number):void
        {
            super.width = value;
            callJS(methodResize, value, height);
        }

        override public function set height(value:Number):void
        {
            super.height = value;
            callJS(methodResize, width, value);
        }

        override public function set x(value:Number):void
        {
            super.x = value;
            var point:Point = localToGlobal(new Point(0, 0));
            callJS(methodMove, point.x, point.y);
        }

        override public function set y(value:Number):void
        {
            super.y = value;
            var point:Point = localToGlobal(new Point(0, 0));
            callJS(methodMove, point.x, point.y);
        }
    }
}</pre>
<p> FlexIFrame.js is JavaScript file. It uses your flex IFrame id from to create (html iframe) or use html element (any element) with the same id (id in flash = id in html). So you can use your prefabricated html elements (banners etc).</p>
<pre class="brush: jscript; title: ; notranslate">var FlexIFrame = {
    get: function(id)
    {
        var iframe = document.getElementById(id);
        if(!iframe)
            return FlexIFrame.create(id);
        return iframe;
    },

    create: function(id)
    {
        var iframe = document.createElement('iframe');
        iframe.id = id;
        iframe.frameborder = 0;
        iframe.style.position = &quot;absolute&quot;;
        iframe.style.zIndex = 1;
        iframe.style.border = &quot;none&quot;;
        document.body.insertBefore(iframe, document.body.firstChild);
        return iframe;
    },

    resize: function(id, width, height)
    {
        var iframe = FlexIFrame.get(id);
        iframe.style.width = width + &quot;px&quot;;
        iframe.style.height = height + &quot;px&quot;;
    },

    move: function(id, x, y)
    {
        var iframe = FlexIFrame.get(id);
        iframe.style.left = x + &quot;px&quot;;
        iframe.style.top = y + &quot;px&quot;;
    },

    navigate: function(id, url)
    {
        var iframe = FlexIFrame.get(id);
        iframe.src = url;
    },

    visibility: function(id, visible)
    {
        var iframe = FlexIFrame.get(id);
        iframe.style.display = visible ? &quot;block&quot; : &quot;none&quot;;
    }
}</pre>
<p>This is simple applicationion presenting draggable TitleWindow with &#8220;web browser&#8221; inside:</p>
<pre class="brush: xml; title: ; notranslate">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot; layout=&quot;vertical&quot; applicationComplete=&quot;init()&quot; xmlns:html=&quot;sk.yoz.html.*&quot;&gt;
&lt;mx:Script&gt;
&lt;![CDATA[
    private function init():void
    {
        go();
    }

    private function go():void
    {
        iframe.url = iframeURL.text;
    }

    private function mouseDownHandler():void
    {
        container.startDrag();
        container.addEventListener(Event.ENTER_FRAME, enterFrameDrag);
    }

    private function mouseUpHandler():void
    {
        container.stopDrag();
    }

    private function enterFrameDrag(event:Event):void
    {
        iframe.positionChanged = true;
        iframe.invalidateProperties();
    }
]]&gt;
&lt;/mx:Script&gt;
&lt;mx:TitleWindow id=&quot;container&quot; layout=&quot;vertical&quot; width=&quot;300&quot; height=&quot;300&quot;
    mouseDown=&quot;mouseDownHandler()&quot; mouseUp=&quot;mouseUpHandler()&quot; &gt;
    &lt;mx:HBox width=&quot;100%&quot;&gt;
        &lt;mx:TextInput id=&quot;iframeURL&quot; text=&quot;http://blog.yoz.sk&quot; width=&quot;100%&quot;
            enter=&quot;go()&quot;/&gt;
        &lt;mx:Button label=&quot;Go&quot; click=&quot;go()&quot;/&gt;
    &lt;/mx:HBox&gt;
    &lt;html:IFrame width=&quot;100%&quot; height=&quot;100%&quot; id=&quot;iframe&quot; autoResize=&quot;true&quot;/&gt;
&lt;/mx:TitleWindow&gt;
&lt;/mx:Application&gt;</pre>
<p>Finally do not forget to add flexiframe.js into your .html file:</p>
<pre class="brush: xml; title: ; notranslate">&lt;html&gt;
    &lt;head&gt;
        &lt;script type=&quot;text/javascript&quot; src=&quot;js/flexiframe.js&quot;&gt;&lt;/script&gt;
        ...</pre>
<p>Update (Feb 8, 2010): flexiframe.js resize() method uses style property for width and height (instead of original html element width, height)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.yoz.sk/2009/10/flex-iframe-web-browser-in-flash/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

