Only update stylesheet if it's overridden per-domain

This commit is contained in:
Florian Bruhin 2018-06-14 13:24:50 +02:00
parent 4f665784f2
commit 6a00877a71
2 changed files with 25 additions and 9 deletions

View File

@ -25,7 +25,7 @@ import netrc
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.config import config from qutebrowser.config import config, configutils
from qutebrowser.utils import usertypes, message, log, objreg, jinja, utils from qutebrowser.utils import usertypes, message, log, objreg, jinja, utils
from qutebrowser.mainwindow import mainwindow from qutebrowser.mainwindow import mainwindow
@ -274,9 +274,16 @@ def get_tab(win_id, target):
def get_user_stylesheet(url=None): def get_user_stylesheet(url=None):
"""Get the combined user-stylesheet.""" """Get the combined user-stylesheet.
If `url` is given and there's no overridden stylesheet, return
`configutils.UNSET`.
"""
css = '' css = ''
stylesheets = config.instance.get("content.user_stylesheets", url) stylesheets = config.instance.get("content.user_stylesheets", url,
fallback=url is None)
if stylesheets is configutils.UNSET:
return stylesheets
for filename in stylesheets: for filename in stylesheets:
with open(filename, 'r', encoding='utf-8') as f: with open(filename, 'r', encoding='utf-8') as f:

View File

@ -32,7 +32,7 @@ from PyQt5.QtNetwork import QAuthenticator
from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets import QApplication
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineScript from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineScript
from qutebrowser.config import configdata, config from qutebrowser.config import configdata, config, configutils
from qutebrowser.browser import browsertab, mouse, shared from qutebrowser.browser import browsertab, mouse, shared
from qutebrowser.browser.webengine import (webview, webengineelem, tabhistory, from qutebrowser.browser.webengine import (webview, webengineelem, tabhistory,
interceptor, webenginequtescheme, interceptor, webenginequtescheme,
@ -796,7 +796,7 @@ class _WebEngineScripts(QObject):
@pyqtSlot(str) @pyqtSlot(str)
def _on_config_changed(self, option): def _on_config_changed(self, option):
if option in ['scrolling.bar', 'content.user_stylesheets']: if option in ['scrolling.bar', 'content.user_stylesheets']:
self._update_stylesheet(url=self._tab.url()) self._update_stylesheet(url=self._tab.url(), force=True)
@pyqtSlot() @pyqtSlot()
def _on_load_finished(self): def _on_load_finished(self):
@ -804,11 +804,20 @@ class _WebEngineScripts(QObject):
self._update_stylesheet(url) self._update_stylesheet(url)
@pyqtSlot(QUrl) @pyqtSlot(QUrl)
def _update_stylesheet(self, url): def _update_stylesheet(self, url, force=False):
"""Update the custom stylesheet in existing tabs.""" """Update the custom stylesheet in existing tabs.
Arguments:
url: The url to get the stylesheet for.
force: Also update the global stylesheet.
"""
css = shared.get_user_stylesheet(url=url) css = shared.get_user_stylesheet(url=url)
code = javascript.assemble('stylesheet', 'set_css', css) if css is configutils.UNSET and force:
self._tab.run_js_async(code) css = shared.get_user_stylesheet(url=None)
if css is not configutils.UNSET:
code = javascript.assemble('stylesheet', 'set_css', css)
self._tab.run_js_async(code)
def _inject_early_js(self, name, js_code, *, def _inject_early_js(self, name, js_code, *,
world=QWebEngineScript.ApplicationWorld, world=QWebEngineScript.ApplicationWorld,