diff options
author | Florian Bruhin <me@the-compiler.org> | 2020-12-03 12:24:50 +0100 |
---|---|---|
committer | Florian Bruhin <me@the-compiler.org> | 2020-12-03 12:24:50 +0100 |
commit | ee1431ab4881e4d1814e22f6ace729ddbb1329d2 (patch) | |
tree | e4e09a4b2c6f1f85adea2e93dbee41bf5f4cd5f3 | |
parent | 9774208b63bb00e169376c25c68983beae6709e9 (diff) | |
parent | 846825bb95805b5fdf59b6ad07d1ad01cba00d53 (diff) | |
download | qutebrowser-ee1431ab4881e4d1814e22f6ace729ddbb1329d2.tar.gz qutebrowser-ee1431ab4881e4d1814e22f6ace729ddbb1329d2.zip |
Merge remote-tracking branch 'origin/pr/5900'
-rw-r--r-- | doc/userscripts.asciidoc | 2 | ||||
-rw-r--r-- | qutebrowser/browser/greasemonkey.py | 45 | ||||
-rw-r--r-- | qutebrowser/commands/userscripts.py | 1 | ||||
-rw-r--r-- | tests/helpers/fixtures.py | 2 | ||||
-rw-r--r-- | tests/unit/javascript/test_greasemonkey.py | 7 |
5 files changed, 33 insertions, 24 deletions
diff --git a/doc/userscripts.asciidoc b/doc/userscripts.asciidoc index 2dc34402d..9bbc68ce0 100644 --- a/doc/userscripts.asciidoc +++ b/doc/userscripts.asciidoc @@ -18,7 +18,7 @@ mpv, a simple key binding to something like `:spawn mpv {url}` should suffice. Also note userscripts need to have the executable bit set (`chmod +x`) for qutebrowser to run them. -To call a userscript, it needs to be stored in your data directory under +To call a userscript, it needs to be stored in your config or data directory under `userscripts` (for example: `~/.local/share/qutebrowser/userscripts/myscript`), or just use an absolute path. diff --git a/qutebrowser/browser/greasemonkey.py b/qutebrowser/browser/greasemonkey.py index 0305bd589..df8b2b0c2 100644 --- a/qutebrowser/browser/greasemonkey.py +++ b/qutebrowser/browser/greasemonkey.py @@ -41,9 +41,12 @@ from qutebrowser.misc import objects gm_manager = cast('GreasemonkeyManager', None) -def _scripts_dir(): +def _scripts_dirs(): """Get the directory of the scripts.""" - return os.path.join(standarddir.data(), 'greasemonkey') + return [ + os.path.join(standarddir.data(), 'greasemonkey'), + os.path.join(standarddir.config(), 'greasemonkey'), + ] class GreasemonkeyScript: @@ -277,18 +280,19 @@ class GreasemonkeyManager(QObject): self._run_end = [] self._run_idle = [] - scripts_dir = os.path.abspath(_scripts_dir()) - log.greasemonkey.debug("Reading scripts from: {}".format(scripts_dir)) - for script_filename in glob.glob(os.path.join(scripts_dir, '*.js')): - if not os.path.isfile(script_filename): - continue - script_path = os.path.join(scripts_dir, script_filename) - with open(script_path, encoding='utf-8-sig') as script_file: - script = GreasemonkeyScript.parse(script_file.read(), - script_filename) - if not script.name: - script.name = script_filename - self.add_script(script, force) + for scripts_dir in _scripts_dirs(): + scripts_dir = os.path.abspath(scripts_dir) + log.greasemonkey.debug("Reading scripts from: {}".format(scripts_dir)) + for script_filename in glob.glob(os.path.join(scripts_dir, '*.js')): + if not os.path.isfile(script_filename): + continue + script_path = os.path.join(scripts_dir, script_filename) + with open(script_path, encoding='utf-8-sig') as script_file: + script = GreasemonkeyScript.parse(script_file.read(), + script_filename) + if not script.name: + script.name = script_filename + self.add_script(script, force) self.scripts_reloaded.emit() def add_script(self, script, force=False): @@ -325,7 +329,7 @@ class GreasemonkeyManager(QObject): log.greasemonkey.debug("Loaded script: {}".format(script.name)) def _required_url_to_file_path(self, url): - requires_dir = os.path.join(_scripts_dir(), 'requires') + requires_dir = os.path.join(_scripts_dirs()[0], 'requires') if not os.path.exists(requires_dir): os.mkdir(requires_dir) return os.path.join(requires_dir, utils.sanitize_filename(url)) @@ -426,7 +430,7 @@ def greasemonkey_reload(force=False): """Re-read Greasemonkey scripts from disk. The scripts are read from a 'greasemonkey' subdirectory in - qutebrowser's data directory (see `:version`). + qutebrowser's data or config directories (see `:version`). Args: force: For any scripts that have required dependencies, @@ -440,7 +444,8 @@ def init(): global gm_manager gm_manager = GreasemonkeyManager() - try: - os.mkdir(_scripts_dir()) - except FileExistsError: - pass + for scripts_dir in _scripts_dirs(): + try: + os.mkdir(scripts_dir) + except FileExistsError: + pass diff --git a/qutebrowser/commands/userscripts.py b/qutebrowser/commands/userscripts.py index ce25d7d28..6d2c2f147 100644 --- a/qutebrowser/commands/userscripts.py +++ b/qutebrowser/commands/userscripts.py @@ -395,6 +395,7 @@ def _lookup_path(cmd): directories = [ os.path.join(standarddir.data(), "userscripts"), os.path.join(standarddir.data(system=True), "userscripts"), + os.path.join(standarddir.config(), "userscripts"), ] for directory in directories: cmd_path = os.path.join(directory, cmd) diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 3c0623dfb..6f80099bb 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -166,7 +166,7 @@ def fake_web_tab(stubs, tab_registry, mode_manager, qapp): @pytest.fixture -def greasemonkey_manager(monkeypatch, data_tmpdir): +def greasemonkey_manager(monkeypatch, data_tmpdir, config_tmpdir): gm_manager = greasemonkey.GreasemonkeyManager() monkeypatch.setattr(greasemonkey, 'gm_manager', gm_manager) diff --git a/tests/unit/javascript/test_greasemonkey.py b/tests/unit/javascript/test_greasemonkey.py index 5d549ca68..5f2c94b56 100644 --- a/tests/unit/javascript/test_greasemonkey.py +++ b/tests/unit/javascript/test_greasemonkey.py @@ -41,12 +41,15 @@ test_gm_script = r""" console.log("Script is running."); """ -pytestmark = pytest.mark.usefixtures('data_tmpdir') +pytestmark = [ + pytest.mark.usefixtures('data_tmpdir'), + pytest.mark.usefixtures('config_tmpdir') +] def _save_script(script_text, filename): # pylint: disable=no-member - file_path = py.path.local(greasemonkey._scripts_dir()) / filename + file_path = py.path.local(greasemonkey._scripts_dirs()[0]) / filename # pylint: enable=no-member file_path.write_text(script_text, encoding='utf-8', ensure=True) |