Try to fix intermittent per-domain stylesheets

This commit is contained in:
Jordyn/The Linux Geek 2018-04-20 00:27:59 -05:00 committed by Florian Bruhin
parent f391d726c0
commit b04934c2b3
3 changed files with 20 additions and 6 deletions

View File

@ -812,6 +812,9 @@ class AbstractTab(QWidget):
self.title_changed.emit(url.toDisplayString()) self.title_changed.emit(url.toDisplayString())
self.url_changed.emit(url) self.url_changed.emit(url)
url = self.url(requested=True)
self._update_stylesheet(url)
@pyqtSlot() @pyqtSlot()
def _on_load_started(self): def _on_load_started(self):
self._progress = 0 self._progress = 0

View File

@ -801,7 +801,7 @@ class _WebEngineScripts(QObject):
def _update_stylesheet(self, url=None): def _update_stylesheet(self, url=None):
"""Update the custom stylesheet in existing tabs.""" """Update the custom stylesheet in existing tabs."""
css = shared.get_user_stylesheet(url=url) css = shared.get_user_stylesheet(url=url)
code = javascript.assemble('stylesheet', 'set_css', css) code = javascript.assemble('stylesheet', 'set_css', css, delay=True)
self._tab.run_js_async(code) self._tab.run_js_async(code)
def _inject_early_js(self, name, js_code, *, def _inject_early_js(self, name, js_code, *,
@ -1226,9 +1226,6 @@ class WebEngineTab(browsertab.AbstractTab):
# the old icon is still displayed. # the old icon is still displayed.
self.icon_changed.emit(QIcon()) self.icon_changed.emit(QIcon())
url = self.url(requested=True)
self._update_stylesheet(url)
@pyqtSlot(QUrl) @pyqtSlot(QUrl)
def _on_predicted_navigation(self, url): def _on_predicted_navigation(self, url):
"""If we know we're going to visit an URL soon, change the settings. """If we know we're going to visit an URL soon, change the settings.

View File

@ -64,14 +64,28 @@ def _convert_js_arg(arg):
arg, type(arg).__name__)) arg, type(arg).__name__))
def assemble(module, function, *args): def assemble(module, function, *args, delay=False):
"""Assemble a javascript file and a function call.""" """Assemble a javascript file and a function call."""
js_args = ', '.join(_convert_js_arg(arg) for arg in args) js_args = ', '.join(_convert_js_arg(arg) for arg in args)
if module == 'window': if module == 'window':
parts = ['window', function] parts = ['window', function]
else: else:
parts = ['window', '_qutebrowser', module, function] parts = ['window', '_qutebrowser', module, function]
code = '"use strict";\n{}({});'.format('.'.join(parts), js_args) if delay:
code = '''
"use strict";
if (document.readyState !== "loading") {{
{parts}({args});
}} else {{
window.addEventListener("DOMContentLoaded", () => {{
{parts}({args});
}});
}}
'''
code = code.format(parts='.'.join(parts), args=js_args)
else:
code = '"use strict";\n{}({});'.format('.'.join(parts), js_args)
# print(code)
return code return code