avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 1 | var savedSuccessfullyTimeout = null; |
| 2 | |
| 3 | const exclusiveOptions = [['thread', 'threadall']]; |
| 4 | |
| 5 | // Get the value of the option set in the options/experiments page |
| 6 | function getOptionValue(opt) { |
| 7 | if (specialOptions.includes(opt)) { |
| 8 | switch (opt) { |
| 9 | case 'profileindicatoralt_months': |
| 10 | return document.getElementById(opt).value || 12; |
| 11 | |
| 12 | case 'ccdarktheme_mode': |
| 13 | return document.getElementById(opt).value || 'switch'; |
| 14 | |
| 15 | case 'ccdragndropfix': |
| 16 | return document.getElementById(opt).checked || false; |
| 17 | |
| 18 | default: |
| 19 | console.warn('Unrecognized option: ' + opt); |
| 20 | return undefined; |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | return document.getElementById(opt).checked || false; |
| 25 | } |
| 26 | |
| 27 | // Returns whether the option is included in the current context |
| 28 | function isOptionShown(opt) { |
| 29 | if (!optionsPrototype.hasOwnProperty(opt)) return false; |
| 30 | return optionsPrototype[opt].context == window.CONTEXT; |
| 31 | } |
| 32 | |
| 33 | function save(e) { |
| 34 | // Validation checks before saving |
| 35 | if (isOptionShown('profileindicatoralt_months')) { |
| 36 | var months = document.getElementById('profileindicatoralt_months'); |
| 37 | if (!months.checkValidity()) { |
| 38 | console.warn(months.validationMessage); |
| 39 | return; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | e.preventDefault(); |
| 44 | |
| 45 | chrome.storage.sync.get(null, function(items) { |
| 46 | var options = cleanUpOptions(items, true); |
| 47 | |
| 48 | // Save |
| 49 | Object.keys(options).forEach(function(opt) { |
| 50 | if (!isOptionShown(opt)) return; |
| 51 | options[opt] = getOptionValue(opt); |
| 52 | }); |
| 53 | |
| 54 | chrome.storage.sync.set(options, function() { |
| 55 | window.close(); |
| 56 | |
| 57 | // In browsers like Firefox window.close is not supported: |
| 58 | if (savedSuccessfullyTimeout !== null) |
| 59 | window.clearTimeout(savedSuccessfullyTimeout); |
| 60 | |
| 61 | document.getElementById('save-indicator').innerText = |
| 62 | '✓ ' + chrome.i18n.getMessage('options_saved'); |
| 63 | savedSuccessfullyTimeout = window.setTimeout(_ => { |
| 64 | document.getElementById('save-indicator').innerText = ''; |
| 65 | }, 3699); |
| 66 | }); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | function i18n() { |
| 71 | document.querySelectorAll('[data-i18n]') |
| 72 | .forEach( |
| 73 | el => el.innerHTML = chrome.i18n.getMessage( |
| 74 | 'options_' + el.getAttribute('data-i18n'))); |
| 75 | } |
| 76 | |
| 77 | window.addEventListener('load', function() { |
| 78 | i18n(); |
| 79 | |
avm99963 | 7309b06 | 2021-04-22 12:41:08 +0200 | [diff] [blame] | 80 | if (window.CONTEXT == 'options' && !isReleaseVersion()) { |
| 81 | var experimentsLink = document.querySelector('.experiments-link'); |
| 82 | experimentsLink.removeAttribute('hidden'); |
| 83 | experimentsLink.addEventListener('click', _ => chrome.tabs.create({ |
| 84 | url: chrome.runtime.getURL('options/experiments.html'), |
| 85 | })); |
| 86 | } |
| 87 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 88 | chrome.storage.sync.get(null, function(items) { |
| 89 | items = cleanUpOptions(items, false); |
| 90 | |
| 91 | for ([opt, optMeta] of Object.entries(optionsPrototype)) { |
| 92 | if (!isOptionShown(opt)) continue; |
| 93 | |
| 94 | if (specialOptions.includes(opt)) { |
| 95 | switch (opt) { |
| 96 | case 'profileindicatoralt_months': |
| 97 | var input = document.createElement('input'); |
| 98 | input.type = 'number'; |
| 99 | input.id = 'profileindicatoralt_months'; |
| 100 | input.max = '12'; |
| 101 | input.min = '1'; |
| 102 | input.value = items[opt]; |
| 103 | input.required = true; |
| 104 | document.getElementById('profileindicatoralt_months--container') |
| 105 | .appendChild(input); |
| 106 | break; |
| 107 | |
| 108 | case 'ccdarktheme_mode': |
| 109 | var select = document.createElement('select'); |
| 110 | select.id = 'ccdarktheme_mode'; |
| 111 | |
| 112 | const modes = ['switch', 'system']; |
| 113 | for (const mode of modes) { |
| 114 | var modeOption = document.createElement('option'); |
| 115 | modeOption.value = mode; |
| 116 | modeOption.textContent = |
| 117 | chrome.i18n.getMessage('options_ccdarktheme_mode_' + mode); |
| 118 | if (items.ccdarktheme_mode == mode) modeOption.selected = true; |
| 119 | select.appendChild(modeOption); |
| 120 | } |
| 121 | |
| 122 | document.getElementById('ccdarktheme_mode--container') |
| 123 | .appendChild(select); |
| 124 | break; |
| 125 | |
| 126 | // Firefox doesn't support drag and dropping bookmarks into the text |
| 127 | // editor while preserving the bookmark title. |
| 128 | case 'ccdragndropfix': |
| 129 | var showOption = !isFirefox(); |
| 130 | if (showOption) { |
| 131 | document.getElementById('dragndrop-wrapper') |
| 132 | .removeAttribute('hidden'); |
| 133 | |
| 134 | if (items[opt] === true) |
| 135 | document.getElementById(opt).checked = true; |
| 136 | } |
| 137 | break; |
| 138 | |
| 139 | default: |
| 140 | console.warn('Unrecognized option: ' + opt); |
| 141 | break; |
| 142 | } |
| 143 | continue; |
| 144 | } |
| 145 | |
| 146 | if (items[opt] === true) document.getElementById(opt).checked = true; |
| 147 | } |
| 148 | |
| 149 | exclusiveOptions.forEach(exclusive => { |
| 150 | if (!isOptionShown(exclusive[0]) || !isOptionShown(exclusive[1])) return; |
| 151 | |
| 152 | exclusive.forEach( |
| 153 | el => document.getElementById(el).addEventListener('change', e => { |
| 154 | if (document.getElementById(exclusive[0]).checked && |
| 155 | document.getElementById(exclusive[1]).checked) { |
| 156 | document |
| 157 | .getElementById( |
| 158 | exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)]) |
| 159 | .checked = false; |
| 160 | } |
| 161 | })); |
| 162 | }); |
| 163 | document.querySelector('#save').addEventListener('click', save); |
| 164 | }); |
| 165 | }); |