CEF 重写弹窗事件

CEF开发想要在当前窗体加载目标url,而不想在弹出窗口中打开网页,需要重写OnBeforePopup,它是属于CefLifeSpanHandler类中。

bool CefHandlerImpl::OnBeforePopup(CefRefPtr<CefBrowser> browser,
                                    CefRefPtr<CefFrame> frame,
                                    const CefString& target_url,
                                    const CefString& target_frame_name,
                                    CefLifeSpanHandler::WindowOpenDisposition target_disposition,
                                    bool user_gesture,
                                    const CefPopupFeatures& popupFeatures,
                                    CefWindowInfo& windowInfo,
                                    CefRefPtr<CefClient>& client,
                                    CefBrowserSettings& settings,
                                    CefRefPtr<CefDictionaryValue>& extra_info,
                                    bool* no_javascript_access)
{
    switch (target_disposition)
    {
    case WOD_NEW_FOREGROUND_TAB:
    case WOD_NEW_BACKGROUND_TAB:
    case WOD_NEW_POPUP:
    case WOD_NEW_WINDOW:
        browser->GetMainFrame()->LoadURL(target_url);
        return true;
    }

    return false;
}

第一个参数browser代表了发出popup请求的浏览器对象。

frame是发出popup请求的那个frame。

target_url是要加载的目标url。

target_disposition是显示方式(UNENOWN,当前选项卡,单例模式选项卡,新前台选项卡,新的背景选项卡,新弹出,新窗口,保存到磁盘,记录,忽视行动)。

user_gesture表示弹窗是用户手动点击打开(true)还是自动打开(false)的。

popupFeatures结构包含了关于请求的弹出窗口的额外信息。

//弹窗的额外信息
popupFeatures.height;   //弹窗的高度
popupFeatures.width;   //弹窗的宽度
popupFeatures.heightSet;
popupFeatures.widthSet;
popupFeatures.x;
popupFeatures.y;
popupFeatures.xSet;
popupFeatures.ySet;
popupFeatures.menuBarVisible;
popupFeatures.statusBarVisible;
popupFeatures.toolBarVisible;
popupFeatures.scrollbarsVisible;

client和settings值将默认为源浏览器的值。如果no_javascript_access值设置为false,则新浏览器将不能编写脚本,并且可能不会托管在与源浏览器相同的渲染进程中。

如果父浏览器被封装在CefBrowserView中,那么对windowInfo的任何修改都会被忽略。如果父浏览器在弹出浏览器创建完成之前被销毁,弹出浏览器创建将被取消(通过调用OnAfterCreated为弹出浏览器指示)。

extra_info参数提供了一个机会来指定特定于创建的弹出式浏览器的额外信息,这些信息将在渲染进程中传递给CefRenderProcessHandler::OnBrowserCreated()。

返回true就可以禁止创建新窗口。

参考于:https://www.cnblogs.com/sinceret/p/10346171.html

原文地址:https://www.cnblogs.com/tingtaishou/p/14760845.html