-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathuserscript.py
More file actions
195 lines (166 loc) · 5.72 KB
/
Copy pathuserscript.py
File metadata and controls
195 lines (166 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import re
from string import Template
from typing import Callable, NamedTuple, Optional
import warnings
from urlmatch import urlmatch
import modules.inline as inline
import modules.metadata as metadata
from modules.metadata import Metadata, Tag, Tag_boolean, Tag_string
from modules.patterns import isIncludePattern, isMatchPattern, regexFromIncludePattern
from modules.utilities import compose2, stripIndentation, strs
REGEX_URL: re.Pattern = re.compile(r"^https?://")
directive_name : str = "name"
directive_version : str = "version"
directive_run_at : str = "run-at"
directive_match : str = "match"
directive_include : str = "include"
directive_exclude : str = "exclude"
directive_noframes : str = "noframes"
directive_downloadURL : str = "downloadURL"
document_end : str = "document-end"
document_start : str = "document-start"
document_idle : str = "document-idle"
tag_name: Tag_string = Tag_string(
name = directive_name,
unique = True,
default = None,
required = True,
predicate = lambda val: val != "",
)
tag_run_at: Tag_string = Tag_string(
name = directive_run_at,
unique = True,
default = document_end,
required = False,
predicate = lambda val: val in [ document_end, document_start, document_idle ],
)
tag_match: Tag_string = Tag_string(
name = directive_match,
unique = False,
default = None,
required = False,
predicate = isMatchPattern,
)
tag_include: Tag_string = Tag_string(
name = directive_include,
unique = False,
default = None,
required = False,
predicate = isIncludePattern,
)
tag_exclude: Tag_string = Tag_string(
name = directive_exclude,
unique = False,
default = None,
required = False,
predicate = isIncludePattern,
)
tag_noframes: Tag_boolean = Tag_boolean(
name = directive_noframes,
unique = True,
default = False,
required = False,
predicate = None,
)
tag_version: Tag_string = Tag_string(
name = directive_version,
unique = True,
default = None,
required = False,
predicate = None,
)
tag_downloadURL: Tag_string = Tag_string(
name = directive_downloadURL,
unique = True,
default = None,
required = False,
predicate = lambda val: REGEX_URL.match(val) is not None,
)
METADATA_TAGS: list[Tag] = [
tag_name,
tag_run_at,
tag_match,
tag_noframes,
tag_include,
tag_exclude,
tag_version,
tag_downloadURL,
]
validateMetadata: Callable[[Metadata], Metadata] = metadata.validator(METADATA_TAGS)
STRING_WARNING_INVALID_REGEX: Template = Template(f"""{metadata.tag(directive_include)}/{metadata.tag(directive_exclude)} patterns starting and ending with `/` are interpreted as regular expressions, and this pattern is not a valid regex:
$pattern
The regex engine reported this error:
$error
""")
class Userscript(NamedTuple):
name: str
version: Optional[str]
content: str
runAt: str
noframes: bool
matchPatterns: list[str]
includePatternRegexes: list[re.Pattern]
excludePatternRegexes: list[re.Pattern]
downloadURL: Optional[str]
unsafeSequences: list[str] # in <script> tag
def __str__(self) -> str:
return self.name
def create(content: str) -> Userscript:
validMetadata: Metadata = validateMetadata(metadata.parse(metadata.extract(content)))
valueOf = metadata.valueGetter_one(validMetadata)
allValuesOf = metadata.valueGetter_all(validMetadata)
includePatternRegexes: list[re.Pattern] = list(filter(
lambda x: x is not None,
map(
compose2(regexFromIncludePattern_safe, str),
allValuesOf(tag_include)
)
))
excludePatternRegexes: list[re.Pattern] = list(filter(
lambda x: x is not None,
map(
compose2(regexFromIncludePattern_safe, str),
allValuesOf(tag_exclude)
)
))
return Userscript(
name = str(valueOf(tag_name)),
version = None if valueOf(tag_version) is None else str(valueOf(tag_version)),
content = content,
runAt = str(valueOf(tag_run_at)),
noframes = bool(valueOf(tag_noframes)),
matchPatterns = strs(allValuesOf(tag_match)),
includePatternRegexes = includePatternRegexes,
excludePatternRegexes = excludePatternRegexes,
downloadURL = None if valueOf(tag_downloadURL) is None else str(valueOf(tag_downloadURL)),
unsafeSequences = inline.unsafeSequencesIn(content),
)
def applicableChecker(url: str) -> Callable[[Userscript], bool]:
def isApplicable(userscript: Userscript) -> bool:
for regex in userscript.excludePatternRegexes:
if regex.search(url) is not None:
return False
for regex in userscript.includePatternRegexes:
if regex.search(url) is not None:
return True
for pattern in userscript.matchPatterns:
if urlmatch(pattern, url):
return True
return False
return isApplicable
def withEventListener(event: str) -> Callable[[str], str]:
return lambda scriptContent: f"""window.addEventListener("{event}", function() {{\n{scriptContent}\n}});"""
def withNoframes(scriptContent: str) -> str:
return stripIndentation(f"""
if (window.top === window) {{ // {metadata.tag(directive_noframes)}
{scriptContent}
}}
""")
def withVersionSuffix(url: str, version: Optional[str]) -> str:
return url + ("" if version is None else "?v=" + version)
def regexFromIncludePattern_safe(pattern: str) -> Optional[re.Pattern]:
try:
return regexFromIncludePattern(pattern)
except re.error as error:
# warnings.warn(STRING_WARNING_INVALID_REGEX.substitute(pattern=pattern, error=error)) # TODO: uncomment when we can handle warnings
return None