{"id":195,"date":"2014-03-10T15:46:18","date_gmt":"2014-03-10T15:46:18","guid":{"rendered":"http:\/\/www.confidant.ca\/blog\/?p=195"},"modified":"2017-04-21T17:12:02","modified_gmt":"2017-04-21T17:12:02","slug":"solved-using-externalinterface-with-swiffy-and-as2","status":"publish","type":"post","link":"https:\/\/www.confidant.ca\/blog\/2014\/solved-using-externalinterface-with-swiffy-and-as2\/","title":{"rendered":"Solved: Using ExternalInterface With Swiffy and AS2"},"content":{"rendered":"<p>I\u2019ve been thankful for <a title=\"Google's Flash Conversion Tool\" href=\"https:\/\/www.google.com\/doubleclick\/studio\/swiffy\/\">Swiffy<\/a> as a tool for migrating Flash content to HTML5\/SVG. It really does an amazing job and its drawbacks are few, but recently I discovered that it does not support using ExternalInterface with ActionScript 2 projects. In layman&#8217;s terms this means that the converted application cannot communicate with JavaScript on the page it resides on.<\/p>\n<h2><strong>The problem:<\/strong><\/h2>\n<p>Our application needed to support keyboard input on touch-screen devices, and the keyboard is only summoned when a real text input box receives focus. Swiffy uses SVG, which has no form text fields. Therefore we needed to use a JavaScript prompt for entering text, which then returned its data back to the Swiffy application. Remember, we can&#8217;t use ExternalInterface.<\/p>\n<h2>The solution: NOT using ExternalInterface<\/h2>\n<p>Fortunately LoadVars is supported with Swiffy&#8217;s AS2, so I came up with the idea of tricking the application into thinking it was communicating with a server. Fortunately I found <a href=\"https:\/\/github.com\/deitch\/ewsjs\/\">EWSJS<\/a>, which is a JavaScript for doing just that. It intercepts AJAX calls and returns whatever data you like.<\/p>\n<p>I have posted my code in case anybody wants it, but seriously\u2014if you don&#8217;t need this problem solved save yourself and stop reading now.<\/p>\n<p>The Actionscript needed to have these functions:<code><pre><code class=\"prettify\">\ngrabTextLabelFromJavascript();\nvar result_thing:LoadVars = new LoadVars();\nvar t:Number;\nresult_thing.onLoad = function(success:Boolean) {\nif (success) {\ntrace (&quot;got it:&quot;+result_thing.newTextValue);\nif(result_thing.newTextValue==&quot;&quot;){\n\/\/keep going\ntrace(&quot;keep going&quot;);\n} else {\nif( result_thing.newTextValue==&quot;CANCEL&quot;){\ntrace(&quot;cancel&quot;);\n} else {\ntrace(&quot;making a new label:&quot;+result_thing.newTextValue);\nmakeTextLabel(result_thing.newTextValue);\n}\nclearInterval(t);\ngetURL(&quot;javascript:enableServer(false);&quot;); \/\/disables the fake server\n}\n} else {\ntrace(&quot;nothing&quot;);\n}\n};\nfunction grabTextLabelFromJavascript(){\nt=setInterval(this,&quot;loadIt&quot;,500);\ngetURL(&quot;javascript:summonPrompt();&quot;);\n}\nfunction loadIt():Void{\nvar send_lv:LoadVars = new LoadVars();\nsend_lv.sendAndLoad(&quot;\/labelValue&quot;, result_thing, &quot;GET&quot;);\n}\n<\/code><\/pre><br \/>\n<\/code><br \/>\nThe Swiffy resides in an iframe on another page so there is code on both HTML documents. First the iframe code:<code><br \/>\n<pre><code class=\"prettify\">\/\/ remember you will need to embed ewsjs.js and JQuery on this page\n&lt;script type=&quot;text\/javascript&quot;&gt;\/\/ &lt;!&amp;#91;CDATA&amp;#91; var newLabel=&quot;&quot;; var theResponse=&quot;&quot;; (function(&#36&;) { var route, router; router = { &quot;\/labelValue&quot;: { &quot;GET&quot;: function(config) { \/\/ return the new text label if (newLabel!=&quot;&quot;) { console.log(&quot;newLabel ok:&quot;+newLabel); theResponse=&quot;newTextValue=&quot;+newLabel; newLabel=&quot;&quot;; }else{ console.log(&quot;nope&quot;); theResponse=&quot;newTextValue=&quot;; } return({status: 200, response: theResponse, headers: {&quot;Content-Type&quot;:&quot;text\/plain&quot;}}); } } }; \/\/ simple to wrap around the actual handler, makes it easier to handle GET\/PUT\/POST\/DELETE var handlerFn = function(r) { return function(config){ if (r&amp;#91;config.method&amp;#93;) { return(r&amp;#91;config.method&amp;#93;(config)); } }; }; for (route in router) { if (router.hasOwnProperty(route)) { EWS.registerHandler(route,handlerFn(router&amp;#91;route&amp;#93;)); } } }(jQuery)); function saveLabel(l){ console.log(&quot;saving label:&quot;+l); newLabel=l; } \/\/ &amp;#93;&amp;#93;&gt;&lt;\/script&gt;<\/code><\/pre><\/code><\/p>\n<p>Now the iframe&#8217;s containing page:<code><br \/>\n<pre><code class=\"prettify\">&lt;script type=&quot;text\/javascript&quot;&gt;\/\/ &lt;!&amp;#91;CDATA&amp;#91; var newText=&quot;&quot;; function summonPrompt(){ enableServer(true); \/\/newText=&quot;&quot;; var lt = prompt(&quot;Please enter text for the label:&quot;,&quot;Label text&quot;); if (lt!=null) { document.getElementById(&quot;swiffyFrame&quot;).contentWindow.saveLabel(lt); \/\/newText=lt; } else { document.getElementById(&quot;swiffyFrame&quot;).contentWindow.saveLabel(&quot;CANCEL&quot;); } } function enableServer(bool){ console.log(&quot;enabling server:&quot;+bool); document.getElementById(&#039;swiffyFrame&#039;).contentWindow.EWS.enable(bool); } \/\/ &amp;#93;&amp;#93;&gt;&lt;\/script&gt;<\/code><\/pre><\/code><br \/>\nI hope this helps someone out there in cyberspace.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been thankful for Swiffy as a tool for migrating Flash content to HTML5\/SVG. It really does an amazing job and its drawbacks are few, but recently I discovered that it does not support using ExternalInterface with ActionScript 2 projects. In layman&#8217;s terms this means that the converted application cannot communicate with JavaScript on the &hellip; <a href=\"https:\/\/www.confidant.ca\/blog\/2014\/solved-using-externalinterface-with-swiffy-and-as2\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Solved: Using ExternalInterface With Swiffy and AS2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[3],"tags":[37,31,36],"class_list":["post-195","post","type-post","status-publish","format-standard","hentry","category-development","tag-as2","tag-flash","tag-swiffy"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p28GE6-39","_links":{"self":[{"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/posts\/195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/comments?post=195"}],"version-history":[{"count":13,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/posts\/195\/revisions\/296"}],"wp:attachment":[{"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/media?parent=195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/categories?post=195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.confidant.ca\/blog\/wp-json\/wp\/v2\/tags?post=195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}