From d7d270ea284c66cf27e816df6a5907d09879ad63 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 15 Nov 2016 23:17:27 +0100 Subject: [PATCH] Implement user stylesheets for QtWebEngine --- .../browser/webengine/webenginesettings.py | 43 ++++++++++++++++++- qutebrowser/browser/webengine/webenginetab.py | 1 - qutebrowser/config/configdata.py | 6 +-- 3 files changed, 43 insertions(+), 7 deletions(-) diff --git a/qutebrowser/browser/webengine/webenginesettings.py b/qutebrowser/browser/webengine/webenginesettings.py index ec54e0044..1568abad5 100644 --- a/qutebrowser/browser/webengine/webenginesettings.py +++ b/qutebrowser/browser/webengine/webenginesettings.py @@ -27,11 +27,13 @@ Module attributes: import os # pylint: disable=no-name-in-module,import-error,useless-suppression -from PyQt5.QtWebEngineWidgets import QWebEngineSettings, QWebEngineProfile +from PyQt5.QtWebEngineWidgets import (QWebEngineSettings, QWebEngineProfile, + QWebEngineScript) # pylint: enable=no-name-in-module,import-error,useless-suppression +from qutebrowser.browser import shared from qutebrowser.config import websettings, config -from qutebrowser.utils import objreg, utils, standarddir +from qutebrowser.utils import objreg, utils, standarddir, javascript class Attribute(websettings.Attribute): @@ -63,9 +65,45 @@ class StaticSetter(websettings.StaticSetter): GLOBAL_SETTINGS = QWebEngineSettings.globalSettings +def _init_stylesheet(profile): + """Initialize custom stylesheets. + + Mostly inspired by QupZilla: + https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/app/mainapplication.cpp#L1063-L1101 + https://github.com/QupZilla/qupzilla/blob/v2.0/src/lib/tools/scripts.cpp#L119-L132 + + FIXME:qtwebengine Use QWebEngineStyleSheet once that's available + https://codereview.qt-project.org/#/c/148671/ + """ + old_script = profile.scripts().findScript('_qute_stylesheet') + if not old_script.isNull(): + profile.scripts().remove(old_script) + + css = shared.get_user_stylesheet() + source = """ + (function() {{ + var css = document.createElement('style'); + css.setAttribute('type', 'text/css'); + css.appendChild(document.createTextNode('{}')); + document.getElementsByTagName('head')[0].appendChild(css); + }})() + """.format(javascript.string_escape(css)) + + script = QWebEngineScript() + script.setName('_qute_stylesheet') + script.setInjectionPoint(QWebEngineScript.DocumentReady) + script.setWorldId(QWebEngineScript.ApplicationWorld) + script.setRunsOnSubFrames(True) + script.setSourceCode(source) + profile.scripts().insert(script) + + def update_settings(section, option): """Update global settings when qwebsettings changed.""" websettings.update_mappings(MAPPINGS, section, option) + profile = QWebEngineProfile.defaultProfile() + if section == 'ui' and option in ['hide-scrollbar', 'user-stylesheet']: + _init_stylesheet(profile) def init(): @@ -78,6 +116,7 @@ def init(): profile.setCachePath(os.path.join(standarddir.cache(), 'webengine')) profile.setPersistentStoragePath( os.path.join(standarddir.data(), 'webengine')) + _init_stylesheet(profile) websettings.init_mappings(MAPPINGS) objreg.get('config').changed.connect(update_settings) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 66aaf733d..52a22f9d0 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -469,7 +469,6 @@ class WebEngineTab(browsertab.AbstractTab): self._set_widget(widget) self._connect_signals() self.backend = usertypes.Backend.QtWebEngine - # init js stuff self._init_js() self._child_event_filter = None self.needs_qtbug54419_workaround = False diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index ebc89145c..74480bdfd 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -323,14 +323,12 @@ def data(readonly=False): "page."), ('user-stylesheet', - SettingValue(typ.File(none_ok=True), '', - backends=[usertypes.Backend.QtWebKit]), + SettingValue(typ.File(none_ok=True), ''), "User stylesheet to use (absolute filename or filename relative to " "the config directory). Will expand environment variables."), ('hide-scrollbar', - SettingValue(typ.Bool(), 'true', - backends=[usertypes.Backend.QtWebKit]), + SettingValue(typ.Bool(), 'true'), "Hide the main scrollbar."), ('css-media-type',