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