def createWindow(self, wintype): """Called by Qt when a page wants to create a new window. This function is called from the createWindow() method of the associated QWebEnginePage, each time the page wants to create a new window of the given type. This might be the result, for example, of a JavaScript request to open a document in a new window. Args: wintype: This enum describes the types of window that can be created by the createWindow() function. QWebEnginePage::WebBrowserWindow: A complete web browser window. QWebEnginePage::WebBrowserTab: A web browser tab. QWebEnginePage::WebDialog: A window without decoration. QWebEnginePage::WebBrowserBackgroundTab: A web browser tab without hiding the current visible WebEngineView. Return: The new QWebEngineView object. """ debug_type = debug.qenum_key(QWebEnginePage, wintype) background_tabs = config.get('tabs', 'background-tabs') log.webview.debug("createWindow with type {}, background_tabs " "{}".format(debug_type, background_tabs)) if wintype == QWebEnginePage.WebBrowserWindow: # Shift-Alt-Click target = usertypes.ClickTarget.window elif wintype == QWebEnginePage.WebDialog: log.webview.warning("{} requested, but we don't support " "that!".format(debug_type)) target = usertypes.ClickTarget.tab elif wintype == QWebEnginePage.WebBrowserTab: # Middle-click / Ctrl-Click with Shift # FIXME:qtwebengine this also affects target=_blank links... if background_tabs: target = usertypes.ClickTarget.tab else: target = usertypes.ClickTarget.tab_bg elif wintype == QWebEnginePage.WebBrowserBackgroundTab: # Middle-click / Ctrl-Click if background_tabs: target = usertypes.ClickTarget.tab_bg else: target = usertypes.ClickTarget.tab else: raise ValueError("Invalid wintype {}".format(debug_type)) tab = shared.get_tab(self._win_id, target) return tab._widget # pylint: disable=protected-access
def acceptNavigationRequest(self, _frame: QWebFrame, request: QNetworkRequest, typ: QWebPage.NavigationType): """Override acceptNavigationRequest to handle clicked links. Setting linkDelegationPolicy to DelegateAllLinks and using a slot bound to linkClicked won't work correctly, because when in a frameset, we have no idea in which frame the link should be opened. Checks if it should open it in a tab (middle-click or control) or not, and then conditionally opens the URL here or in another tab/window. """ url = request.url() log.webview.debug("navigation request: url {}, type {}, " "target {} override {}".format( url.toDisplayString(), debug.qenum_key(QWebPage, typ), self.open_target, self._tabdata.override_target)) if self._tabdata.override_target is not None: target = self._tabdata.override_target self._tabdata.override_target = None else: target = self.open_target if typ == QWebPage.NavigationTypeReload: self.reloading.emit(url) return True elif typ != QWebPage.NavigationTypeLinkClicked: return True if not url.isValid(): msg = urlutils.get_errstring(url, "Invalid link clicked") message.error(msg) self.open_target = usertypes.ClickTarget.normal return False if target == usertypes.ClickTarget.normal: return True tab = shared.get_tab(self._win_id, target) tab.openurl(url) self.open_target = usertypes.ClickTarget.normal return False
def _on_navigation_request(self, navigation): super()._on_navigation_request(navigation) if not navigation.accepted: return log.webview.debug("target {} override {}".format( self.data.open_target, self.data.override_target)) if self.data.override_target is not None: target = self.data.override_target self.data.override_target = None else: target = self.data.open_target if (navigation.navigation_type == navigation.Type.link_clicked and target != usertypes.ClickTarget.normal): tab = shared.get_tab(self.win_id, target) tab.openurl(navigation.url) self.data.open_target = usertypes.ClickTarget.normal navigation.accepted = False if navigation.is_main_frame: self.settings.update_for_url(navigation.url)
def createWindow(self, wintype): """Called by Qt when a page wants to create a new window. This function is called from the createWindow() method of the associated QWebEnginePage, each time the page wants to create a new window of the given type. This might be the result, for example, of a JavaScript request to open a document in a new window. Args: wintype: This enum describes the types of window that can be created by the createWindow() function. QWebEnginePage::WebBrowserWindow: A complete web browser window. QWebEnginePage::WebBrowserTab: A web browser tab. QWebEnginePage::WebDialog: A window without decoration. QWebEnginePage::WebBrowserBackgroundTab: A web browser tab without hiding the current visible WebEngineView. (Added in Qt 5.7) Return: The new QWebEngineView object. """ debug_type = debug.qenum_key(QWebEnginePage, wintype) background_tabs = config.get('tabs', 'background-tabs') log.webview.debug("createWindow with type {}, background_tabs " "{}".format(debug_type, background_tabs)) try: background_tab_wintype = QWebEnginePage.WebBrowserBackgroundTab except AttributeError: # This is unavailable with an older PyQt, but we still might get # this with a newer Qt... background_tab_wintype = 0x0003 if wintype == QWebEnginePage.WebBrowserWindow: # Shift-Alt-Click target = usertypes.ClickTarget.window elif wintype == QWebEnginePage.WebDialog: log.webview.warning("{} requested, but we don't support " "that!".format(debug_type)) target = usertypes.ClickTarget.tab elif wintype == QWebEnginePage.WebBrowserTab: # Middle-click / Ctrl-Click with Shift # FIXME:qtwebengine this also affects target=_blank links... if background_tabs: target = usertypes.ClickTarget.tab else: target = usertypes.ClickTarget.tab_bg elif wintype == background_tab_wintype: # Middle-click / Ctrl-Click if background_tabs: target = usertypes.ClickTarget.tab_bg else: target = usertypes.ClickTarget.tab else: raise ValueError("Invalid wintype {}".format(debug_type)) tab = shared.get_tab(self._win_id, target) # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-54419 vercheck = qtutils.version_check qtbug54419_fixed = ((vercheck('5.6.2') and not vercheck('5.7.0')) or qtutils.version_check('5.7.1') or os.environ.get('QUTE_QTBUG54419_PATCHED', '')) if not qtbug54419_fixed: tab.needs_qtbug54419_workaround = True return tab._widget # pylint: disable=protected-access