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.
This commit is contained in:
Jimmy 2018-01-20 16:31:10 +13:00
parent 60e6d28eb1
commit 87a0c2a7a7

View File

@ -25,6 +25,7 @@ import json
import fnmatch import fnmatch
import functools import functools
import glob import glob
import textwrap
import attr import attr
from PyQt5.QtCore import pyqtSignal, QObject, QUrl from PyQt5.QtCore import pyqtSignal, QObject, QUrl
@ -122,10 +123,11 @@ class GreasemonkeyScript:
def add_required_script(self, source): def add_required_script(self, source):
"""Add the source of a required script to this script.""" """Add the source of a required script to this script."""
# NOTE: If source also contains a greasemonkey metadata block then # The additional source is indented in case it also contains a
# QWebengineScript will parse that instead of the actual one. # metadata block. Because we pass everything at once to
# Adding an indent to source would stop that. # QWebEngineScript and that would parse the first metadata block
self._code = "\n".join([source, self._code]) # found as the valid one.
self._code = "\n".join([textwrap.indent(source, " "), self._code])
@attr.s @attr.s