Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frame.ExecuteScript to retrieve frame html source problem #18

Open
PaulYBChiang opened this issue Feb 10, 2025 · 1 comment
Open

Frame.ExecuteScript to retrieve frame html source problem #18

PaulYBChiang opened this issue Feb 10, 2025 · 1 comment

Comments

@PaulYBChiang
Copy link

The following code frameId can be retrieved but resultObjectAsJson always returned null watever script was document.documentElement.outerHTML; or "return 'Hello from JavaScript!';";

Frame.ExecuteScript works?

    public static TabPage AddNewWebPage(string title, string url)
    {
        var newTab = new TabPage(title)
        { Tag = url };
        tabcontrol.TabPages.Add(newTab);
        var webView = new WebView() { Dock = DockStyle.Fill };
        webView.FrameCreated += (s, e) => 
        {
            var frameId = e.Frame.FrameId;
            var frameName = e.Frame.name;
            Console.WriteLine($"Frame Created - ID: {frameId}, Name: {frameName}");

            string script = "document.documentElement.outerHTML;";
            

            var handler = new ScriptCompletedHandler();

            e.Frame.ExecuteScript(script, handler);
        };
        newTab.Controls.Add(webView);
        webView.Navigate(url);

        return newTab;
    }
    public class ScriptCompletedHandler : ICoreWebView2ExecuteScriptCompletedHandler
    {
        public ScriptCompletedHandler() { }

        public void Invoke(int errorCode, string resultObjectAsJson)
        {
            if (errorCode == 0)
            {
                Console.WriteLine("Script executed successfully.");

                Console.WriteLine("HTML Source:");
                Console.WriteLine(resultObjectAsJson);
            }
            else
            {
                Console.WriteLine($"Script execution failed with error code: {errorCode}");
            }
        }
    }
@DIGAToDIGA
Copy link
Contributor

https://learn.microsoft.com/en-us/microsoft-edge/webview2/reference/win32/icorewebview2frame2?view=webview2-1.0.2957.106#executescript
=> "The result of evaluating the provided JavaScript is passed to the completion handler. The result value is a JSON encoded string. If the result is undefined, contains a reference cycle, or otherwise is not able to be encoded into JSON, then the result is considered to be null, which is encoded in JSON as the string "null"."

        webView.FrameCreated += (s, e) => 
        {
            var frameId = e.Frame.FrameId;
            var frameName = e.Frame.name;
            Console.WriteLine($"Frame Created - ID: {frameId}, Name: {frameName}");

            ScriptContext.Run(() =>
                {
                        string script = "document.documentElement.outerHTML;";
                        var handler = new ScriptCompletedHandler();
                        e.Frame.ExecuteScript(script, handler);
                });
       }

This might help you.
Please remember that WinForms is synchronous, while the calls from WebView2 are asynchronous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants