From 6ca541d359b5d1d3cd088efc65683cce6bbb35cc Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 30 May 2015 10:37:25 -0400 Subject: [PATCH 01/12] Fixed issue #401. --- qutebrowser/browser/commands.py | 6 ++++++ qutebrowser/config/configdata.py | 1 + 2 files changed, 7 insertions(+) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 0c01260d7..9f836d36a 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -985,6 +985,12 @@ class CommandDispatcher: url = objreg.get('quickmark-manager').get(name) self._open(url, tab, bg, window) + @cmdutils.register(instance='command-dispatcher', name='select-follow', scope='window') + def select_follow(self): + """Follow the selected text.""" + widget = self._current_widget() + widget.page().currentFrame().evaluateJavaScript('window.getSelection().anchorNode.parentNode.click()') + @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') def toggle_inspector(self): diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 02b8c6008..135e91e81 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -1239,6 +1239,7 @@ KEY_DATA = collections.OrderedDict([ ('stop', ['']), ('print', ['']), ('open qute:settings', ['Ss']), + ('select-follow', ['', '']), ])), ('insert', collections.OrderedDict([ From a56a14fb705bf77f35f0e360fc7b8159672c158a Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 30 May 2015 13:15:53 -0400 Subject: [PATCH 02/12] Added the possibility to open a selected link in a new tab. --- qutebrowser/browser/commands.py | 10 ++++++++-- qutebrowser/config/configdata.py | 3 ++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 9f836d36a..bc3931a65 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -986,9 +986,15 @@ class CommandDispatcher: self._open(url, tab, bg, window) @cmdutils.register(instance='command-dispatcher', name='select-follow', scope='window') - def select_follow(self): - """Follow the selected text.""" + def select_follow(self, tab=False): + """Follow the selected text. + + Args: + tab: Load the selected link in a new tab. + """ widget = self._current_widget() + if tab: + widget.page().open_target = usertypes.ClickTarget.tab widget.page().currentFrame().evaluateJavaScript('window.getSelection().anchorNode.parentNode.click()') @cmdutils.register(instance='command-dispatcher', name='inspector', diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 135e91e81..dec02b069 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -1239,7 +1239,8 @@ KEY_DATA = collections.OrderedDict([ ('stop', ['']), ('print', ['']), ('open qute:settings', ['Ss']), - ('select-follow', ['', '']), + ('select-follow', ['']), + ('select-follow -t', ['']), ])), ('insert', collections.OrderedDict([ From 989e3b7291f34ec7f560a1293124aab71ba9aa62 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 30 May 2015 13:56:36 -0400 Subject: [PATCH 03/12] Added a fallback for when JavaScript is disabled. --- qutebrowser/browser/commands.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index bc3931a65..17ef07196 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -25,7 +25,9 @@ import shlex import subprocess import posixpath import functools +from xml.etree import ElementTree +from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWidgets import QApplication, QTabBar from PyQt5.QtCore import Qt, QUrl, QEvent from PyQt5.QtGui import QClipboard, QKeyEvent @@ -985,7 +987,8 @@ class CommandDispatcher: url = objreg.get('quickmark-manager').get(name) self._open(url, tab, bg, window) - @cmdutils.register(instance='command-dispatcher', name='select-follow', scope='window') + @cmdutils.register(instance='command-dispatcher', name='select-follow', + scope='window') def select_follow(self, tab=False): """Follow the selected text. @@ -993,9 +996,19 @@ class CommandDispatcher: tab: Load the selected link in a new tab. """ widget = self._current_widget() - if tab: - widget.page().open_target = usertypes.ClickTarget.tab - widget.page().currentFrame().evaluateJavaScript('window.getSelection().anchorNode.parentNode.click()') + if QWebSettings.globalSettings().testAttribute( + QWebSettings.JavascriptEnabled): + if tab: + widget.page().open_target = usertypes.ClickTarget.tab + widget.page().currentFrame().evaluateJavaScript( + 'window.getSelection().anchorNode.parentNode.click()') + else: + selected_element = ElementTree.fromstring( + '' + widget.selectedHtml() + '').find('a') + if selected_element is not None: + url = selected_element.attrib['href'] + if url: + self._open(QUrl(url), tab) @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') From b1f8a70c02c5af72abff64d00b0a1715d24f378e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sat, 30 May 2015 18:03:39 -0400 Subject: [PATCH 04/12] Added try/except for parse error. --- qutebrowser/browser/commands.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 17ef07196..7a4345176 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1003,12 +1003,15 @@ class CommandDispatcher: widget.page().currentFrame().evaluateJavaScript( 'window.getSelection().anchorNode.parentNode.click()') else: - selected_element = ElementTree.fromstring( - '' + widget.selectedHtml() + '').find('a') - if selected_element is not None: - url = selected_element.attrib['href'] - if url: - self._open(QUrl(url), tab) + try: + selected_element = ElementTree.fromstring( + '' + widget.selectedHtml() + '').find('a') + if selected_element is not None: + url = selected_element.attrib['href'] + if url: + self._open(QUrl(url), tab) + except ElementTree.ParseError: + pass @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') From 4ff9d585ea77d5438fa398e9269afb4ead623e8e Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 11:56:27 -0400 Subject: [PATCH 05/12] Fixed to use qualified import. --- qutebrowser/browser/commands.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 7a4345176..deafedb75 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -25,7 +25,7 @@ import shlex import subprocess import posixpath import functools -from xml.etree import ElementTree +import xml.etree.ElementTree from PyQt5.QtWebKit import QWebSettings from PyQt5.QtWidgets import QApplication, QTabBar @@ -1004,13 +1004,13 @@ class CommandDispatcher: 'window.getSelection().anchorNode.parentNode.click()') else: try: - selected_element = ElementTree.fromstring( + selected_element = xml.etree.ElementTree.fromstring( '' + widget.selectedHtml() + '').find('a') if selected_element is not None: url = selected_element.attrib['href'] if url: self._open(QUrl(url), tab) - except ElementTree.ParseError: + except xml.etree.ElementTree.ParseError: pass @cmdutils.register(instance='command-dispatcher', name='inspector', From c5c145320cf48804a8636037d2c2c52da0b8780c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:02:15 -0400 Subject: [PATCH 06/12] Fixed exception handling in select_follow command. --- qutebrowser/browser/commands.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index deafedb75..d2738560e 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1006,12 +1006,13 @@ class CommandDispatcher: try: selected_element = xml.etree.ElementTree.fromstring( '' + widget.selectedHtml() + '').find('a') - if selected_element is not None: - url = selected_element.attrib['href'] - if url: - self._open(QUrl(url), tab) except xml.etree.ElementTree.ParseError: - pass + raise cmdexc.CommandError('Parse error') + + if selected_element is not None: + url = selected_element.attrib['href'] + if url: + self._open(QUrl(url), tab) @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') From 87e988816777dcaad807d732d438aab63f08bb11 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:07:08 -0400 Subject: [PATCH 07/12] Added exception handling for href attribute. --- qutebrowser/browser/commands.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index d2738560e..748fe7d3a 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1007,12 +1007,14 @@ class CommandDispatcher: selected_element = xml.etree.ElementTree.fromstring( '' + widget.selectedHtml() + '').find('a') except xml.etree.ElementTree.ParseError: - raise cmdexc.CommandError('Parse error') + raise cmdexc.CommandError('Parse error!') if selected_element is not None: - url = selected_element.attrib['href'] - if url: - self._open(QUrl(url), tab) + try: + url = selected_element.attrib['href'] + except KeyError: + raise cmdexc.CommandError('Anchor elment without href!') + self._open(QUrl(url), tab) @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') From 1cd64481de2db6f381204beecc3ca8eab01f976c Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:13:37 -0400 Subject: [PATCH 08/12] Fixed for relative url. --- qutebrowser/browser/commands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 748fe7d3a..ff7a02aad 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1014,7 +1014,8 @@ class CommandDispatcher: url = selected_element.attrib['href'] except KeyError: raise cmdexc.CommandError('Anchor elment without href!') - self._open(QUrl(url), tab) + url = self._current_url().resolved(QUrl(url)) + self._open(url, tab) @cmdutils.register(instance='command-dispatcher', name='inspector', scope='window') From d0eda3336cd9665623a14e0729f19bb86f9840e8 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:18:27 -0400 Subject: [PATCH 09/12] Added a page variable. --- qutebrowser/browser/commands.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index ff7a02aad..36ef67b74 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -996,11 +996,12 @@ class CommandDispatcher: tab: Load the selected link in a new tab. """ widget = self._current_widget() + page = widget.page() if QWebSettings.globalSettings().testAttribute( QWebSettings.JavascriptEnabled): if tab: - widget.page().open_target = usertypes.ClickTarget.tab - widget.page().currentFrame().evaluateJavaScript( + page.open_target = usertypes.ClickTarget.tab + page.currentFrame().evaluateJavaScript( 'window.getSelection().anchorNode.parentNode.click()') else: try: From c0b6aef7748a2aff2f64cf4c7b6b8ac83e434ea1 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:50:28 -0400 Subject: [PATCH 10/12] Fixed command name. --- qutebrowser/browser/commands.py | 4 ++-- qutebrowser/config/configdata.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 36ef67b74..18164cf31 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -987,9 +987,9 @@ class CommandDispatcher: url = objreg.get('quickmark-manager').get(name) self._open(url, tab, bg, window) - @cmdutils.register(instance='command-dispatcher', name='select-follow', + @cmdutils.register(instance='command-dispatcher', hide=True, scope='window') - def select_follow(self, tab=False): + def follow_selected(self, tab=False): """Follow the selected text. Args: diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index dec02b069..1480ddb2e 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -1239,8 +1239,8 @@ KEY_DATA = collections.OrderedDict([ ('stop', ['']), ('print', ['']), ('open qute:settings', ['Ss']), - ('select-follow', ['']), - ('select-follow -t', ['']), + ('follow-selected', ['']), + ('follow-selected -t', ['']), ])), ('insert', collections.OrderedDict([ From 27cbe618f0c8f4b6e74e0abd5ddc3b857239a550 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:53:14 -0400 Subject: [PATCH 11/12] Added hasSelection check before trying to click on a selected link. --- qutebrowser/browser/commands.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 18164cf31..a221ab44e 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -997,6 +997,8 @@ class CommandDispatcher: """ widget = self._current_widget() page = widget.page() + if not page.hasSelection(): + return if QWebSettings.globalSettings().testAttribute( QWebSettings.JavascriptEnabled): if tab: From 3d0721afea3747287e33c6a5351c8fc3b6a441e5 Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Sun, 31 May 2015 12:56:08 -0400 Subject: [PATCH 12/12] Fixed error messages. --- qutebrowser/browser/commands.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index a221ab44e..0f09baddf 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1010,13 +1010,13 @@ class CommandDispatcher: selected_element = xml.etree.ElementTree.fromstring( '' + widget.selectedHtml() + '').find('a') except xml.etree.ElementTree.ParseError: - raise cmdexc.CommandError('Parse error!') + raise cmdexc.CommandError('Could not parse selected element!') if selected_element is not None: try: url = selected_element.attrib['href'] except KeyError: - raise cmdexc.CommandError('Anchor elment without href!') + raise cmdexc.CommandError('Anchor element without href!') url = self._current_url().resolved(QUrl(url)) self._open(url, tab)