W00t!! The window.eval() function is something I've been wanting in Silverlight forever!! Looks like it's here now, finally, in Silverlight v2!
// App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e)
{
// Load the main control // generated
this.RootVisual = new Page(); // generated
// added ..
string html = HtmlPage.Window.Eval("document.documentElement.innerHTML").ToString();
HtmlPage.Window.Alert(html); // worked!
}
Now lemme try some jQuery ..
<!-- HTML: -->
<script type="text/javascript" language="javascript">
function showAlert(el) {
alert("Alert from jQuery introspection of $(" + el.tagName + ").html() ..\n\n"
+ $(el).html());
}
</script>
...
// App.xaml.cs:
private void Application_Startup(object sender, StartupEventArgs e)
{
// Load the main control // generated
this.RootVisual = new Page(); // generated
object bodyObject = HtmlPage.Window.Eval("document.body");
HtmlPage.Window.Invoke("showAlert", bodyObject);
}
W00t! It works!! Now lemme try some plugin interop using only outer passing...
<!-- HTML -->
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"
width="550" height="400" id="tweenMov">
<!-- first random swf I found -->
<param name="movie" value="http://www.albinoblacksheep.com/flash01/tweenaflashodyssey(www.albinoblacksheep.com).swf">
<param name="quality" value="high">
<param name="bgcolor" value="#000000">
<embed src="/support/flash/ts/documents/myFlashMovie.swf"
quality=high bgcolor=#FFFFFF width="550" height="400"
name="tweenMovie" id="smurfsMovie align="" type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</object>
...
// App.xaml.cs:
object movieObject = HtmlPage.Window.Eval("tweenMovie");
Hmm, putting a breakpoint on this and introspecting movieObject, it's still an HtmlElement with nothing much associated with it.
I can still pass it around as an object reference ...
Initial jQuery test:
<!-- HTML <script>: -->
window.onload = function() {
var movieObj = $("#tweenMov")[0];
alert("bgcolor=" + movieObj.bgcolor + "\n"
+ "quality=" + movieObj.quality);
}
.. and then try same test after passing to Silverlight and having Silverlight pass it back out again ..
<!-- HTML <script>: -->
function showParams(movieObj, fromMsg) {
alert(fromMsg + "\n\nbgcolor=" + movieObj.bgcolor + "\n"
+ "quality=" + movieObj.quality);
}
..
// App.xaml.cs:
object movieObject = HtmlPage.Window.Eval("tweenMov");
HtmlPage.Window.Invoke("showParams", movieObject, "From Silverlight:");
Worked! Tested in Firefox, too, but it failed; apparently, it choked on the Flash markup before the other stuff had a chance, and I'm too tired to clean it up.
Now let's see if I can declare Javascript functions on the fly so that I can use browser scripting to perform cross-plugin communications without prior scripting in markup:
// App.xaml.cs:
object val = HtmlPage.Window.Eval("function showHelloWorld() {"
+ " alert(\"Hello world! This is Silverlight talkin!\");"
+ " return \"Yay!\";"
+ "}"
+ "showHelloWorld();");
HtmlPage.Window.Alert(val.ToString());
It works. Yay!! Can I do the same to elaborately introspect the Flash plug-in?
object val = HtmlPage.Window.Eval("function getFlashBgColor() {"
+ "return $(\"#tweenMov\")[0].bgcolor;"
+ "}"
+ "getFlashBgColor();");
HtmlPage.Window.Alert("Flash background color: " + val.ToString());
Looks like I got my wish! Now I can use the CLR to control DHTML through the browser script runtime, which alone knows the full extent of its DOM. Silverlight is browser script runtime friendly!