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
def has_js(self):
"""Check if qutebrowser can run javascript in this tab."""
raise NotImplementedError
def shutdown(self):
raise NotImplementedError

View File

@ -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())

View File

@ -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()

View File

@ -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()

View File

@ -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(