You now understand the basics of script injection. We have shown that by using a simple mixture of JavaScript, XML, and AS3, it's very easy to extend the powers of Flash into the world of the webpage.

This concludes the AS3 Script Injection Tutorial.

I leave you with a few useful tidbits that didn't quite fit in anywhere:

For Best Practices I suggest ending all variable names with _js or _vb for JavaScript XML and VBScript XML, respectively. It would have been nice to create a JavaScript extension to the XML Class, but the XML class is marked as final so declarations like var script_js:JavaScript are not yet possible. If anybody knows of a way to pull this off, please let me know!


Injection Starter Script: Here's an empty Wrapper Function for your copy-paste boilerplate enjoyment, consisting of the import, XML, and calling functions. It's handy to keep around; I for one always forget how to properly format a CDATA tag!
import flash.external.ExternalInterface;
var script_js :XML =
    <script>
        <![CDATA[
            function(){
                // Your Code Here: Dont forget to indent with SPACES
            }
        ]]>
    </script>
ExternalInterface.call(script_js);


Which Browser?:
Although by no means the best way to do this, the following script will at least tell you if your SWF file is running within Internet Explorer, so you'll know if VBScript injection is possible:
import flash.external.ExternalInterface;
var getProp_js :XML =
    <script>
        <![CDATA[
            function(){
                return ~PROP~;
            }
        ]]>
    </script>;

function GetBrowserProperty(myProp:String):String {
    // Helper function to explicitly query the browser
    // for the specified property value.
    // Note that this can cause the browser to crash
    // if you try to get too complex of an object:
    // keep it to strings and numbers if possible.
    var js = getProp_js.toString();
    js = js.replace( /~PROP~/g,myProp);
    trace(js);
    try {
        var myresult = ExternalInterface.call(js);
    } catch (e) {
        myresult=false;
    }
    return myresult;
}

var _userAgent = String(GetBrowserProperty("navigator.userAgent"));
var _isIE = (_userAgent=="")?false:(_userAgent.indexOf("MSIE")!= -1);
ExternalInterface.call("alert", "Internet Exploder:" + _isIE);


Look for an advanced version of this script in a future article.

For Further Reading:
Googling the term "Bookmarklets" will result in a plethora of premade anonymous Wrapper Functions, most of which may be stored and injected from ActionScript. Bookmarklets are JavaScript-based URLs ("javascript:") intended to be added to a user's "Favorites" list; when clicked, a bookmarklet performs some function, one of a wide variety such as a page layout modification, a search query or data extraction, rather than go to another webpage like normal Bookmarks do.

For example: Show the cookie for this Site

The bookmarklet's URL is posted below:

javascript:if(document.cookie.length<1){alert('No%20cookie%20for%20this%20site.')}else{alert('Cookie%20for%20this%20site:'+document.cookie)}


Bookmarklets went out of fashion a few years ago because of their (then) limited use, but are regaining popularity because of renewed interest (and improved capabilities) in JavaScript. Most bookmarklets can be used directly from ActionScript with little or no modification; even if you can't find one that suits your immediate need, studying them may help you better understand how to write Wrapper-encapsulated functions.



This was my first tutorial. Comments, suggestions, etc are appreciated!