-
-
Notifications
You must be signed in to change notification settings - Fork 865
Expand file tree
/
Copy pathsettings.js
More file actions
687 lines (558 loc) · 20.1 KB
/
Copy pathsettings.js
File metadata and controls
687 lines (558 loc) · 20.1 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
document.title = l('settingsPreferencesHeading') + ' | Min'
var contentTypeBlockingContainer = document.getElementById('content-type-blocking')
var banner = document.getElementById('restart-required-banner')
var siteThemeCheckbox = document.getElementById('checkbox-site-theme')
var showDividerCheckbox = document.getElementById('checkbox-show-divider')
var showFaviconTabsCheckbox = document.getElementById('checkbox-show-favicon-tabs')
var userscriptsCheckbox = document.getElementById('checkbox-userscripts')
var userscriptsShowDirectorySection = document.getElementById('userscripts-show-directory')
var separateTitlebarCheckbox = document.getElementById('checkbox-separate-titlebar')
var openTabsInForegroundCheckbox = document.getElementById('checkbox-open-tabs-in-foreground')
var autoPlayCheckbox = document.getElementById('checkbox-enable-autoplay')
var userAgentCheckbox = document.getElementById('checkbox-user-agent')
var userAgentInput = document.getElementById('input-user-agent')
function showRestartRequiredBanner () {
banner.hidden = false
settings.set('restartNow', true)
}
settings.get('restartNow', (value) => {
if (value === true) {
showRestartRequiredBanner()
}
})
/* content blocking settings */
var trackingLevelContainer = document.getElementById('tracking-level-container')
var trackingLevelOptions = Array.from(trackingLevelContainer.querySelectorAll('input[name=blockingLevel]'))
var blockingExceptionsContainer = document.getElementById('content-blocking-information')
var blockingExceptionsInput = document.getElementById('content-blocking-exceptions')
var blockedRequestCount = document.querySelector('#content-blocking-blocked-requests strong')
settings.listen('filteringBlockedCount', function (value) {
var count = value || 0
var valueStr
if (count > 50000) {
valueStr = new Intl.NumberFormat(navigator.locale, { notation: 'compact', maximumSignificantDigits: 4 }).format(count)
} else {
valueStr = new Intl.NumberFormat().format(count)
}
blockedRequestCount.textContent = valueStr
})
function updateBlockingLevelUI (level) {
var radio = trackingLevelOptions[level]
radio.checked = true
if (level === 0) {
blockingExceptionsContainer.hidden = true
} else {
blockingExceptionsContainer.hidden = false
radio.parentNode.appendChild(blockingExceptionsContainer)
}
if (document.querySelector('#tracking-level-container .setting-option.selected')) {
document.querySelector('#tracking-level-container .setting-option.selected').classList.remove('selected')
}
radio.parentNode.classList.add('selected')
}
function changeBlockingLevelSetting (level) {
settings.get('filtering', function (value) {
if (!value) {
value = {}
}
value.blockingLevel = level
settings.set('filtering', value)
updateBlockingLevelUI(level)
})
}
function setExceptionInputSize () {
blockingExceptionsInput.style.height = (blockingExceptionsInput.scrollHeight + 2) + 'px'
}
settings.get('filtering', function (value) {
// migrate from old settings (<v1.9.0)
if (value && typeof value.trackers === 'boolean') {
if (value.trackers === true) {
value.blockingLevel = 2
} else if (value.trackers === false) {
value.blockingLevel = 0
}
delete value.trackers
settings.set('filtering', value)
}
if (value && value.blockingLevel !== undefined) {
updateBlockingLevelUI(value.blockingLevel)
} else {
updateBlockingLevelUI(1)
}
if (value && value.exceptionDomains && value.exceptionDomains.length > 0) {
blockingExceptionsInput.value = value.exceptionDomains.join(', ') + ', '
setExceptionInputSize()
}
})
trackingLevelOptions.forEach(function (item, idx) {
item.addEventListener('change', function () {
changeBlockingLevelSetting(idx)
})
})
blockingExceptionsInput.addEventListener('input', function () {
setExceptionInputSize()
// remove protocols because of https://github.com/minbrowser/min/issues/1428
var newValue = this.value.split(',').map(i => i.trim().replace('http://', '').replace('https://', '')).filter(i => !!i)
settings.get('filtering', function (value) {
if (!value) {
value = {}
}
value.exceptionDomains = newValue
settings.set('filtering', value)
})
})
/* content type settings */
var contentTypes = {
// humanReadableName: contentType
scripts: 'script',
images: 'image'
}
// used for showing localized strings
var contentTypeSettingNames = {
scripts: 'settingsBlockScriptsToggle',
images: 'settingsBlockImagesToggle'
}
for (var contentType in contentTypes) {
(function (contentType) {
settings.get('filtering', function (value) {
// create the settings section for blocking each content type
var section = document.createElement('div')
section.classList.add('setting-section')
var id = 'checkbox-block-' + contentTypes[contentType]
var checkbox = document.createElement('input')
checkbox.type = 'checkbox'
checkbox.id = id
if (value && value.contentTypes) {
checkbox.checked = value.contentTypes.indexOf(contentTypes[contentType]) != -1
}
var label = document.createElement('label')
label.setAttribute('for', id)
label.textContent = l(contentTypeSettingNames[contentType])
section.appendChild(checkbox)
section.appendChild(label)
contentTypeBlockingContainer.appendChild(section)
checkbox.addEventListener('change', function (e) {
settings.get('filtering', function (value) {
if (!value) {
value = {}
}
if (!value.contentTypes) {
value.contentTypes = []
}
if (e.target.checked) { // add the item to the array
value.contentTypes.push(contentTypes[contentType])
} else { // remove the item from the array
var idx = value.contentTypes.indexOf(contentTypes[contentType])
value.contentTypes.splice(idx, 1)
}
settings.set('filtering', value)
})
})
})
})(contentType)
}
/* dark mode setting */
var darkModeNever = document.getElementById('dark-mode-never')
var darkModeNight = document.getElementById('dark-mode-night')
var darkModeAlways = document.getElementById('dark-mode-always')
var darkModeSystem = document.getElementById('dark-mode-system')
// -1 - off ; 0 - auto ; 1 - on
settings.get('darkMode', function (value) {
darkModeNever.checked = (value === -1)
darkModeNight.checked = (value === 0)
darkModeAlways.checked = (value === 1 || value === true)
darkModeSystem.checked = (value === 2 || value === undefined || value === false)
})
darkModeNever.addEventListener('change', function (e) {
if (this.checked) {
settings.set('darkMode', -1)
}
})
darkModeNight.addEventListener('change', function (e) {
if (this.checked) {
settings.set('darkMode', 0)
}
})
darkModeAlways.addEventListener('change', function (e) {
if (this.checked) {
settings.set('darkMode', 1)
}
})
darkModeSystem.addEventListener('change', function (e) {
if (this.checked) {
settings.set('darkMode', 2)
}
})
/* site theme setting */
settings.get('siteTheme', function (value) {
if (value === true || value === undefined) {
siteThemeCheckbox.checked = true
} else {
siteThemeCheckbox.checked = false
}
})
siteThemeCheckbox.addEventListener('change', function (e) {
settings.set('siteTheme', this.checked)
})
/* startup settings */
var startupSettingInput = document.getElementById('startup-options')
settings.get('startupTabOption', function(value = 2) {
startupSettingInput.value = value
})
startupSettingInput.addEventListener('change', function() {
settings.set('startupTabOption', parseInt(this.value))
})
/* new window settings */
var newWindowSettingInput = document.getElementById('new-window-options')
settings.get('newWindowOption', function(value = 1) {
newWindowSettingInput.value = value
})
newWindowSettingInput.addEventListener('change', function() {
settings.set('newWindowOption', parseInt(this.value))
})
/* userscripts setting */
settings.get('userscriptsEnabled', function (value) {
if (value === true) {
userscriptsCheckbox.checked = true
userscriptsShowDirectorySection.hidden = false
}
})
userscriptsCheckbox.addEventListener('change', function (e) {
settings.set('userscriptsEnabled', this.checked)
userscriptsShowDirectorySection.hidden = !this.checked
})
userscriptsShowDirectorySection.getElementsByTagName('a')[0].addEventListener('click', function() {
postMessage({ message: 'showUserscriptDirectory' })
})
/* show divider between tabs setting */
settings.get('showDividerBetweenTabs', function (value) {
if (value === true) {
showDividerCheckbox.checked = true
}
})
showDividerCheckbox.addEventListener('change', function (e) {
settings.set('showDividerBetweenTabs', this.checked)
})
/* show favicons in tabs setting */
settings.get('showFaviconInTabs', function (value) {
if (value === true) {
showFaviconTabsCheckbox.checked = true
}
})
showFaviconTabsCheckbox.addEventListener('change', function (e) {
settings.set('showFaviconInTabs', this.checked)
})
/* language setting*/
var languagePicker = document.getElementById('setting-language-picker')
for (var language in languages) { //from localization.build.js
var item = document.createElement('option')
item.textContent = languages[language].name
item.value = languages[language].identifier
languagePicker.appendChild(item)
}
languagePicker.value = getCurrentLanguage()
languagePicker.addEventListener('change', function () {
settings.set('userSelectedLanguage', this.value)
showRestartRequiredBanner()
})
/* separate titlebar setting */
settings.get('useSeparateTitlebar', function (value) {
if (value === true) {
separateTitlebarCheckbox.checked = true
}
})
separateTitlebarCheckbox.addEventListener('change', function (e) {
settings.set('useSeparateTitlebar', this.checked)
showRestartRequiredBanner()
})
/* tabs in foreground setting */
settings.get('openTabsInForeground', function (value) {
if (value === true) {
openTabsInForegroundCheckbox.checked = true
}
})
openTabsInForegroundCheckbox.addEventListener('change', function (e) {
settings.set('openTabsInForeground', this.checked)
})
/* media autoplay setting */
settings.get('enableAutoplay', function (value) {
autoPlayCheckbox.checked = value
})
autoPlayCheckbox.addEventListener('change', function (e) {
settings.set('enableAutoplay', this.checked)
})
/* user agent settting */
settings.get('customUserAgent', function (value) {
if (value) {
userAgentCheckbox.checked = true
userAgentInput.style.visibility = 'visible'
userAgentInput.value = value
}
})
userAgentCheckbox.addEventListener('change', function (e) {
if (this.checked) {
userAgentInput.style.visibility = 'visible'
} else {
settings.set('customUserAgent', null)
userAgentInput.style.visibility = 'hidden'
showRestartRequiredBanner()
}
})
userAgentInput.addEventListener('input', function (e) {
if (this.value) {
settings.set('customUserAgent', this.value)
} else {
settings.set('customUserAgent', null)
}
showRestartRequiredBanner()
})
/* update notifications setting */
var updateNotificationsCheckbox = document.getElementById('checkbox-update-notifications')
settings.get('updateNotificationsEnabled', function (value) {
if (value === false) {
updateNotificationsCheckbox.checked = false
} else {
updateNotificationsCheckbox.checked = true
}
})
updateNotificationsCheckbox.addEventListener('change', function (e) {
settings.set('updateNotificationsEnabled', this.checked)
})
/* usage statistics setting */
var usageStatisticsCheckbox = document.getElementById('checkbox-usage-statistics')
settings.get('collectUsageStats', function (value) {
if (value === false) {
usageStatisticsCheckbox.checked = false
} else {
usageStatisticsCheckbox.checked = true
}
})
usageStatisticsCheckbox.addEventListener('change', function (e) {
settings.set('collectUsageStats', this.checked)
})
/* default search engine setting */
var searchEngineDropdown = document.getElementById('default-search-engine')
var searchEngineInput = document.getElementById('custom-search-engine')
searchEngineInput.setAttribute('placeholder', l('customSearchEngineDescription'))
settings.onLoad(function () {
if (currentSearchEngine.custom) {
searchEngineInput.hidden = false
searchEngineInput.value = currentSearchEngine.searchURL
}
for (var searchEngine in searchEngines) {
var item = document.createElement('option')
item.textContent = searchEngines[searchEngine].name
if (searchEngines[searchEngine].name == currentSearchEngine.name) {
item.setAttribute('selected', 'true')
}
searchEngineDropdown.appendChild(item)
}
// add custom option
item = document.createElement('option')
item.textContent = 'custom'
if (currentSearchEngine.custom) {
item.setAttribute('selected', 'true')
}
searchEngineDropdown.appendChild(item)
})
searchEngineDropdown.addEventListener('change', function (e) {
if (this.value === 'custom') {
searchEngineInput.hidden = false
} else {
searchEngineInput.hidden = true
settings.set('searchEngine', { name: this.value })
}
})
searchEngineInput.addEventListener('input', function (e) {
settings.set('searchEngine', { url: this.value })
})
/* key map settings */
settings.get('keyMap', function (keyMapSettings) {
var keyMap = userKeyMap(keyMapSettings)
var keyMapList = document.getElementById('key-map-list')
Object.keys(keyMap).forEach(function (action) {
var li = createKeyMapListItem(action, keyMap)
keyMapList.appendChild(li)
})
})
function formatCamelCase (text) {
var result = text.replace(/([a-z])([A-Z])/g, '$1 $2')
return result.charAt(0).toUpperCase() + result.slice(1)
}
function createKeyMapListItem (action, keyMap) {
var li = document.createElement('li')
var label = document.createElement('label')
var input = document.createElement('input')
label.innerText = formatCamelCase(action)
label.htmlFor = action
input.type = 'text'
input.id = input.name = action
input.value = formatKeyValue(keyMap[action])
input.addEventListener('input', onKeyMapChange)
li.appendChild(label)
li.appendChild(input)
return li
}
function formatKeyValue (value) {
// multiple shortcuts should be separated by commas
if (value instanceof Array) {
value = value.join(', ')
}
// use either command or ctrl depending on the platform
if (navigator.platform === 'MacIntel') {
value = value.replace(/\bmod\b/g, 'command')
} else {
value = value.replace(/\bmod\b/g, 'ctrl')
value = value.replace(/\boption\b/g, 'alt')
}
if (navigator.platform === 'Win32') {
value = value.replace(/\bsuper\b/g, 'win')
}
return value
}
function parseKeyInput (input) {
// input may be a single mapping or multiple mappings comma separated.
var parsed = input.toLowerCase().split(',')
parsed = parsed.map(function (e) { return e.trim() })
// Remove empty
parsed = parsed.filter(Boolean)
// convert key names back to generic equivalents
parsed = parsed.map(function (e) {
if (navigator.platform === 'MacIntel') {
e = e.replace(/\b(command)|(cmd)\b/g, 'mod')
} else {
e = e.replace(/\b(control)|(ctrl)\b/g, 'mod')
e = e.replace(/\balt\b/g, 'option')
e = e.replace(/\bwin\b/g, 'super')
}
return e
})
return parsed.length > 1 ? parsed : parsed[0]
}
function onKeyMapChange (e) {
var action = this.name
var newValue = this.value
settings.get('keyMap', function (keyMapSettings) {
if (!keyMapSettings) {
keyMapSettings = {}
}
keyMapSettings[action] = parseKeyInput(newValue)
settings.set('keyMap', keyMapSettings)
showRestartRequiredBanner()
})
}
/* Password auto-fill settings */
var passwordManagersDropdown = document.getElementById('selected-password-manager')
for (var manager in passwordManagers) {
var item = document.createElement('option')
item.textContent = passwordManagers[manager].name
passwordManagersDropdown.appendChild(item)
}
settings.listen('passwordManager', function (value) {
passwordManagersDropdown.value = currentPasswordManager.name
})
passwordManagersDropdown.addEventListener('change', function (e) {
if (this.value === 'none') {
settings.set('passwordManager', { name: 'none' })
} else {
settings.set('passwordManager', { name: this.value })
}
})
var keychainViewLink = document.getElementById('keychain-view-link')
keychainViewLink.addEventListener('click', function () {
postMessage({ message: 'showCredentialList' })
})
settings.listen('passwordManager', function (value) {
keychainViewLink.hidden = !(currentPasswordManager.name === 'Built-in password manager')
})
/* proxy settings */
const proxyTypeInput = document.getElementById('selected-proxy-type')
const proxyInputs = Array.from(document.querySelectorAll('#proxy-settings-container input'))
const toggleProxyOptions = proxyType => {
document.getElementById('manual-proxy-section').hidden = proxyType != 1
document.getElementById('pac-option').hidden = proxyType != 2
}
const setProxy = (key, value) => {
settings.get('proxy', (proxy = {}) => {
proxy[key] = value
settings.set('proxy', proxy)
})
}
settings.get('proxy', (proxy = {}) => {
toggleProxyOptions(proxy.type)
proxyTypeInput.options.selectedIndex = proxy.type || 0
proxyInputs.forEach(item => item.value = proxy[item.name] || '')
})
proxyTypeInput.addEventListener('change', e => {
const proxyType = e.target.options.selectedIndex
setProxy('type', proxyType)
toggleProxyOptions(proxyType)
})
proxyInputs.forEach(item => item.addEventListener('change', e => setProxy(e.target.name, e.target.value)))
settings.get('customBangs', (value) => {
const bangslist = document.getElementById('custom-bangs')
if (value) {
value.forEach(function (bang) {
bangslist.appendChild(createBang(bang.phrase, bang.snippet, bang.redirect))
})
}
})
document.getElementById('add-custom-bang').addEventListener('click', function () {
const bangslist = document.getElementById('custom-bangs')
const newListItem = createBang()
bangslist.appendChild(newListItem)
document.body.scrollBy(0, Math.round(newListItem.getBoundingClientRect().height))
})
function createBang (bang, snippet, redirect) {
var li = document.createElement('li')
var bangInput = document.createElement('input')
var snippetInput = document.createElement('input')
var redirectInput = document.createElement('input')
var xButton = document.createElement('button')
var current = { phrase: bang ?? '', snippet: snippet ?? '', redirect: redirect ?? '' }
function update (key, input) {
settings.get('customBangs', function (d) {
const filtered = d ? d.filter((bang) => bang.phrase !== current.phrase && (key !== 'phrase' || bang.phrase !== input.value)) : []
filtered.push({ phrase: bangInput.value, snippet: snippetInput.value, redirect: redirectInput.value })
settings.set('customBangs', filtered)
current[key] = input.value
})
}
bangInput.type = 'text'
snippetInput.type = 'text'
redirectInput.type = 'text'
bangInput.value = bang ?? ''
snippetInput.value = snippet ?? ''
redirectInput.value = redirect ?? ''
xButton.className = 'i carbon:close custom-bang-delete-button'
bangInput.placeholder = l('settingsCustomBangsPhrase')
snippetInput.placeholder = l('settingsCustomBangsSnippet')
redirectInput.placeholder = l('settingsCustomBangsRedirect')
xButton.addEventListener('click', function () {
li.remove()
settings.get('customBangs', (d) => {
settings.set('customBangs', d.filter((bang) => bang.phrase !== bangInput.value))
})
showRestartRequiredBanner()
})
bangInput.addEventListener('change', function () {
if (this.value.startsWith('!')) {
this.value = this.value.slice(1)
}
update('phrase', bangInput)
showRestartRequiredBanner()
})
snippetInput.addEventListener('change', function () {
update('snippet', snippetInput)
showRestartRequiredBanner()
})
redirectInput.addEventListener('change', function () {
update('redirect', redirectInput)
showRestartRequiredBanner()
})
li.appendChild(bangInput)
li.appendChild(snippetInput)
li.appendChild(redirectInput)
li.appendChild(xButton)
return li
}