From 5f58ebebbf06e4c589062851fb17378fedd6f0d3 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 5 Sep 2016 18:11:01 +0200 Subject: [PATCH] Remove needs_js for @cmdutils.register This gets rid of a QtWebKit import in commands.py, and also makes those checks work later when we have per-domain settings. --- qutebrowser/browser/browsertab.py | 4 ++++ qutebrowser/browser/commands.py | 7 ++++--- qutebrowser/browser/webengine/webenginetab.py | 4 ++++ qutebrowser/browser/webkit/webkittab.py | 4 ++++ qutebrowser/commands/command.py | 16 ++++------------ 5 files changed, 20 insertions(+), 15 deletions(-) diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index a704c325d..614428cb0 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -715,6 +715,10 @@ class AbstractTab(QWidget): """ raise NotImplementedError + def has_js(self): + """Check if qutebrowser can run javascript in this tab.""" + raise NotImplementedError + def shutdown(self): raise NotImplementedError diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 0b368a474..e2a126d58 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1496,7 +1496,7 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', deprecated="Use :insert-text {primary}", modes=[KeyMode.insert], hide=True, scope='window', - needs_js=True, backend=usertypes.Backend.QtWebKit) + backend=usertypes.Backend.QtWebKit) def paste_primary(self): """Paste the primary selection at cursor position.""" try: @@ -1505,8 +1505,7 @@ class CommandDispatcher: self.insert_text(utils.get_clipboard()) @cmdutils.register(instance='command-dispatcher', maxsplit=0, - scope='window', needs_js=True, - backend=usertypes.Backend.QtWebKit) + scope='window', backend=usertypes.Backend.QtWebKit) def insert_text(self, text): """Insert text at cursor position. @@ -1515,6 +1514,8 @@ class CommandDispatcher: """ # FIXME:qtwebengine have a proper API for this tab = self._current_widget() + if not tab.has_js(): + raise cmdexc.CommandError("This command needs javascript enabled.") page = tab._widget.page() # pylint: disable=protected-access try: elem = webkitelem.focus_elem(page.currentFrame()) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 97f810e67..01ad4c406 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -458,6 +458,10 @@ class WebEngineTab(browsertab.AbstractTab): else: self._widget.page().runJavaScript(code, callback) + def has_js(self): + # QtWebEngine can run JS even if the page can't + return True + def shutdown(self): log.stub() diff --git a/qutebrowser/browser/webkit/webkittab.py b/qutebrowser/browser/webkit/webkittab.py index 12f32de97..b4973ad48 100644 --- a/qutebrowser/browser/webkit/webkittab.py +++ b/qutebrowser/browser/webkit/webkittab.py @@ -618,6 +618,10 @@ class WebKitTab(browsertab.AbstractTab): if callback is not None: callback(result) + def has_js(self): + settings = QWebSettings.globalSettings() + return settings.testAttribute(QWebSettings.JavascriptEnabled) + def icon(self): return self._widget.icon() diff --git a/qutebrowser/commands/command.py b/qutebrowser/commands/command.py index 0d9602ca2..1d28bde2a 100644 --- a/qutebrowser/commands/command.py +++ b/qutebrowser/commands/command.py @@ -23,8 +23,6 @@ import inspect import collections import traceback -from PyQt5.QtWebKit import QWebSettings - from qutebrowser.commands import cmdexc, argparser from qutebrowser.utils import (log, utils, message, docutils, objreg, usertypes, typing) @@ -83,7 +81,6 @@ class Command: both) no_replace_variables: Don't replace variables like {url} _qute_args: The saved data from @cmdutils.argument - _needs_js: Whether the command needs javascript enabled _modes: The modes the command can be executed in. _not_modes: The modes the command can not be executed in. _count: The count set for the command. @@ -92,10 +89,10 @@ class Command: """ def __init__(self, *, handler, name, instance=None, maxsplit=None, - hide=False, modes=None, not_modes=None, needs_js=False, - debug=False, ignore_args=False, deprecated=False, - no_cmd_split=False, star_args_optional=False, scope='global', - backend=None, no_replace_variables=False): + hide=False, modes=None, not_modes=None, debug=False, + ignore_args=False, deprecated=False, no_cmd_split=False, + star_args_optional=False, scope='global', backend=None, + no_replace_variables=False): # I really don't know how to solve this in a better way, I tried. # pylint: disable=too-many-locals if modes is not None and not_modes is not None: @@ -120,7 +117,6 @@ class Command: self._modes = modes self._not_modes = not_modes self._scope = scope - self._needs_js = needs_js self._star_args_optional = star_args_optional self.debug = debug self.ignore_args = ignore_args @@ -171,10 +167,6 @@ class Command: "{}: This command is not allowed in {} mode.".format( self.name, mode_names)) - if self._needs_js and not QWebSettings.globalSettings().testAttribute( - QWebSettings.JavascriptEnabled): - raise cmdexc.PrerequisitesError( - "{}: This command needs javascript enabled.".format(self.name)) used_backend = usertypes.arg2backend[objreg.get('args').backend] if self.backend is not None and used_backend != self.backend: raise cmdexc.PrerequisitesError(