0

I need to inject a js file with a class into WebBrowser element for WP7 application, but I get an unknown runtime error:

 webbrowser.InvokeScript("eval", "function Player() {  this.play = function(e) { window.external.notify(e); }}

Is this possible?

Darius V
  • 583
  • 1
  • 4
  • 18
  • 1
    Describe your error. You can find it in your IDE. I once encountered with a js inject error because I tried to inject it before the webbrowser is ready. Be sure to place your code in the webbrowser's documentReady() callback – SolessChong Jun 03 '13 at 11:05
  • There is no documentReady() callback in Windows Phone WebBrowser, but the error seems to be gone when I put this code into Navigated callback. There are no errors now. But it is not working... – Darius V Jun 03 '13 at 11:11
  • Duplicate: [How to inject Javascript in WebBrowser control?](http://stackoverflow.com/questions/8348503/how-to-inject-javascript-in-the-wp7-webbrowser-control) – Alaa Masoud Jun 03 '13 at 11:34
  • I have to confess I haven't tried WP. However things are similar with c# winform. And Navigated() is called after successfully loading the document. – SolessChong Jun 03 '13 at 13:54
  • Alla Masoud, it is not a duplicate. This problem is more specific than the one you've pointed out. – Darius V Jun 03 '13 at 16:26

1 Answers1

1

I just encountered this myself.

The problem is that you are not sending in the parameter as a string. eval takes a string as parameter and if you invoke this script with your current parameter it will translate into:

eval(function Player() {  this.play = function(e) { window.external.notify(e); }})

when what you really want to do is:

eval("function Player() {  this.play = function(e) { window.external.notify(e); }}")

So the correct this, try:

webbrowser.InvokeScript("eval", "\"function Player() {  this.play = function(e) { window.external.notify(e); }}\"");
Martin_G
  • 125
  • 1
  • 5