forked from OpenUserJS/OpenUserJS.org
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockUserScript.pegjs
More file actions
220 lines (198 loc) · 4.78 KB
/
blockUserScript.pegjs
File metadata and controls
220 lines (198 loc) · 4.78 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// peg grammar for parsing the UserScript metadata block
/*
Test the generated parser with some input for peg.js site at https://pegjs.org/online:
// ==UserScript==
// @name RFC 2606§3 - Hello, World!
// @name:es ¡Hola mundo!
// @name:fr Salut tout le monde!
// @namespace http://localhost.localdomain
// @description Test values with known UserScript metadata keys.
// @description:es Prueba de valores con UserScript metadatos llaves conocidas.
// @description:fr Valeurs d'essai avec des clés de métadonnées UserScript connues.
// @copyright 2013+, OpenUserJS Group (https://github.com/orgs/OpenUserJs/people)
// @license CC-BY-NC-SA-4.0; https://creativecommons.org/licenses/by-nc-sa/4.0/legalcode
// @licence GPL-3.0; http://www.gnu.org/licenses/gpl-3.0.txt
// @version 1.2.3
// @icon http://example.com/favicon.ico
// @iconURL http://example.com/faviconURL.ico
// @run-at document-end
// @run-at document-start
// @noframes
// @grant GM_log
// @grant none
// @homepageURL http://example.com/foo/atHomepageURL1
// @homepage http://example.com/foo/atHomepage2
// @website http://example.com/foo/atSite3
// @source http://example.com/foo/atSource4
// @downloadURL http://example.org/foo1.atDownloadURL.user.js
// @installURL http://example.org/foo2.atInstallURL.user.js
// @downloadURL http://example.org/foo3.atDownloadURL.user.js
// @updateURL http://example.org/foo1.atUpdateURL.meta.js
// @updateURL http://example.org/foo2.atUpdateURL.meta.js
// @updateURL http://example.org/foo3.atUpdateURL.meta.js
// @require http://example.net/library1.js
// @require http://example.net/library2.js
// @require http://example.net/library3.js
// @resource css http://example.net/library1.css
// @resource peg http://example.net/grammar2.pegjs
// @resource img http://example.net/image3.png
// @include http://example.com/*
// @include http://example.org/*
// @include http://example.net/*
// @match http://example.net/*
// @exclude http://example.com/foo
// @exclude http://example.org/foo
// @contributionURL https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=your.email@example.org&item_name=OpenUserJS+Author+Donation
// ==/UserScript==
*/
{
var upmix = function (aKeyword) {
// Keywords need to mirrored in the below rules for detection and transformation
switch (aKeyword) {
case 'homepage':
case 'source':
case 'website':
aKeyword = 'homepageURL';
break;
case 'defaulticon':
case 'iconURL':
aKeyword = 'icon';
break;
case 'licence':
aKeyword = 'license';
break;
case 'installURL':
aKeyword = 'downloadURL';
break;
}
return aKeyword;
}
}
block =
'// ==UserScript==\n'
lines:line*
'// ==/UserScript==' ('\n'?)
{
return lines;
}
line =
'// @'
data:
(
item0 /
item1 /
item1Localized /
items1 /
items2
)
'\n'?
{
return data;
}
whitespace = [ \t\n]+
non_whitespace = $[^ \t\n]+
non_newline = $[^\n]+
item0 =
keyword:
(
'unwrap' /
'noframes'
)
{
return {
unique: true,
key: upmix(keyword)
};
}
item1 =
keyword:
(
'version' /
'updateURL' /
'supportURL' /
'run-at' /
'namespace' /
'installURL' /
'iconURL' /
'icon64' /
'icon' /
'downloadURL' /
'defaulticon' /
'contributionURL'
)
whitespace
value: non_newline
{
return {
unique: true,
key: upmix(keyword),
value: value.replace(/\s+$/, '')
};
}
item1Localized =
keyword:
(
'name' /
'description'
)
locale: (':' localeValue:$[a-zA-Z-]+ {
return localeValue;
})?
whitespace
value: non_newline
{
var keywordUpmixed = upmix(keyword);
var obj = {
unique: true,
key: keywordUpmixed,
value: value.replace(/\s+$/, '')
}
if (locale) {
obj.key += ':' + locale;
obj.keyword = keywordUpmixed;
obj.locale = locale;
}
return obj;
}
items1 =
keyword:
(
'website' /
'source' /
'require' /
'match' /
'license' /
'licence' /
'include' /
'homepageURL' /
'homepage' /
'grant' /
'exclude' /
'copyright'
)
whitespace
value: non_newline
{
return {
key: upmix(keyword),
value: value.replace(/\s+$/, '')
};
}
items2 =
keyword:
(
'resource'
)
whitespace
value1: non_whitespace
whitespace
value2: non_newline
{
var value2trimmed = value2.replace(/\s+$/, '');
return {
key: upmix(keyword),
value: value1 + '\u0020' + value2trimmed,
value1: value1,
value2: value2trimmed
};
}