Implement user stylesheets for QtWebEngine

This commit is contained in:
Florian Bruhin 2016-11-15 23:17:27 +01:00
parent fcb955458c
commit d7d270ea28
3 changed files with 43 additions and 7 deletions

View File

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

View File

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

View File

@ -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',