{"id":197,"date":"2009-05-01T16:21:14","date_gmt":"2009-05-01T21:21:14","guid":{"rendered":"http:\/\/thaylin.info\/blog\/?p=197"},"modified":"2009-11-04T13:09:48","modified_gmt":"2009-11-04T18:09:48","slug":"javascript-injection-via-as3-to-embed-external-libraries","status":"publish","type":"post","link":"https:\/\/www.thaylin.com\/blog\/2009\/05\/01\/javascript-injection-via-as3-to-embed-external-libraries\/","title":{"rendered":"Javascript injection via as3 to embed external libraries"},"content":{"rendered":"<p>On my quest to make the facebook actionscript connectivity as easy to work with as possible I needed to be able to reproduce the <\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\n<script src=\"myExternalLibrary.js\">\r\n<\/pre>\n<p>call in order for everything to work appropriately. <\/p>\n<p>After some searching I came across a clip of code that worked beautifully <!--more-->and once I implemented it with actionscript it was exactly what I needed:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nvar js:XML = \r\n<script><![CDATA[\r\n\tif(!window.FB)\r\n\t\t{\r\n\t\t\t\tvar head = document.getElementsByTagName('HEAD').item(0);\r\n\t\t\t\tvar body = document.getElementsByTagName('body').item(0);\r\n\t\t\t\tdiv = document.createElement('div');\r\n\t\t\t\tdiv.id = 'FB_HiddenContainer';\r\n\t\t\t\tbody.appendChild(div);\r\n\t\t\t\tscript = document.createElement('script');\r\n\t\t\t\tscript.src = src;\r\n\t\t\t\tscript.type = 'text\/javascript';\r\n\t\t\t\thead.appendChild(script);\r\n\t\t}\r\n\t]]><\/script>\r\nExternalInterface.call(js, 'http:\/\/static.ak.connect.facebook.com\/js\/api_lib\/v0.4\/FeatureLoader.js.php');\r\n<\/pre>\n<p>First let's look at the code to embed the call into the head.<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\n\t\t\tvar head = document.getElementsByTagName('HEAD').item(0);\r\n\t\t\tscript = document.createElement('script');\r\n\t\t\tscript.src = src;\r\n\t\t\tscript.type = 'text\/javascript';\r\n\t\t\thead.appendChild(script);\r\n<\/pre>\n<p>This take sthe element on the html that is the head tag and creates a script element (pretty much just how it reads). the src is then pushed in through the call to ExternalInterface with<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\nExternalInterface.call(js, 'http:\/\/static.ak.connect.facebook.com\/js\/api_lib\/v0.4\/FeatureLoader.js.php');\r\n<\/pre>\n<p>Once I create this initial test I ran the call and my flash kept disappearing. I was like WTF!?!?<br \/>\nThen after going through the Facebook js code I realized there was a test to check if there was a hidden div container called FB_HiddenContainer in the html and if not then document.write() it. This overwrote the embedding code I was using to embed my flash thereby removing it totally from the page. I use the same type of method for the insertion of the script tag to insert a div tag in the body of the html as follows:<\/p>\n<pre lang=\"actionscript\" line=\"1\" colla=\"+\">\r\n\t\t\t\tvar body = document.getElementsByTagName('body').item(0);\r\n\t\t\t\tdiv = document.createElement('div');\r\n\t\t\t\tdiv.id = 'FB_HiddenContainer';\r\n\t\t\t\tbody.appendChild(div);\r\n<\/pre>\n<p>And voila! You now have a [script src='http:\/\/static.ak.connect.facebook.com\/js\/api_lib\/v0.4\/FeatureLoader.js.php'] making all FB methods available, as well as the [div id='FB_HiddenContainer'] container in the body to prevent that funkiness of removing your swf. <\/p>\n<p>Hope this might be helpful to anyone wanting to do the same!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On my quest to make the facebook actionscript connectivity as easy to work with as possible I needed to be able to reproduce the call in order for everything to work appropriately. After some searching I came across a clip of code that worked beautifully<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[11,6],"tags":[32,16,15,31],"_links":{"self":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/197"}],"collection":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":11,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"predecessor-version":[{"id":286,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions\/286"}],"wp:attachment":[{"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.thaylin.com\/blog\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}