diff --git a/qutebrowser/browser/browsertab.py b/qutebrowser/browser/browsertab.py index 7e78b2621..0b1c8d8da 100644 --- a/qutebrowser/browser/browsertab.py +++ b/qutebrowser/browser/browsertab.py @@ -812,6 +812,9 @@ class AbstractTab(QWidget): self.title_changed.emit(url.toDisplayString()) self.url_changed.emit(url) + url = self.url(requested=True) + self._update_stylesheet(url) + @pyqtSlot() def _on_load_started(self): self._progress = 0 diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 5f0952bca..101ad3611 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -801,7 +801,7 @@ class _WebEngineScripts(QObject): def _update_stylesheet(self, url=None): """Update the custom stylesheet in existing tabs.""" 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) def _inject_early_js(self, name, js_code, *, @@ -1226,9 +1226,6 @@ class WebEngineTab(browsertab.AbstractTab): # the old icon is still displayed. self.icon_changed.emit(QIcon()) - url = self.url(requested=True) - self._update_stylesheet(url) - @pyqtSlot(QUrl) def _on_predicted_navigation(self, url): """If we know we're going to visit an URL soon, change the settings. diff --git a/qutebrowser/utils/javascript.py b/qutebrowser/utils/javascript.py index 93df8e70f..e7053736c 100644 --- a/qutebrowser/utils/javascript.py +++ b/qutebrowser/utils/javascript.py @@ -64,14 +64,28 @@ def _convert_js_arg(arg): arg, type(arg).__name__)) -def assemble(module, function, *args): +def assemble(module, function, *args, delay=False): """Assemble a javascript file and a function call.""" js_args = ', '.join(_convert_js_arg(arg) for arg in args) if module == 'window': parts = ['window', function] else: 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