-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptions.js
276 lines (247 loc) · 10.5 KB
/
options.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
var defaultKey = {
togglePlayAndPauseKeyCode: 'p',
jumpToBeginningKeyCode: 'h',
jumpToEndKeyCode: 'e',
rewindTimeKeyCode: 'a',
advanceTimeKeyCode: 's',
speedDownKeyCode: 'd',
speedUpKeyCode: 'u',
resetSpeedKeyCode: 'r',
toggleFullscreenKeyCode: 'f',
getNextVideoElementKeyCode: 'n',
partialLoopKeyCode: 'l',
partialLoopPrecision: 100,
skipTimeAmount: 5,
playOrPauseWhenLoadingSelect: 'default',
showOrHideProgressBarSelect: 'default',
scrollToPlayerChecked: false,
rememberPlaybackSpeedChecked: true,
};
$(function() {
loadOptions();
$('#save').on('click', saveOptions);
$('#reset').on('click', resetOptions);
initShortcutInput('toggle-play-and-pause');
initShortcutInput('jump-to-beginning');
initShortcutInput('jump-to-end');
initShortcutInput('rewind-time');
initShortcutInput('advance-time');
initShortcutInput('speed-down');
initShortcutInput('speed-up');
initShortcutInput('reset-speed');
initShortcutInput('toggle-fullscreen');
initShortcutInput('get-next-video-element');
initShortcutInput('partial-loop');
initNumericInput('partial-loop-precision');
initNumericInput('skip-time-amount');
});
function loadOptions() {
chrome.storage.sync.get(defaultKey, function(storage) {
// key
updateInputText('toggle-play-and-pause', storage.togglePlayAndPauseKeyCode);
updateInputText('jump-to-beginning', storage.jumpToBeginningKeyCode);
updateInputText('jump-to-end', storage.jumpToEndKeyCode);
updateInputText('rewind-time', storage.rewindTimeKeyCode);
updateInputText('advance-time', storage.advanceTimeKeyCode);
updateInputText('speed-down', storage.speedDownKeyCode);
updateInputText('speed-up', storage.speedUpKeyCode);
updateInputText('reset-speed', storage.resetSpeedKeyCode);
updateInputText('toggle-fullscreen', storage.toggleFullscreenKeyCode);
updateInputText('get-next-video-element', storage.getNextVideoElementKeyCode);
updateInputText('partial-loop', storage.partialLoopKeyCode);
// numeric
document.getElementById('partial-loop-precision').value = storage.partialLoopPrecision;
document.getElementById('skip-time-amount').value = storage.skipTimeAmount;
// select menu
document.getElementById('play-or-pause-when-loading').value = storage.playOrPauseWhenLoadingSelect;
document.getElementById('show-or-hide-progress-bar').value = storage.showOrHideProgressBarSelect;
// check box
document.getElementById('scroll-to-player').checked = storage.scrollToPlayerChecked;
document.getElementById('remember-playback-speed').checked = storage.rememberPlaybackSpeedChecked;
});
}
function updateInputText(inputID, keyCode) {
document.getElementById(inputID).value = keyCode;
document.getElementById(inputID).keyCode = keyCode;
}
function saveOptions() {
var togglePlayAndPauseKeyCode = document.getElementById('toggle-play-and-pause').value;
var jumpToBeginningKeyCode = document.getElementById('jump-to-beginning').value;
var jumpToEndKeyCode = document.getElementById('jump-to-end').value;
var rewindTimeKeyCode = document.getElementById('rewind-time').value;
var advanceTimeKeyCode = document.getElementById('advance-time').value;
var speedDownKeyCode = document.getElementById('speed-down').value;
var speedUpKeyCode = document.getElementById('speed-up').value;
var resetSpeedKeyCode = document.getElementById('reset-speed').value;
var toggleFullscreenKeyCode = document.getElementById('toggle-fullscreen').value;
var getNextVideoElementKeyCode = document.getElementById('get-next-video-element').value;
var partialLoopKeyCode = document.getElementById('partial-loop').value;
var partialLoopPrecision = document.getElementById('partial-loop-precision').value;
var skipTimeAmount = document.getElementById('skip-time-amount').value;
var playOrPauseWhenLoadingSelect = document.getElementById('play-or-pause-when-loading').value;
var showOrHideProgressBarSelect = document.getElementById('show-or-hide-progress-bar').value;
var scrollToPlayerChecked = document.getElementById('scroll-to-player').checked;
var rememberPlaybackSpeedChecked = document.getElementById('remember-playback-speed').checked;
var validateFlag = [];
validateFlag[0] = checkValidate('toggle-play-and-pause');
validateFlag[1] = checkValidate('jump-to-beginning');
validateFlag[2] = checkValidate('jump-to-end');
validateFlag[3] = checkValidate('rewind-time');
validateFlag[4] = checkValidate('advance-time');
validateFlag[5] = checkValidate('speed-down');
validateFlag[6] = checkValidate('speed-up');
validateFlag[7] = checkValidate('reset-speed');
validateFlag[8] = checkValidate('toggle-fullscreen');
validateFlag[9] = checkValidate('get-next-video-element');
validateFlag[10] = checkValidate('partial-loop');
validateFlag[11] = checkValidateNumeric('partial-loop-precision');
validateFlag[12] = checkValidateNumeric('skip-time-amount');
validateFlag[13] = checkValidateSelect('play-or-pause-when-loading', ['default', 'play', 'pause']);
validateFlag[14] = checkValidateSelect('show-or-hide-progress-bar', ['default', 'show', 'hide']);
validateFlag[15] = checkValidateChecked('scroll-to-player');
validateFlag[16] = checkValidateChecked('remember-playback-speed');
// when some input is wrong.
for (var i = 0; i < validateFlag.length; i++) {
if (validateFlag[i] === false) {
return false;
}
}
chrome.storage.sync.set({
togglePlayAndPauseKeyCode: togglePlayAndPauseKeyCode,
jumpToBeginningKeyCode: jumpToBeginningKeyCode,
jumpToEndKeyCode: jumpToEndKeyCode,
rewindTimeKeyCode: rewindTimeKeyCode,
advanceTimeKeyCode: advanceTimeKeyCode,
speedDownKeyCode: speedDownKeyCode,
speedUpKeyCode: speedUpKeyCode,
resetSpeedKeyCode: resetSpeedKeyCode,
toggleFullscreenKeyCode: toggleFullscreenKeyCode,
getNextVideoElementKeyCode: getNextVideoElementKeyCode,
partialLoopKeyCode: partialLoopKeyCode,
partialLoopPrecision: partialLoopPrecision,
skipTimeAmount: skipTimeAmount,
playOrPauseWhenLoadingSelect: playOrPauseWhenLoadingSelect,
showOrHideProgressBarSelect: showOrHideProgressBarSelect,
scrollToPlayerChecked: scrollToPlayerChecked,
rememberPlaybackSpeedChecked: rememberPlaybackSpeedChecked
}, function() {
var status = $('#status');
status.text('Saved');
setTimeout(function() {
status.text('');
}, 1500);
});
}
function resetOptions() {
chrome.storage.sync.set(defaultKey, function() {
loadOptions();
// reset error marks
for (var i = 0; i < $('input').length; i++) {
$('input').eq(i).parent().children('.invalid-value').remove();
$('input').eq(i).css('border', '1px solid #cccccc');
}
var status = $('#status');
status.text('Reset');
setTimeout(function() {
status.text('');
}, 1500);
});
}
function initShortcutInput(inputID) {
document.getElementById(inputID).addEventListener('focus', inputFocus);
document.getElementById(inputID).addEventListener('blur', inputBlur);
document.getElementById(inputID).addEventListener('keypress', recordKeypress);
}
function initNumericInput(inputID) {
document.getElementById(inputID).addEventListener('focus', function() {
$(this).css('border', '1px solid #cccccc');
});
}
function inputFocus(event) {
event.target.value = '';
$(this).css('border', '1px solid #cccccc');
};
function inputBlur(event) {
event.target.value = event.target.keyCode;
}
function recordKeypress(event) {
var nomalizedChar = encodeYenSignToBackslash(event.key);
event.target.value = nomalizedChar;
event.target.keyCode = nomalizedChar;
event.preventDefault();
event.stopPropagation();
}
function encodeYenSignToBackslash(key) {
// 165 -> Yen Sign
if (key.charCodeAt() == 165) {
key = '\\';
}
return key;
}
function checkValidate(inputID) {
var inputID = '#' + inputID;
$(inputID).parent().children('.invalid-value').remove();
if ($(inputID).val().match(/^[0-9a-zA-Z-^\\@\[\];:,./_=~|`{}+*<>?!"#$%&'()]{1}$/) === null && $(inputID).val().match(/^Enter$/) === null) {
// some input is wrong.
$(inputID).css('border', '1px solid red');
$(inputID).parent().append('<div class="invalid-value">Invalid value</div>');
var errorFlag = true;
} else {
$(inputID).css('border', '1px solid #cccccc');
}
// return value: true -> save the options, false -> do not save.
if (errorFlag === true) {
return false;
} else {
return true;
}
}
function checkValidateNumeric(inputID) {
var inputID = '#' + inputID;
$(inputID).parent().children('.invalid-value').remove();
if ($(inputID).val().match(/^[0-9]+$/) === null) {
// numeric pattern is wrong.
$(inputID).css('border', '1px solid red');
$(inputID).parent().append('<div class="invalid-value">Invalid value</div>');
var errorFlag = true;
} else {
$(inputID).css('border', '1px solid #cccccc');
}
// return value: true -> save the options, false -> do not save.
if (errorFlag === true) {
return false;
} else {
return true;
}
}
function checkValidateChecked(inputID) {
var inputID = '#' + inputID;
$(inputID).parent().children('.invalid-value').remove();
if ($(inputID).prop('checked') !== true && $(inputID).prop('checked') !== false) {
// checkbox value is wrong
$(inputID).css('border', '1px solid red');
$(inputID).parent().append('<div class="invalid-value">Invalid value</div>');
var errorFlag = true;
} else {
$(inputID).css('border', '1px solid #cccccc');
}
// return value: true -> save the options, false -> do not save.
if (errorFlag === true) {
return false;
} else {
return true;
}
}
function checkValidateSelect(inputID, menu) {
var inputID = '#' + inputID;
$(inputID).parent().children('.invalid-value').remove();
if (menu.includes($(inputID).val())) {
$(inputID).css('border', '1px solid #cccccc');
return true;
}
else {
$(inputID).css('border', '1px solid red');
$(inputID).parent().append('<div class="invalid-value">Invalid value</div>');
return false;
}
}