Make qute://settings work with QtWebEngine and new config

Fixes #2332
This commit is contained in:
Florian Bruhin 2017-06-15 16:05:23 +02:00
parent fede64ba7a
commit ffc29ee043
6 changed files with 48 additions and 63 deletions

View File

@ -35,7 +35,7 @@ import pkg_resources
from PyQt5.QtCore import QUrlQuery, QUrl
import qutebrowser
from qutebrowser.config import config
from qutebrowser.config import config, configdata, configexc
from qutebrowser.utils import (version, utils, jinja, log, message, docutils,
objreg, usertypes, qtutils)
from qutebrowser.misc import objects
@ -441,3 +441,37 @@ def qute_backend_warning(_url):
version=pkg_resources.parse_version,
title="Legacy backend warning")
return 'text/html', html
def _qute_settings_set(url):
"""Handler for qute://settings/set."""
query = QUrlQuery(url)
option = query.queryItemValue('option', QUrl.FullyDecoded)
value = query.queryItemValue('value', QUrl.FullyDecoded)
# https://github.com/qutebrowser/qutebrowser/issues/727
if option == 'content.javascript.enabled' and value == 'false':
msg = ("Refusing to disable javascript via qute://settings "
"as it needs javascript support.")
message.error(msg)
return 'text/html', b'error: ' + msg.encode('utf-8')
try:
config.instance.set(option, value)
return 'text/html', b'ok'
except configexc.Error as e:
message.error(str(e))
return 'text/html', b'error: ' + str(e).encode('utf-8')
@add_handler('settings')
def qute_settings(url):
"""Handler for qute://settings. View/change qute configuration."""
# FIXME:conf add a test for this
if url.path() == '/set':
return _qute_settings_set(url)
config_getter = config.instance.get # FIXME to_str
html = jinja.render('settings.html', title='settings',
configdata=configdata, confget=config_getter)
return 'text/html', html

View File

@ -20,16 +20,13 @@
"""QtWebKit specific qute://* handlers and glue code."""
import mimetypes
import functools
import configparser
from PyQt5.QtCore import pyqtSlot, QObject
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtNetwork import QNetworkReply
from qutebrowser.browser import pdfjs, qutescheme
from qutebrowser.browser.webkit.network import schemehandler, networkreply
from qutebrowser.utils import jinja, log, message, objreg, usertypes, qtutils
from qutebrowser.config import configexc, configdata, config
from qutebrowser.utils import jinja, log, message, usertypes, qtutils
class QuteSchemeHandler(schemehandler.SchemeHandler):
@ -70,36 +67,6 @@ class QuteSchemeHandler(schemehandler.SchemeHandler):
self.parent())
class JSBridge(QObject):
"""Javascript-bridge for special qute://... pages."""
@pyqtSlot(str, str, str)
def set(self, sectname, optname, value):
"""Slot to set a setting from qute://settings."""
# https://github.com/qutebrowser/qutebrowser/issues/727
if ((sectname, optname) == ('content', 'allow-javascript') and
value == 'false'):
message.error("Refusing to disable javascript via qute://settings "
"as it needs javascript support.")
return
# FIXME:conf
message.error("Setting doesn't work yet!")
# try:
# objreg.get('config').set('conf', sectname, optname, value)
# except (configexc.Error, configparser.Error) as e:
# message.error(str(e))
@qutescheme.add_handler('settings', backend=usertypes.Backend.QtWebKit)
def qute_settings(_url):
"""Handler for qute://settings. View/change qute configuration."""
config_getter = config.instance.get # FIXME to_str
html = jinja.render('settings.html', title='settings',
configdata=configdata, confget=config_getter)
return 'text/html', html
@qutescheme.add_handler('pdfjs', backend=usertypes.Backend.QtWebKit)
def qute_pdfjs(url):
"""Handler for qute://pdfjs. Return the pdf.js viewer."""

View File

@ -35,23 +35,16 @@ from PyQt5.QtPrintSupport import QPrinter
from qutebrowser.browser import browsertab
from qutebrowser.browser.network import proxy
from qutebrowser.browser.webkit import webview, tabhistory, webkitelem
from qutebrowser.browser.webkit.network import webkitqutescheme
from qutebrowser.utils import qtutils, objreg, usertypes, utils, log, debug
def init():
"""Initialize QtWebKit-specific modules."""
qapp = QApplication.instance()
if not qtutils.version_check('5.8'):
# Otherwise we initialize it globally in app.py
log.init.debug("Initializing proxy...")
proxy.init()
log.init.debug("Initializing js-bridge...")
js_bridge = webkitqutescheme.JSBridge(qapp)
objreg.register('js-bridge', js_bridge)
class WebKitAction(browsertab.AbstractAction):

View File

@ -135,22 +135,6 @@ class WebView(QWebView):
url: The URL to load as QUrl
"""
self.load(url)
if url.scheme() == 'qute':
frame = self.page().mainFrame()
frame.javaScriptWindowObjectCleared.connect(self.add_js_bridge)
@pyqtSlot()
def add_js_bridge(self):
"""Add the javascript bridge for qute://... pages."""
frame = self.sender()
if not isinstance(frame, QWebFrame):
log.webview.error("Got non-QWebFrame {!r} in "
"add_js_bridge!".format(frame))
return
if frame.url().scheme() == 'qute':
bridge = objreg.get('js-bridge')
frame.addToJavaScriptWindowObject('qute', bridge)
@pyqtSlot(usertypes.KeyMode)
def on_mode_entered(self, mode):

View File

@ -131,6 +131,9 @@ class NewConfigManager(QObject):
raise configexc.NoOptionError(option)
return value.typ.from_py(value.default)
def set(self, option, value):
raise configexc.Error("Setting doesn't work yet!")
class ConfigContainer:

View File

@ -1,9 +1,13 @@
{% extends "base.html" %}
{% block script %}
var cset = function(section, option, el) {
value = el.value;
window.qute.set(section, option, value);
var cset = function(option, value) {
// FIXME:conf we might want some error handling here?
var url = "qute://settings/set?option=" + encodeURIComponent(option);
url += "&value=" + encodeURIComponent(value);
var xhr = new XMLHttpRequest();
xhr.open("GET", url);
xhr.send();
}
{% endblock %}
@ -32,7 +36,7 @@ th pre { color: grey; text-align: left; }
</td>
<td>
<input type="text"
onblur="cset('{{ option.name }}', this)"
onblur="cset('{{ option.name }}', this.value)"
value="{{ confget(option.name) }}">
</input>
</td>