使用CEFSHARP函数GetSourCeasync()时,源代码与ChatsIndionOurce()相比

发布于 2025-02-02 03:51:01 字数 873 浏览 4 评论 0原文

我已经安装了最新版本的CEFSHARP,当我调用Viewsource()时,它将打开带有源代码的记事本窗口。但是,当我调用getSourCeasync()时,代码非常不同,并且缺少我在记事本窗口中显示的 var html 中所需的html。唯一的工作是以某种方式将记事本中的代码内容复制到我的应用中并使用它。有人知道如何像记事本窗口中所示获取HTML吗?我正在使用Visual Studio 2017 Express在Windows 7 Pro操作系统中运行该应用程序。这是我的代码...

        private void WebBrowserFrameLoadEndedAsync(object sender, FrameLoadEndEventArgs e)
        {
                chromeBrowser.ViewSource();

                chromeBrowser.GetSourceAsync().ContinueWith(taskHtml =>
                {
                    var html = taskHtml.Result;
                });
            }
        }

Here is the web page that the browser goes to...

chromeBrowser = new ChromiumWebBrowser("https://www.amazon.com/product-reviews/B084RCFDJ3/ref=acr_search_hist_5?ie=UTF8&filterByStar=five_star&reviewerType=all_reviews#reviews-filter-bar");

I have the latest version of CefSharp installed and when I call ViewSource(), it opens up a notepad window with the source code. But when I call GetSourceAsync() the code is very different and missing the HTML I need in the var html that is shown in the Notepad window. The only work around would be to somehow copy the contents of the code in Notepad into my app and use it. Does anyone know how to get the html as shown in the NotePad window? I'm running the application in the Windows 7 Pro operating system using Visual Studio 2017 Express. Here is my code...

        private void WebBrowserFrameLoadEndedAsync(object sender, FrameLoadEndEventArgs e)
        {
                chromeBrowser.ViewSource();

                chromeBrowser.GetSourceAsync().ContinueWith(taskHtml =>
                {
                    var html = taskHtml.Result;
                });
            }
        }

Here is the web page that the browser goes to...

chromeBrowser = new ChromiumWebBrowser("https://www.amazon.com/product-reviews/B084RCFDJ3/ref=acr_search_hist_5?ie=UTF8&filterByStar=five_star&reviewerType=all_reviews#reviews-filter-bar");

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

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

发布评论

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

评论(2

一向肩并 2025-02-09 03:51:01

事实证明,我正在搜索错误的短语。所以现在我只打电话以下...

string source = await chromeBrowser.GetBrowser().MainFrame.GetSourceAsync();

It turns out, I was searching the source for the wrong phrase. So now I just call the following...

string source = await chromeBrowser.GetBrowser().MainFrame.GetSourceAsync();
拥抱影子 2025-02-09 03:51:01

我已经详细介绍了GetSource和ViewSource之间的区别。

有关FrameloDadend的一些重要事情。

  • 被称为每个帧,如果您的页面具有多个帧,则
  • 在初始资源完成加载时将被称为多次,如果您的网站是动态创建/渲染的,那么您的呼叫也许也可能发生早期的。
//FrameLoadEnd is called for every frame, if your page has multiple frames then it will be called multiple times.
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    var source = await frame.GetSourceAsync();
}

//To only get the main frame source
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    if (frame.IsMain)
    {
        var source = await frame.GetSourceAsync();
    }
}

// If your website dynamically generates content then you might need to wait a
// little longer for it to render. Introduce a fixed wait period, this can be
// problematic for a number of reasons. 
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    if (frame.IsMain)
    {
        //  Wait a little bit of time for the page to load
        await System.Threading.Tasks.Task.Delay(500);
        var source = await frame.GetSourceAsync();
    }
}

说明行为差异的说明

首先viewsource()立即返回,NotepadgetsourCeasync call具有完全的。

这两种方法都将getsource消息发送到渲染过程,该过程返回readonlysharedMemoryRegion。当您从共享内存部分中阅读数据时,及时会带有不同的快照。

void CefFrameHostImpl::ViewSource() {
  SendCommandWithResponse(
      "GetSource",
      base::BindOnce(&ViewTextCallback, CefRefPtr<CefFrameHostImpl>(this)));
}

void CefFrameHostImpl::GetSource(CefRefPtr<CefStringVisitor> visitor) {
  SendCommandWithResponse("GetSource",
                          base::BindOnce(&StringVisitCallback, visitor));
}

cefframeHostimpl :: getsource方法getsourCeasync呼叫完成非常快速完成,因为它只是从共享存储器部分创建字符串。

cefframeHostimpl :: viewsource方法立即返回要慢得多,并且需要其他处理来在磁盘上创建文件,请写下该字符串,Spawn Notepad。

HTML源始终是给定时间点的来源的快照。对于静态网页,时间没有区别,对于动态渲染/更新的网站,几百毫秒可能意味着您获得了完全不同的来源。
当共享ReadOnlySharedMemoryRegion转换为字符串时,意味着您最终获得的源有细微的差异。

I've gone into detail on the difference between GetSource and ViewSource further down.

Some important things to note about FrameLoadEnd.

  • Is called for every frame, if your page has multiple frames then it will be called multiple times
  • It's called when the initial resources have finished loading, if your website is dynamically created/rendered then your call maybe happening too early.
//FrameLoadEnd is called for every frame, if your page has multiple frames then it will be called multiple times.
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    var source = await frame.GetSourceAsync();
}

//To only get the main frame source
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    if (frame.IsMain)
    {
        var source = await frame.GetSourceAsync();
    }
}

// If your website dynamically generates content then you might need to wait a
// little longer for it to render. Introduce a fixed wait period, this can be
// problematic for a number of reasons. 
private async void BrowserFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
    var frame = e.Frame;

    if (frame.IsMain)
    {
        //  Wait a little bit of time for the page to load
        await System.Threading.Tasks.Task.Delay(500);
        var source = await frame.GetSourceAsync();
    }
}

Explanation of the difference in behaviour

Firstly ViewSource() returns immediately, Notepad is being launched after the GetSourceAsync call has completed.

Both methods send a GetSource message to the render process which returns a ReadOnlySharedMemoryRegion. When you read the data from the shared memory section ends up with a different snapshot in time.

void CefFrameHostImpl::ViewSource() {
  SendCommandWithResponse(
      "GetSource",
      base::BindOnce(&ViewTextCallback, CefRefPtr<CefFrameHostImpl>(this)));
}

void CefFrameHostImpl::GetSource(CefRefPtr<CefStringVisitor> visitor) {
  SendCommandWithResponse("GetSource",
                          base::BindOnce(&StringVisitCallback, visitor));
}

CEF Source reference.

The CefFrameHostImpl::GetSource method which GetSourceAsync calls completes very quickly as it simply creates a string from the shared memory section.

The CefFrameHostImpl::ViewSource method whilst returns immediately is much slower and takes additional processing to create a file on disk, write that string, spawn notepad.

HTML Source is always a snapshot of source for a given point in time. For static web pages, time makes no difference, for dynamically rendered/updated websites a few hundred milliseconds can mean you get entirely different source.
When the shared ReadOnlySharedMemoryRegion is converted into a string means there is a subtle difference in the source you end up getting.

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