Greasemonkey: move scripts for a domain into data class.

Also makes scripts that don't include a greasemonkey metadata block
match any url. QWebEngine already has that behaviour.
This commit is contained in:
Jimmy 2017-10-04 20:42:10 +13:00
parent 41035cb5ca
commit edf737ff7d
2 changed files with 21 additions and 7 deletions

View File

@ -26,6 +26,7 @@ import fnmatch
import functools
import glob
import attr
from PyQt5.QtCore import pyqtSignal, QObject
from qutebrowser.utils import log, standarddir
@ -186,6 +187,8 @@ unsafeWindow = window;
props = ""
script = cls(re.findall(cls.PROPS_REGEX, props), source)
script.script_meta = '"{}"'.format("\\n".join(props.split('\n')[2:]))
if not props:
script.includes = ['*']
return script
def code(self):
@ -215,6 +218,16 @@ unsafeWindow = window;
})
@attr.s
class MatchingScripts(object):
"""All userscripts registered to run on a particular url."""
url = attr.ib()
start = attr.ib(default=attr.Factory(list))
end = attr.ib(default=attr.Factory(list))
idle = attr.ib(default=attr.Factory(list))
class GreasemonkeyManager(QObject):
"""Manager of userscripts and a greasemonkey compatible environment.
@ -278,12 +291,13 @@ class GreasemonkeyManager(QObject):
document-end, document-idle)
"""
if url.split(':', 1)[0] not in self.greaseable_schemes:
return [], [], []
return MatchingScripts(url, [], [], [])
match = functools.partial(fnmatch.fnmatch, url)
tester = (lambda script:
any([match(pat) for pat in script.includes]) and
not any([match(pat) for pat in script.excludes]))
return (
return MatchingScripts(
url,
[script for script in self._run_start if tester(script)],
[script for script in self._run_end if tester(script)],
[script for script in self._run_idle if tester(script)]

View File

@ -300,14 +300,14 @@ class BrowserPage(QWebPage):
"""
greasemonkey = objreg.get('greasemonkey')
url = self.currentFrame().url()
start_scripts, end_scripts, idle_scripts = \
greasemonkey.scripts_for(url.toDisplayString())
scripts = greasemonkey.scripts_for(url.toDisplayString())
if load == "start":
toload = start_scripts
toload = scripts.start
elif load == "end":
toload = end_scripts
toload = scripts.end
elif load == "idle":
toload = idle_scripts
toload = scripts.idle
for script in toload:
log.webview.debug('Running GM script: {}'.format(script.name))