diff --git a/qutebrowser/browser/greasemonkey.py b/qutebrowser/browser/greasemonkey.py index 9a82d6a93..7ee843b0b 100644 --- a/qutebrowser/browser/greasemonkey.py +++ b/qutebrowser/browser/greasemonkey.py @@ -23,7 +23,6 @@ import re import os import json import fnmatch -import functools import glob import attr @@ -178,10 +177,10 @@ class GreasemonkeyManager(QObject): elif script.run_at == 'document-idle': self._run_idle.append(script) else: - log.greasemonkey.warning("Script {} has invalid run-at " - "defined, defaulting to " - "document-end" - .format(script_path)) + if script.run_at: + log.greasemonkey.warning( + "Script {} has invalid run-at defined, " + "defaulting to document-end".format(script_path)) # Default as per # https://wiki.greasespot.net/Metadata_Block#.40run-at self._run_end.append(script) @@ -196,11 +195,23 @@ class GreasemonkeyManager(QObject): """ if url.scheme() not in self.greaseable_schemes: return MatchingScripts(url, [], [], []) - match = functools.partial(fnmatch.fnmatch, - url.toString(QUrl.FullyEncoded)) + + string_url = url.toString(QUrl.FullyEncoded) + + def _match(pattern): + # For include and exclude rules if they start and end with '/' they + # should be treated as a (ecma syntax) regular expression. + if pattern.startswith('/') and pattern.endswith('/'): + matches = re.search(pattern[1:-1], string_url, flags=re.I) + return matches is not None + + # Otherwise they are glob expressions. + return fnmatch.fnmatch(string_url, pattern) + tester = (lambda script: - any(match(pat) for pat in script.includes) and - not any(match(pat) for pat in script.excludes)) + any(_match(pat) for pat in script.includes) and + not any(_match(pat) for pat in script.excludes)) + return MatchingScripts( url, [script for script in self._run_start if tester(script)], diff --git a/tests/unit/javascript/test_greasemonkey.py b/tests/unit/javascript/test_greasemonkey.py index 0f5fe476c..1aaa3380f 100644 --- a/tests/unit/javascript/test_greasemonkey.py +++ b/tests/unit/javascript/test_greasemonkey.py @@ -19,6 +19,7 @@ """Tests for qutebrowser.browser.greasemonkey.""" import logging +import textwrap import pytest import py.path # pylint: disable=no-name-in-module @@ -26,7 +27,7 @@ from PyQt5.QtCore import QUrl from qutebrowser.browser import greasemonkey -test_gm_script = """ +test_gm_script = r""" // ==UserScript== // @name qutebrowser test userscript // @namespace invalid.org @@ -75,6 +76,31 @@ def test_get_scripts_by_url(url, expected_matches): expected_matches) +@pytest.mark.parametrize("url, expected_matches", [ + # included + ('https://github.com/qutebrowser/qutebrowser/', 1), + # neither included nor excluded + ('http://aaaaaaaaaa.com/', 0), + # excluded takes priority + ('http://github.com/foo', 0), +]) +def test_regex_includes_scripts_for(url, expected_matches): + """Ensure our GM @*clude support supports regular expressions.""" + gh_dark_example = textwrap.dedent(r""" + // ==UserScript== + // @include /^https?://((gist|guides|help|raw|status|developer)\.)?github\.com/((?!generated_pages\/preview).)*$/ + // @exclude /https?://github\.com/foo/ + // @run-at document-start + // ==/UserScript== + """) + _save_script(gh_dark_example, 'test.user.js') + gm_manager = greasemonkey.GreasemonkeyManager() + + scripts = gm_manager.scripts_for(QUrl(url)) + assert (len(scripts.start + scripts.end + scripts.idle) == + expected_matches) + + def test_no_metadata(caplog): """Run on all sites at document-end is the default.""" _save_script("var nothing = true;\n", 'nothing.user.js')