5

I can inject JavaScript in WebBrowser control in C# windows form by this link

How to inject JavaScript in WebBrowser control?

But I can't do this in WP7, please help me.

Community
  • 1
  • 1
sma6871
  • 3,198
  • 3
  • 38
  • 52

2 Answers2

7

Unfortunately WebBrowser.Document is not available on WP7. But you can create and call a JavaScript function using InvokeScript. Have a look over here where I describe how.

In short: you don't use .Document and C# but create a piece of JavaScript instead. You then call eval with this script as parameter to invoke it. Like this:

webBrowser1.InvokeScript("eval", "  ...code goes here... ");
Community
  • 1
  • 1
Heinrich Ulbricht
  • 10,064
  • 4
  • 54
  • 85
  • 1
    but some pages may not contains **eval** function – sma6871 Dec 10 '11 at 10:27
  • @Heinrich Ulbricht I have tried your solution but it does not work in my case. I was wondering if you could help? I have javascript code from http://www.liveside.net/2011/10/21/tip-how-to-get-forward-and-find-on-page-back-in-ie9-mobile-on-windows-phone-7-5/ Find On Page option and can not seem to properly insert it into the InvokeScript function. I am getting Error: 80020101 SystemException unhandled. – Matthew Mar 26 '12 at 01:47
  • @Matthew Try sending in your code with quotationmarks. eg. webBrowser1.InvokeScript("\" ... code goes here... \""); – Martin_G Oct 23 '13 at 11:36
  • @sma6871 **eval** is a built-in JavaScript function and always available, regardless of a webpage's content. – Tom Lint Mar 03 '14 at 08:52
  • 1
    @TomLint, what about a basic page like this: ``? With this one, `eval` sure does *not* work with a desktop `WebBrowser`, at least one ` – noseratio Mar 13 '14 at 08:04
  • @Noseratio InvokeScript directly invokes the built-in javascript parser of the webbrowser to execute the specified javascript, there is no need for any – Tom Lint Sep 04 '14 at 11:17
  • @TomLint, my point is correct at least for IE on Windows Desktop, try it for a page without ` – noseratio Sep 04 '14 at 11:41
1

For desktop WebBrowser (WinForms/WPF), at least one <script> tag has to be present on the web page for InvokeScript("eval", ...) to work. I.e., if the page doesn't contain any JavaScript (e.g. <body></body>), eval doesn't work as is.

I don't have Windows Phone SDK/emulator installed to verify if this is also the case with Windows Phone WebBrowser.

Nevertheless, the following works for a Windows Store App. The trick is to use this.webBrowser.InvokeScript("setTimeout", ...) to inject some JavaScript first. I'm using it instead of execScript, which is deprecated since IE11.

async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    // load a blank page
    var tcsLoad = new TaskCompletionSource<object>();
    this.webBrowser.NavigationCompleted += (s, eArgs) =>
        tcsLoad.TrySetResult(Type.Missing);
    this.webBrowser.NavigateToString("<body></body>");
    await tcsLoad.Task;

    // first add a script via "setTimeout", JavaScript gets initialized
    var tcsInit = new TaskCompletionSource<object>();
    this.webBrowser.ScriptNotify += (s, eArgs) =>
    {
        if (eArgs.Value == "initialized")
            tcsInit.TrySetResult(Type.Missing);
    };
    this.webBrowser.InvokeScript("setTimeout", 
        new string[] { "window.external.notify('initialized')", "0" });
    await tcsInit.Task;

    // then use "eval"
    this.webBrowser.InvokeScript("eval", 
        new string[] { "document.body.style.backgroundColor = 'yellow'" });
}

I would appreciate if someone can confirm whether this works or not for WP WebBrowser. LoadCompleted should be used for WP instead of NavigationCompleted in the code above, and WebBrowser.IsScriptEnabled has to be set to true.

noseratio
  • 59,932
  • 34
  • 208
  • 486
  • From my early investigations the settimeout hack does seem to work also for the WPF WebBrowser Control with IE11. Actually calling InvokeScript("settimeout", "ANYFUNCTIONHERE") causes JavaScript to be activated on the page and thus subsequent eval calls also appear to work. – jpierson Apr 30 '14 at 03:10
  • @jpierson, I did test the `setTimeout` trick for the Desktop and Windows Store apps, but I did not for Windows Phone, which is what's this question is about. – noseratio Apr 30 '14 at 03:16