-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathgm_config_unit_test.user.js
More file actions
400 lines (377 loc) · 11.8 KB
/
Copy pathgm_config_unit_test.user.js
File metadata and controls
400 lines (377 loc) · 11.8 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// ==UserScript==
// @name The GM_config Unit Test
// @namespace sizzlemctwizzle
// @description Attempts to test every aspect of GM_config.
// @license MIT
// @version 2.3.0.3
// @grant GM_getValue
// @grant GM_setValue
// @grant GM.getValue
// @grant GM.setValue
// @require https://openuserjs.org/src/libs/sizzle/GM_config.min.js
// @include https://openuserjs.org/scripts/sizzle/The_GM_config_Unit_Test
// @homepageURL https://github.com/sizzlemctwizzle/UserScripts/blob/master/gm_config_unit_test.user.js
// @updateURL https://openuserjs.org/meta/sizzle/The_GM_config_Unit_Test.meta.js
// ==/UserScript==
// ==OpenUserJS==
// @author sizzle
// @collaborator Marti
// ==/OpenUserJS==
/* jshint esversion: 5 */
/* globals GM_config, GM_configStruct */
var fieldDefs = {
'name': {
'section': [GM_config.create('Personal Info About Yourself'), 'We need this info to do stuff'],
'label': GM_config.create('Name'),
'type': 'text',
'default': 'Joe Simmons'
},
'birthday': {
'label': 'Date of Birth',
'type': 'date',
'format': 'dd/mm/yyyy'
},
'age': {
'label': 'Age',
'type': 'unsigned int',
'default': 19
},
'gender': {
'options': ['Male', 'Female'],
'label': 'Gender',
'type': 'radio',
'default': 'Male'
},
'income': {
'labelPos': 'right',
'label': 'Income',
'type': 'float',
'default': 50000.0
},
'status': {
'label': 'Married',
'labelPos': 'above',
'type': 'checkbox',
'default': false
},
'work': {
'label': 'Job',
'type': 'select',
'labelPos': 'below',
'options': ['Carpenter', 'Truck Driver', 'Porn Star'],
'default': 'Truck Driver'
},
'bunchOtext': {
'label': 'Bunch of Text',
'type': 'textarea',
'default': 'I actually did\'t realize we had this field until recently...'
},
'magic': {
'label': 'Magic Button',
'type': 'button',
'click': function() {
alert('Magic works!');
}
},
'upperLeft': {
'section': 'Check some boxes',
'type': 'checkbox',
'default': false
},
'upperLeftMiddle': {
'type': 'checkbox',
'default': false
},
'upperRightMiddle': {
'type': 'checkbox',
'default': false
},
'upperRight': {
'type': 'checkbox',
'default': false
},
'middleLeft': {
'section': [],
'type': 'checkbox',
'default': false
},
'middleLeftMiddle': {
'type': 'checkbox',
'default': false
},
'middleRightMiddle': {
'type': 'checkbox',
'default': false
},
'middleRight': {
'type': 'checkbox',
'default': false
},
'bottomLeft': {
'section': [],
'type': 'checkbox',
'default': false
},
'bottomLeftMiddle': {
'type': 'checkbox',
'default': false
},
'bottomRightMiddle': {
'type': 'checkbox',
'default': false
},
'bottomRight': {
'type': 'checkbox',
'default': false
},
'labelLess': {
'section': GM_config.create('New Section'),
'type': 'text',
'default': 'This value is not saved.',
'save': false
},
'alertTextField': {
'label': 'Alert Text',
'type': 'button',
'click': function() {
var value = GM_config.fields['labelLess'].toValue();
if (value != null) {
alert(value);
GM_config.fields['labelLess'].value = 'Value changed.';
GM_config.fields['labelLess'].reload();
} else alert('null field has already been changed and forgotten.');
}
},
'customCSS': {
'label': 'Enter CSS',
'type': 'text',
'save': false,
'default': ''
},
'validCSS': {
'type': 'hidden',
'default': '',
}
};
GM_config.init(
{
id: 'GM_config',
title: 'Configurable Options Script',
fields: fieldDefs,
css: '#GM_config_section_1 .config_var, #GM_config_section_2 .config_var, #GM_config_section_3 .config_var { margin: 5% !important;display: inline !important; }',
events:
{
open: function(doc) {
doc.getElementById('GM_config_section_header_1').className = 'field_label';
var customCSS = GM_config.fields['customCSS'].node;
var validCSS = GM_config.fields['validCSS'].node;
customCSS.value = validCSS.value;
customCSS.addEventListener('change', function () {
if(/\w+\s*\{\s*\w+\s*:\s*\w+[\s|\S]*\}/.test(customCSS.value))
validCSS.value = customCSS.value;
}, false);
},
save: function(values) {
// All the values that aren't saved are passed to this function
// for (i in values) alert(values[i]);
}
},
'types':
{
'date': {
'default': null,
toNode: function(configId) {
var field = this.settings,
value = this.value,
id = this.id,
create = this.create,
format = (field.format || 'mm/dd/yyyy').split('/'),
slash = null,
retNode = create('div', { className: 'config_var',
id: configId + '_' + id + '_var',
title: field.title || '' });
// Save the format array to the field object so
// it's easier to hack externally
this.format = format;
// Create the field lable
retNode.appendChild(create('label', {
innerHTML: field.label,
id: configId + '_' + id + '_field_label',
for: configId + '_field_' + id,
className: 'field_label'
}));
// Create the inputs for each part of the date
value = value ? value.split('/') : this['default'];
for (var i = 0, len = format.length; i < len; ++i) {
var props = {
id: configId + '_field_' + id + '_' + format[i],
type: 'text',
size: format[i].length,
value: value ? value[i] : '',
onkeydown: function(e) {
var input = e.target;
if (input.value.length >= input.size)
input.value = input.value.substr(0, input.size - 1);
}
};
// Jump to the next input once one is complete
// This saves the user a little work
if (i < format.length - 1) {
slash = create(' / ');
props.onkeyup = function(e) {
var input = e.target,
inputs = input.parentNode.getElementsByTagName('input'),
num = 0;
for (; num < inputs.length && input != inputs[num]; ++num);
if (input.value.length >= input.size)
inputs[num + 1].focus();
};
} else slash = null;
// Actually create and append the input element
retNode.appendChild(create('input', props));
if (slash) retNode.appendChild(slash);
}
return retNode;
},
toValue: function() {
var rval = null;
if (this.wrapper) {
var inputs = this.wrapper.getElementsByTagName('input');
rval = '';
// Join the field values together seperated by slashes
for (var i = 0, len = inputs.length; i < len; ++i) {
// Don't save values that aren't numbers
if (isNaN(Number(inputs[i].value))) {
alert('Date is invalid');
return null;
}
rval += inputs[i].value + (i < len - 1 ? '/' : '');
}
}
// We are just returning a string to be saved
// If you want to use this value you'll want a Date object
return rval;
},
reset: function() {
// Empty all the input fields
if (this.wrapper) {
var inputs = this.wrapper.getElementsByTagName('input');
for (var i = 0, len = inputs.length; i < len; ++i)
inputs[i].value = '';
}
}
}
}
});
(async () => {
// Retrieve language setting
var lang = await GM_config.getValue('lang', 'en');
// Fields in different languages
var langDefs = {
'en': // Fields in English
{
'lang':
{
'label': 'Choose Language',
'type': 'select',
'options': ['en', 'de'],
'save': false // This field's value will NOT be saved
}
},
'de': // Fields in German
{
'lang':
{
'label': 'Sprache wählen',
'type': 'select',
'options': ['en', 'de'],
'save': false // This field's value will NOT be saved
}
}
};
// Use field definitions for the stored language
var fields = langDefs[lang];
// The title for the settings panel in different languages
var titles = {
'en': 'Translations Dialog',
'de': 'Übersetzungen Dialog'
};
var title = titles[lang];
// Translations for the buttons and reset link
var saveButton = {'en': 'Save', 'de': 'Speichern'};
var closeButton = {'en': 'Close', 'de': 'Schließen'};
var resetLink = {
'en': 'Reset fields to default values',
'de': 'Felder zurücksetzen auf Standardwerte'
};
var gmc_trans = new GM_configStruct(
{
'id': 'GM_config_trans', // The id used for this instance of GM_config
'title': title,
'fields': fields, // Fields object
'events':
{
'init': function()
{
// You must manually set an unsaved value
this.fields['lang'].value = lang;
},
'open': function (doc) {
// translate the buttons
var config = this;
doc.getElementById(config.id + '_saveBtn').textContent = saveButton[lang];
doc.getElementById(config.id + '_closeBtn').textContent = closeButton[lang];
doc.getElementById(config.id + '_resetLink').textContent = resetLink[lang];
},
'save': function(values) { // All unsaved values are passed to save
for (var i in values) {
if (i == 'lang' && values[i] != lang) {
var config = this;
lang = values[i];
// Use field definitions for the chosen language
fields = langDefs[lang];
config.fields['lang'].value = lang;
// Use the title for the chose language
title = titles[lang];
// Re-initialize GM_config for the language change
config.init({ 'id': config.id, title: title, 'fields': fields });
// Refresh the config panel for the new language change
config.close();
config.open();
// Save the chosen language for next time
config.setValue('lang', lang);
}
}
}
}
});
GM_config.init(
{
id: 'GM_config',
'events':
{
'init': function()
{
GM_config.open();
}
},
fields:
{
'extra':
{
'label': 'Extra Field',
'type': 'text',
'default': 'This field was added with a second call to init()'
},
'openTrans':
{
'label': 'Open Translation Demo',
'type': 'button',
'click': function() {
GM_config.close();
gmc_trans.open();
}
}
}
});
})();