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.
This commit is contained in:
Florian Bruhin 2016-09-05 18:11:01 +02:00
parent af40abd3b2
commit 5f58ebebbf
5 changed files with 20 additions and 15 deletions

View File

@ -715,6 +715,10 @@ class AbstractTab(QWidget):
""" """
raise NotImplementedError raise NotImplementedError
def has_js(self):
"""Check if qutebrowser can run javascript in this tab."""
raise NotImplementedError
def shutdown(self): def shutdown(self):
raise NotImplementedError raise NotImplementedError

View File

@ -1496,7 +1496,7 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', @cmdutils.register(instance='command-dispatcher',
deprecated="Use :insert-text {primary}", deprecated="Use :insert-text {primary}",
modes=[KeyMode.insert], hide=True, scope='window', modes=[KeyMode.insert], hide=True, scope='window',
needs_js=True, backend=usertypes.Backend.QtWebKit) backend=usertypes.Backend.QtWebKit)
def paste_primary(self): def paste_primary(self):
"""Paste the primary selection at cursor position.""" """Paste the primary selection at cursor position."""
try: try:
@ -1505,8 +1505,7 @@ class CommandDispatcher:
self.insert_text(utils.get_clipboard()) self.insert_text(utils.get_clipboard())
@cmdutils.register(instance='command-dispatcher', maxsplit=0, @cmdutils.register(instance='command-dispatcher', maxsplit=0,
scope='window', needs_js=True, scope='window', backend=usertypes.Backend.QtWebKit)
backend=usertypes.Backend.QtWebKit)
def insert_text(self, text): def insert_text(self, text):
"""Insert text at cursor position. """Insert text at cursor position.
@ -1515,6 +1514,8 @@ class CommandDispatcher:
""" """
# FIXME:qtwebengine have a proper API for this # FIXME:qtwebengine have a proper API for this
tab = self._current_widget() 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 page = tab._widget.page() # pylint: disable=protected-access
try: try:
elem = webkitelem.focus_elem(page.currentFrame()) elem = webkitelem.focus_elem(page.currentFrame())

View File

@ -458,6 +458,10 @@ class WebEngineTab(browsertab.AbstractTab):
else: else:
self._widget.page().runJavaScript(code, callback) 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): def shutdown(self):
log.stub() log.stub()

View File

@ -618,6 +618,10 @@ class WebKitTab(browsertab.AbstractTab):
if callback is not None: if callback is not None:
callback(result) callback(result)
def has_js(self):
settings = QWebSettings.globalSettings()
return settings.testAttribute(QWebSettings.JavascriptEnabled)
def icon(self): def icon(self):
return self._widget.icon() return self._widget.icon()

View File

@ -23,8 +23,6 @@ import inspect
import collections import collections
import traceback import traceback
from PyQt5.QtWebKit import QWebSettings
from qutebrowser.commands import cmdexc, argparser from qutebrowser.commands import cmdexc, argparser
from qutebrowser.utils import (log, utils, message, docutils, objreg, from qutebrowser.utils import (log, utils, message, docutils, objreg,
usertypes, typing) usertypes, typing)
@ -83,7 +81,6 @@ class Command:
both) both)
no_replace_variables: Don't replace variables like {url} no_replace_variables: Don't replace variables like {url}
_qute_args: The saved data from @cmdutils.argument _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. _modes: The modes the command can be executed in.
_not_modes: The modes the command can not be executed in. _not_modes: The modes the command can not be executed in.
_count: The count set for the command. _count: The count set for the command.
@ -92,10 +89,10 @@ class Command:
""" """
def __init__(self, *, handler, name, instance=None, maxsplit=None, def __init__(self, *, handler, name, instance=None, maxsplit=None,
hide=False, modes=None, not_modes=None, needs_js=False, hide=False, modes=None, not_modes=None, debug=False,
debug=False, ignore_args=False, deprecated=False, ignore_args=False, deprecated=False, no_cmd_split=False,
no_cmd_split=False, star_args_optional=False, scope='global', star_args_optional=False, scope='global', backend=None,
backend=None, no_replace_variables=False): no_replace_variables=False):
# I really don't know how to solve this in a better way, I tried. # I really don't know how to solve this in a better way, I tried.
# pylint: disable=too-many-locals # pylint: disable=too-many-locals
if modes is not None and not_modes is not None: if modes is not None and not_modes is not None:
@ -120,7 +117,6 @@ class Command:
self._modes = modes self._modes = modes
self._not_modes = not_modes self._not_modes = not_modes
self._scope = scope self._scope = scope
self._needs_js = needs_js
self._star_args_optional = star_args_optional self._star_args_optional = star_args_optional
self.debug = debug self.debug = debug
self.ignore_args = ignore_args self.ignore_args = ignore_args
@ -171,10 +167,6 @@ class Command:
"{}: This command is not allowed in {} mode.".format( "{}: This command is not allowed in {} mode.".format(
self.name, mode_names)) 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] used_backend = usertypes.arg2backend[objreg.get('args').backend]
if self.backend is not None and used_backend != self.backend: if self.backend is not None and used_backend != self.backend:
raise cmdexc.PrerequisitesError( raise cmdexc.PrerequisitesError(