diff options
author | Florian Bruhin <me@the-compiler.org> | 2021-03-09 18:10:51 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2021-03-09 18:10:51 +0100 |
commit | 7eae8f069406bb72fa4a663c22a4c04245d750f2 (patch) | |
tree | e5f2e15c989c9982da431d9a603d413860e815f1 | |
parent | c4144a6e4b524631cad3113617ab64a909fd0503 (diff) | |
download | qutebrowser-7eae8f069406bb72fa4a663c22a4c04245d750f2.tar.gz qutebrowser-7eae8f069406bb72fa4a663c22a4c04245d750f2.zip |
Fix @run-at default for GreaseMonkey
-rw-r--r-- | doc/changelog.asciidoc | 4 | ||||
-rw-r--r-- | qutebrowser/browser/webengine/webenginetab.py | 12 | ||||
-rw-r--r-- | tests/unit/browser/webengine/test_webenginetab.py | 34 |
3 files changed, 50 insertions, 0 deletions
diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index 93a85866b..d57698df7 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -93,6 +93,10 @@ Fixed properly. - The "try again" button on error pages now works correctly with JavaScript disabled. +- If a GreaseMonkey script doesn't have a "@run-at" comment, qutebrowser + accidentally treated that as "@run-at document-idle". However, other + GreaseMonkey implementations default to "@run-at document-end" instead, which + is what qutebrowser now does, too. [[v2.0.2]] v2.0.2 (2021-02-04) diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 439d99570..450d68751 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -1099,6 +1099,7 @@ class _WebEngineScripts(QObject): for script in scripts: new_script = QWebEngineScript() + try: world = int(script.jsworld) if not 0 <= world <= qtutils.MAX_WORLD_ID: @@ -1116,6 +1117,17 @@ class _WebEngineScripts(QObject): f": {script.jsworld}") continue new_script.setWorldId(world) + + # Corresponds to "@run-at document-end" which is the default according to + # https://wiki.greasespot.net/Metadata_Block#.40run-at - however, + # QtWebEngine uses QWebEngineScript.Deferred (@run-at document-idle) as + # default. + # + # NOTE that this needs to be done before setSourceCode, so that + # QtWebEngine's parsing of GreaseMonkey tags will override it if there is a + # @run-at comment. + new_script.setInjectionPoint(QWebEngineScript.DocumentReady) + new_script.setSourceCode(script.code()) new_script.setName(f"GM-{script.name}") new_script.setRunsOnSubFrames(script.runs_on_sub_frames) diff --git a/tests/unit/browser/webengine/test_webenginetab.py b/tests/unit/browser/webengine/test_webenginetab.py index 7827c379b..2c792f80e 100644 --- a/tests/unit/browser/webengine/test_webenginetab.py +++ b/tests/unit/browser/webengine/test_webenginetab.py @@ -20,6 +20,7 @@ """Test webenginetab.""" import logging +import textwrap import pytest QtWebEngineWidgets = pytest.importorskip("PyQt5.QtWebEngineWidgets") @@ -116,6 +117,39 @@ class TestWebengineScripts: script = collection.toList()[-1] assert script.injectionPoint() == QWebEngineScript.DocumentReady + @pytest.mark.parametrize('run_at, expected', [ + # UserScript::DocumentElementCreation + ('document-start', QWebEngineScript.DocumentCreation), + # UserScript::DocumentLoadFinished + ('document-end', QWebEngineScript.DocumentReady), + # UserScript::AfterLoad + ('document-idle', QWebEngineScript.Deferred), + # default according to https://wiki.greasespot.net/Metadata_Block#.40run-at + (None, QWebEngineScript.DocumentReady), + ]) + def test_run_at_values(self, webengine_scripts, run_at, expected): + if run_at is None: + script = """ + // ==UserScript== + // @name qutebrowser test userscript + // ==/UserScript== + """ + else: + script = f""" + // ==UserScript== + // @name qutebrowser test userscript + // @run-at {run_at} + // ==/UserScript== + """ + + script = textwrap.dedent(script.lstrip('\n')) + scripts = [greasemonkey.GreasemonkeyScript.parse(script)] + webengine_scripts._inject_greasemonkey_scripts(scripts) + + collection = webengine_scripts._widget.page().scripts() + script = collection.toList()[-1] + assert script.injectionPoint() == expected + def test_notification_permission_workaround(): """Make sure the value for QWebEnginePage::Notifications is correct.""" |