JavaScript and VBScript Injection in ActionScript 3

AS3 Script Injection: Bonus Materials
Peter McBride
A 20-year veteran of multimedia design, Pete has been working with Flash since its FutureSplash infancy, garnering numerous awards and recognitions along the way. Considered a pioneer in whatever-it-is we actually do, one of his earliest designs was recently placed in the Smithsonian Institution as a landmark point in the history of computing. In addition to Flash, Pete is an avid JavaScript and CSS addict, with a specialization in cross-language interaction.
View all articles by Peter McBrideYou 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!
Spread The Word
Related Articles
14 Responses to "JavaScript and VBScript Injection in ActionScript 3" 
|
said this on 14 Aug 2008 7:14:51 AM CST
Adobe closed most of thes
I struggle to understa |
|
said this on 30 Dec 2008 8:47:00 AM CST
Some things can't be done
EX. I have an |
|
said this on 17 Aug 2008 6:01:39 PM CST
A few people have raised
I co As I said, the I'll admi 1) Hosting: Sometimes 2) Compressio 3) Collisi 4) Site/Role Blur: 6) Context. Sc 7) Flexibili On one site Script Injectio Like I said in the Enjoy! |
|
said this on 19 Aug 2008 11:10:49 AM CST
The injection you’ve kind
1) Hosting: H 2) Comp 3) Collisions Avoidanc 4) Site/Role Blur: I'm a 5) Modulari 6) Context: Here 7) As for the 90 Fla |
|
said this on 19 Aug 2008 5:47:17 PM CST
"Adobe closed most of the
Not true. This updat This has n If you don't like it, j Your othe |
|
said this on 08 Sep 2008 2:20:52 PM CST
Hi guys,
I wrote an inte |
|
said this on 09 Sep 2008 6:55:56 AM CST
This is an excellent arti
|
|
said this on 12 Nov 2008 4:51:28 PM CST
Great article! :)
How wo I use: <code> < <![CDATA[ // CODE HER ]]> </mx:String> </c That works but if |
|
said this on 20 Jan 2009 4:13:13 AM CST
Excellent article that il
In that situat |
|
said this on 27 Jan 2009 3:06:30 PM CST
I've created what I think
As you may or may no Check it Btw, I a |
|
said this on 01 May 2009 1:36:13 AM CST
Great Article. I spent d
|
|
said this on 04 May 2009 7:46:55 AM CST
"If you don't like it, ju
AllowScrip http://blog. |
|
said this on 04 May 2009 8:14:28 AM CST
"If you don't like it, ju
AllowScrip http://blog. |


Author/Admin)