def add(self, url, title, *, toggle=False): """Add a new bookmark. Args: url: The url to add as bookmark. title: The title for the new bookmark. toggle: remove the bookmark instead of raising an error if it already exists. Return: True if the bookmark was added, and False if it was removed (only possible if toggle is True). """ if not url.isValid(): errstr = urlutils.get_errstring(url) raise InvalidUrlError(errstr) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) if urlstr in self.marks: if toggle: self.delete(urlstr) return False else: raise AlreadyExistsError("Bookmark already exists!") else: self.marks[urlstr] = title self.changed.emit() return True
def _on_navigation_request( self, navigation: usertypes.NavigationRequest ) -> None: """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if not navigation.url.isValid(): # Also a WORKAROUND for missing IDNA 2008 support in QUrl, see # https://bugreports.qt.io/browse/QTBUG-60364 if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def add(self, url, title, *, toggle=False): """Add a new bookmark. Args: url: The url to add as bookmark. title: The title for the new bookmark. toggle: remove the bookmark instead of raising an error if it already exists. Return: True if the bookmark was added, and False if it was removed (only possible if toggle is True). """ if not url.isValid(): errstr = urlutils.get_errstring(url) raise InvalidUrlError(errstr) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) if urlstr in self.marks: if toggle: del self.marks[urlstr] return False else: raise AlreadyExistsError("Bookmark already exists!") else: self.marks[urlstr] = title self.changed.emit() return True
def _on_link_clicked(self, url): log.webview.debug("link clicked: url {}, hint target {}, " "open_target {}".format( url.toDisplayString(), self.data.hint_target, self.data.open_target)) if not url.isValid(): msg = urlutils.get_errstring(url, "Invalid link clicked") message.error(self.win_id, msg) self.data.open_target = usertypes.ClickTarget.normal return False target = self.data.combined_target() if target == usertypes.ClickTarget.normal: return elif target == usertypes.ClickTarget.tab: win_id = self.win_id bg_tab = False elif target == usertypes.ClickTarget.tab_bg: win_id = self.win_id bg_tab = True elif target == usertypes.ClickTarget.window: from qutebrowser.mainwindow import mainwindow window = mainwindow.MainWindow() window.show() win_id = window.win_id bg_tab = False else: raise ValueError("Invalid ClickTarget {}".format(target)) tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) tabbed_browser.tabopen(url, background=bg_tab) self.data.open_target = usertypes.ClickTarget.normal
def _on_navigation_request( self, navigation: usertypes.NavigationRequest ) -> None: """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, utils.pyenum_str(navigation.navigation_type), navigation.is_main_frame)) if navigation.is_main_frame: self.data.last_navigation = navigation if not navigation.url.isValid(): if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def _on_navigation_request( self, navigation: usertypes.NavigationRequest) -> None: """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if navigation.is_main_frame: self.data.last_navigation = navigation if not navigation.url.isValid(): # Also a WORKAROUND for missing IDNA 2008 support in QUrl, see # https://bugreports.qt.io/browse/QTBUG-60364 if navigation.navigation_type == navigation.Type.link_clicked: msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal log.webview.debug("Ignoring invalid URL {} in " "acceptNavigationRequest: {}".format( navigation.url.toDisplayString(), navigation.url.errorString())) navigation.accepted = False
def acceptNavigationRequest(self, _frame, request, typ): """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 opens the URL. Args: _frame: QWebFrame (target frame) request: QNetworkRequest typ: QWebPage::NavigationType """ url = request.url() urlstr = url.toDisplayString() if typ == QWebPage.NavigationTypeReload: self.reloading.emit(url) if typ != QWebPage.NavigationTypeLinkClicked: return True if not url.isValid(): msg = urlutils.get_errstring(url, "Invalid link clicked") message.error(self._win_id, msg) self.open_target = usertypes.ClickTarget.normal return False tabbed_browser = objreg.get('tabbed-browser', scope='window', window=self._win_id) log.webview.debug("acceptNavigationRequest, url {}, type {}, hint " "target {}, open_target {}".format( urlstr, debug.qenum_key(QWebPage, typ), self._hint_target, self.open_target)) if self._hint_target is not None: target = self._hint_target else: target = self.open_target self.open_target = usertypes.ClickTarget.normal if target == usertypes.ClickTarget.tab: tabbed_browser.tabopen(url, False) return False elif target == usertypes.ClickTarget.tab_bg: tabbed_browser.tabopen(url, True) return False elif target == usertypes.ClickTarget.window: from qutebrowser.mainwindow import mainwindow window = mainwindow.MainWindow() window.show() tabbed_browser = objreg.get('tabbed-browser', scope='window', window=window.win_id) tabbed_browser.tabopen(url, False) return False else: return True
def _on_navigation_request(self, navigation): """Handle common acceptNavigationRequest code.""" url = utils.elide(navigation.url.toDisplayString(), 100) log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url, navigation.navigation_type, navigation.is_main_frame)) if (navigation.navigation_type == navigation.Type.link_clicked and not navigation.url.isValid()): msg = urlutils.get_errstring(navigation.url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal navigation.accepted = False
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 acceptNavigationRequest(self, url: QUrl, typ: QWebEnginePage.NavigationType, is_main_frame: bool): """Override acceptNavigationRequest to handle clicked links. This only show an error on invalid links - everything else is handled in createWindow. """ log.webview.debug("navigation request: url {}, type {}, is_main_frame " "{}".format(url.toDisplayString(), debug.qenum_key(QWebEnginePage, typ), is_main_frame)) if (typ == QWebEnginePage.NavigationTypeLinkClicked and not url.isValid()): msg = urlutils.get_errstring(url, "Invalid link clicked") message.error(msg) return False return True
def add(self, url, title): """Add a new bookmark. Args: url: The url to add as bookmark. title: The title for the new bookmark. """ if not url.isValid(): errstr = urlutils.get_errstring(url) raise InvalidUrlError(errstr) urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded) if urlstr in self.marks: raise AlreadyExistsError("Bookmark already exists!") else: self.marks[urlstr] = title self.changed.emit() self.added.emit(title, urlstr)
def _on_link_clicked(self, url): log.webview.debug("link clicked: url {}, override target {}, " "open_target {}".format( url.toDisplayString(), self.data.override_target, self.data.open_target)) if not url.isValid(): msg = urlutils.get_errstring(url, "Invalid link clicked") message.error(msg) self.data.open_target = usertypes.ClickTarget.normal return False target = self.data.combined_target() if target == usertypes.ClickTarget.normal: return elif target == usertypes.ClickTarget.tab: win_id = self.win_id bg_tab = False elif target == usertypes.ClickTarget.tab_bg: win_id = self.win_id bg_tab = True elif target == usertypes.ClickTarget.window: from qutebrowser.mainwindow import mainwindow window = mainwindow.MainWindow() window.show() win_id = window.win_id bg_tab = False else: raise ValueError("Invalid ClickTarget {}".format(target)) tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) tabbed_browser.tabopen(url, background=bg_tab) self.data.open_target = usertypes.ClickTarget.normal