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.

2 Answers 2

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... ");
Sign up to request clarification or add additional context in comments.

7 Comments

but some pages may not contains eval function
@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 liveside.net/2011/10/21/… Find On Page option and can not seem to properly insert it into the InvokeScript function. I am getting Error: 80020101 SystemException unhandled.
@Matthew Try sending in your code with quotationmarks. eg. webBrowser1.InvokeScript("\" ... code goes here... \"");
@sma6871 eval is a built-in JavaScript function and always available, regardless of a webpage's content.
@TomLint, what about a basic page like this: <body></body>? With this one, eval sure does not work with a desktop WebBrowser, at least one <script> tag is required. Do you reckon it is not required for for Windows Phone?
|
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.

2 Comments

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, 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.