From 87a0c2a7a70f2a6c1e73cf4f21682279d50cc8f3 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Sat, 20 Jan 2018 16:31:10 +1300 Subject: [PATCH] Greasemonkey: indent source of required scripts This is for the case where a script uses `@require` to pull down another greasemonkey script. Since QWebEngineScript doesn't support `@require` we pass scripts to it with any required ones pre-pended. To avoid QWebEngineScript parsing the first metadata block, the one from the required script, we indent the whole lot. Because the greasemonkey spec says that the //==UserScript== text must start in the first column. --- qutebrowser/browser/greasemonkey.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/greasemonkey.py b/qutebrowser/browser/greasemonkey.py index 683985f54..d37340d88 100644 --- a/qutebrowser/browser/greasemonkey.py +++ b/qutebrowser/browser/greasemonkey.py @@ -25,6 +25,7 @@ import json import fnmatch import functools import glob +import textwrap import attr from PyQt5.QtCore import pyqtSignal, QObject, QUrl @@ -122,10 +123,11 @@ class GreasemonkeyScript: def add_required_script(self, source): """Add the source of a required script to this script.""" - # NOTE: If source also contains a greasemonkey metadata block then - # QWebengineScript will parse that instead of the actual one. - # Adding an indent to source would stop that. - self._code = "\n".join([source, self._code]) + # The additional source is indented in case it also contains a + # metadata block. Because we pass everything at once to + # QWebEngineScript and that would parse the first metadata block + # found as the valid one. + self._code = "\n".join([textwrap.indent(source, " "), self._code]) @attr.s