CEFSHARP如何在Windows中打开的新页面。

发布于 2025-02-07 02:31:13 字数 1288 浏览 3 评论 0原文

我的Winform应用程序使用CEFSHARP, 当我打开page1.html in cefsharppage1.html不是我自己的页面,我无法控制此页面的代码。 page1.html代码看起来像这样:

<html>
    <head>
        <title>page1</title>
    </head>
    <body>
        this is page 111
        <script language=javascript>
            var toUrl="http://localhost/KintechToGica22/page2.html";
            window.open(toUrl,'openTest2','');
        </script>
    </body>
</html>

我可以将js注入page1.html与以下C#代码

//ChromiumWebBrowser browser = (ChromiumWebBrowser)sender;
string text = System.IO.File.ReadAllText(@"c:\my.js");
browser.ExecuteScriptAsync(text);

I使用window.poin.open.open代码> pag1.html 要打开页面page2.htmlpage2.html代码看起来像这样:

<html>
    <head>
        <title>page2</title>
    </head>
    <body>
        this is Page 222
    </body>
</html>

但是我如何控制page2.html, 我想用page2.html注射JS做某事

My Winform application uses CefSharp,
When I open page1.html in CefSharp,
the page1.html is not my own page and I have no control over the code of this page.
The page1.html code looks like this:

<html>
    <head>
        <title>page1</title>
    </head>
    <body>
        this is page 111
        <script language=javascript>
            var toUrl="http://localhost/KintechToGica22/page2.html";
            window.open(toUrl,'openTest2','');
        </script>
    </body>
</html>

I can inject JS into page1.html with the following c# code

//ChromiumWebBrowser browser = (ChromiumWebBrowser)sender;
string text = System.IO.File.ReadAllText(@"c:\my.js");
browser.ExecuteScriptAsync(text);

I use window.open in pag1.html to open a page page2.html,
The page2.html code looks like this:

<html>
    <head>
        <title>page2</title>
    </head>
    <body>
        this is Page 222
    </body>
</html>

But how do I control page2.html,
I want to do something with page2.html injection JS

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少钕鈤記 2025-02-14 02:31:13

window.open调用将创建一个新的弹出窗口。您可以通过实现

一个基本示例看起来像:

public class CustomLoadHandler : CefSharp.Handler.LoadHandler
{
    protected override void OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs args)
    {
        // Execute script before DOM has started loading
        // window.open will create a new popup, check IsPopup = true
        // Add any additional checks like comparing the URL that meet your requirements 
        // Execute your script here on in OnFrameLoadEnd. Not both
        if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
        {
            string script = System.IO.File.ReadAllText(@"c:\my.js");
            args.Frame.ExecuteJavaScriptAsync(script);
        }
        base.OnFrameLoadStart(chromiumWebBrowser, args);
    }

    protected override void OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs args)
    {
        // Execute script before DOM has started loading
        // window.open will create a new popup, check IsPopup = true
        // Add any additional checks like comparing the URL that meet your requirements
        // Execute your script here on in OnFrameLoadStart. Not both
        if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
        {
            string script = System.IO.File.ReadAllText(@"c:\my.js");
            args.Frame.ExecuteJavaScriptAsync(script);
        }

        base.OnFrameLoadEnd(chromiumWebBrowser, args);
    }
}

chromiumWebBrowser.LoadHandler = new CustomLoadHandler();

The window.open call will create a new popup. You can get load events for popups (and main browser) by implementing LoadHandler.

A basic example would look something like:

public class CustomLoadHandler : CefSharp.Handler.LoadHandler
{
    protected override void OnFrameLoadStart(IWebBrowser chromiumWebBrowser, FrameLoadStartEventArgs args)
    {
        // Execute script before DOM has started loading
        // window.open will create a new popup, check IsPopup = true
        // Add any additional checks like comparing the URL that meet your requirements 
        // Execute your script here on in OnFrameLoadEnd. Not both
        if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
        {
            string script = System.IO.File.ReadAllText(@"c:\my.js");
            args.Frame.ExecuteJavaScriptAsync(script);
        }
        base.OnFrameLoadStart(chromiumWebBrowser, args);
    }

    protected override void OnFrameLoadEnd(IWebBrowser chromiumWebBrowser, FrameLoadEndEventArgs args)
    {
        // Execute script before DOM has started loading
        // window.open will create a new popup, check IsPopup = true
        // Add any additional checks like comparing the URL that meet your requirements
        // Execute your script here on in OnFrameLoadStart. Not both
        if (args.Browser.IsPopup && args.Frame.Url.EndsWith("page2.html"))
        {
            string script = System.IO.File.ReadAllText(@"c:\my.js");
            args.Frame.ExecuteJavaScriptAsync(script);
        }

        base.OnFrameLoadEnd(chromiumWebBrowser, args);
    }
}

chromiumWebBrowser.LoadHandler = new CustomLoadHandler();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文