forked from samliew/SO-mod-userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSOMU-options.user.js
322 lines (296 loc) · 10.3 KB
/
SOMU-options.user.js
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
// ==UserScript==
// @name SOMU Options
// @description Adds right sidebar to modify options of installed userscripts from the Stack Overflow Moderation Userscripts repo
// @homepage https://github.com/samliew/SO-mod-userscripts
// @author @samliew
// @version 2.2
//
// @include https://*stackoverflow.com/*
// @include https://*serverfault.com/*
// @include https://*superuser.com/*
// @include https://*askubuntu.com/*
// @include https://*mathoverflow.net/*
// @include https://*.stackexchange.com/*
//
// @exclude *chat.*
// @exclude https://stackoverflow.com/c/*
// ==/UserScript==
/* globals StackExchange, GM_info */
'use strict';
const toInt = v => v == null || isNaN(Number(v)) ? null : Number(v);
const toBool = v => v == null ? null : v === true || v.toLowerCase() === 'true';
const toSlug = str => (str || '').toLowerCase().replace(/[^a-z0-9]+/g, '-');
// Any way to avoid using a global variable?
var SOMU = unsafeWindow.SOMU || /** @type {SOMU} */ ({
keyPrefix: 'SOMU:',
hasInit: false,
sidebar: null,
sidebarContent: null,
store: window.localStorage,
/**
* @param {string} scriptName
* @param {string} optionName
* @param {string|number|boolean} [defaultValue]
* @param {"bool"|"int"|"string"} [dataType]
*/
getOptionValue(scriptName, optionName, defaultValue = null, dataType = 'string') {
const scriptSlug = toSlug(scriptName);
const optionSlug = toSlug(optionName);
const uniqueSlug = `${SOMU.keyPrefix}${scriptSlug}:${optionSlug}`;
let v = /** @type {string|number|boolean} */(this.store.getItem(uniqueSlug));
if (dataType === 'int') v = toInt(v);
if (dataType === 'bool') {
v = toBool(v);
return v;
}
return v || defaultValue;
},
/**
* @param {string} key
* @param {string} value
*/
saveOptionValue: function (key, value) {
this.store.setItem(key, value.trim());
},
/**
* @param {string} scriptName
* @param {string} optionName
* @param {string|number|boolean} [defaultValue]
* @param {"bool"|"int"|"string"} [dataType]
*/
addOption(scriptName, optionName, defaultValue = '', dataType = 'string') {
const scriptSlug = toSlug(scriptName);
const optionSlug = toSlug(optionName);
const uniqueSlug = `${SOMU.keyPrefix}${scriptSlug}:${optionSlug}`;
let scriptHeader = this.sidebar.find(`.somu-${scriptSlug}`);
// If option has already been added, do nothing
if ($('.' + uniqueSlug).length > 0) {
console.log('Option has already been added!', uniqueSlug);
return false;
}
// If scriptname header not found yet, insert header
if (scriptHeader.length === 0) {
scriptHeader = $(`<h3 class="title-section somu-${scriptSlug}">${scriptName}</h3>`).prependTo(this.sidebarContent);
}
// Get option value from store
const currValue = SOMU.getOptionValue(scriptName, optionName, defaultValue, dataType);
// Build field HTML
let fieldHtml = '';
if (dataType === 'bool') {
fieldHtml = `<input type="checkbox" class="input" name="${uniqueSlug}" data-datatype="bool" data-currentvalue="${currValue}" data-defaultvalue="${defaultValue}" ${currValue === true ? 'checked="checked"' : ''} />`;
}
else if (dataType === 'string') {
fieldHtml = `<textarea class="input" name="${uniqueSlug}" data-datatype="string" data-currentvalue="${currValue}" data-defaultvalue="${defaultValue}">${currValue}</textarea>`;
}
// Insert option under header
const optionElem = $(`<div class="col-12 details somu-${uniqueSlug}">
<div class="info-header">${optionName}:</div>
<div class="info-value">
${fieldHtml}
<span class="somu-delete" title="reset value to default value">Del</span><span class="somu-save" title="save changes">Save</span>
</div></div>`).insertAfter(scriptHeader);
optionElem.find('.input').trigger('change');
this.sidebar.removeClass('no-items');
},
handleSidebarEvents: function () {
$(this.sidebar)
.on('change keyup', '.input', function () {
if (this.dataset.datatype === 'bool') {
let currvalue = this.dataset.currentvalue === 'true';
let defaultvalue = this.dataset.defaultvalue === 'true';
$(this).toggleClass('js-changed', $(this).prop('checked') != currvalue);
$(this).toggleClass('js-notdefault', !$(this).hasClass('js-changed') && currvalue != defaultvalue);
}
else {
$(this).toggleClass('js-changed', this.dataset.currentvalue !== this.value.trim());
$(this).toggleClass('js-notdefault', !$(this).hasClass('js-changed') && this.dataset.currentvalue != this.dataset.defaultvalue);
}
})
.on('focus', '.input', function () {
SOMU.sidebar.addClass('focused');
})
.on('blur', '.input', function () {
SOMU.sidebar.removeClass('focused');
})
.on('click', '.somu-save', function () {
const $el = $(this).prevAll('.input').removeClass('js-changed');
const el = $el.get(0);
if (el.dataset.datatype === 'bool') {
el.dataset.currentvalue = $el.prop('checked');
}
else {
el.dataset.currentvalue = el.value;
}
$el.trigger('change');
SOMU.saveOptionValue(el.name, el.dataset.currentvalue);
})
.on('click', '.somu-delete', function () {
const $el = $(this).prevAll('.input');
const el = $el.get(0);
el.dataset.currentvalue = el.dataset.defaultvalue;
if (el.dataset.datatype === 'bool') {
$el.prop('checked', el.dataset.defaultvalue === 'true');
}
else {
el.value = el.dataset.currentvalue;
}
$el.trigger('change');
SOMU.saveOptionValue(el.name, el.dataset.currentvalue);
})
.on('click', '.title-section', function () {
$(this).nextUntil('.title-section').toggle();
});
},
appendStyles: function () {
const styles = document.createElement('style');
styles.setAttribute('data-somu', GM_info?.script.name);
styles.innerHTML = `
#somusidebar {
position: fixed;
z-index: 8950;
top: 44px;
left: 100%;
width: calc(100% - 250px);
height: calc(100vh - 43px);
max-width: 420px;
padding: 10px 5px 40px;
background: var(--white);
opacity: 0.7;
border: 1px solid var(--black-150);
box-shadow: -2px 2px 14px -3px rgba(0,0,0,0.25);
}
#somusidebar:after {
content: 'somu';
position: absolute;
right: 100%;
top: 5px;
width: auto;
height: 30px;
padding: 5px 8px;
background: var(--white);
border: 1px solid var(--black-150);
border-right: none;
box-shadow: -3px 2px 10px -2px rgba(0,0,0,0.25);
}
#somusidebar.no-items {
visibility: hidden;
pointer-events: none;
z-index: -1;
}
.somusidebar-open #somusidebar,
#somusidebar.focused,
#somusidebar:hover {
right: -1px;
left: initial;
opacity: 1;
}
.somusidebar-open #somusidebar {
top: 50px;
box-shadow: none;
}
.somusidebar-open #somusidebar:after {
display: none;
}
.somusidebar-compact #somusidebar {
top: 0px;
height: 100vh;
}
.somusidebar-compact #somusidebar:after {
top: 49px;
}
#somusidebar .title-section {
cursor: pointer;
}
#somusidebar .details {
margin-bottom: 15px;
}
#somusidebar .info-value {
position: relative;
margin: 3px 0 10px;
}
#somusidebar .info-value .input {
display: block;
width: 100%;
margin: 0;
padding: 5px 7px;
padding-right: 45px;
border: 1px solid var(--black-150);
}
#somusidebar .info-value .input[type="checkbox"] {
width: auto;
}
#somusidebar .info-value .input ~ span {
position: absolute;
display: none;
top: 0;
right: 0px;
width: auto;
height: 100%;
padding: 16px 5px;
font-size: 0.85em;
text-transform: uppercase;
background: var(--black-500);
color: var(--white);
cursor: pointer;
}
#somusidebar .info-value .input[type="checkbox"] ~ span {
top: -11px;
height: auto;
padding: 5px 5px;
}
#somusidebar .info-value .somu-save:hover {
background :var(--green-400);
}
#somusidebar .info-value .somu-delete:hover {
background: var(--red-500);
}
#somusidebar .info-value .input.js-notdefault:not(.js-changed) ~ .somu-delete,
#somusidebar .info-value .input.js-changed ~ .somu-save {
display: block;
}
#somusidebar span.info {
position: absolute;
bottom: 0;
left: 0;
padding: 5px;
font-size: 0.85em;
font-style: italic;
color: var(--red-600);
}
#somusidebar-content {
clear: both;
position: absolute;
top: 10px;
left: 10px;
right: 0;
bottom: 24px;
padding-right: 10px;
overflow-y: auto;
}
`;
document.body.appendChild(styles);
},
init() {
// Run validation
if (typeof jQuery === 'undefined') {
console.error('SOMU Options - jQuery not found!');
return;
}
if (this.hasInit) {
console.error('SOMU Options - Userscript has already initialized!');
return;
}
this.hasInit = true;
this.appendStyles();
this.sidebar = $(`<div class="col-12 mod-links no-items" id="somusidebar"><span class="info">Reload the page after you're done making changes for values to take effect.</span></div>`).appendTo('body');
this.sidebarContent = $(`<div id="somusidebar-content"></div>`).prependTo(this.sidebar);
this.handleSidebarEvents();
$(window).on('load resize', function () {
//$('body').toggleClass('somusidebar-open', $(document).width() >= 1800);
$('body').toggleClass('somusidebar-compact', $(window).height() <= 680);
});
}
});
SOMU.init();
// For testing purposes
// SOMU.addOption('Test Script', 'Testing Option', 'default value');