Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 1 | import {getExtVersion, isProdVersion} from '../common/extUtils.js'; |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 2 | import {ensureOptPermissions, grantedOptPermissions, isPermissionsObjectEmpty, missingPermissions} from '../common/optionsPermissions.js'; |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 3 | import {cleanUpOptions, optionsPrototype, specialOptions} from '../common/optionsUtils.js'; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 4 | |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 5 | import optionsPage from './optionsPage.json5'; |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 6 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 7 | var savedSuccessfullyTimeout = null; |
| 8 | |
| 9 | const exclusiveOptions = [['thread', 'threadall']]; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 10 | const kClickShouldEnableFeat = 'data-click-should-enable-feature'; |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 11 | |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 12 | // Get a URL to a document which is part of the extension documentation (using |
| 13 | // |ref| as the Git ref). |
| 14 | function getDocURLWithRef(doc, ref) { |
| 15 | return 'https://gerrit.avm99963.com/plugins/gitiles/infinitegforums/+/' + |
| 16 | ref + '/docs/' + doc; |
| 17 | } |
| 18 | |
| 19 | // Get a URL to a document which is part of the extension documentation |
| 20 | // (autodetect the appropriate Git ref) |
| 21 | function getDocURL(doc) { |
Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 22 | if (!isProdVersion()) return getDocURLWithRef(doc, 'HEAD'); |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 23 | |
| 24 | var version = getExtVersion(); |
| 25 | return getDocURLWithRef(doc, 'refs/tags/v' + version); |
| 26 | } |
| 27 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 28 | // Get the value of the option set in the options/experiments page |
| 29 | function getOptionValue(opt) { |
| 30 | if (specialOptions.includes(opt)) { |
| 31 | switch (opt) { |
| 32 | case 'profileindicatoralt_months': |
| 33 | return document.getElementById(opt).value || 12; |
| 34 | |
| 35 | case 'ccdarktheme_mode': |
| 36 | return document.getElementById(opt).value || 'switch'; |
| 37 | |
Adrià Vilanova Martínez | 849ede6 | 2022-06-20 19:10:28 +0200 | [diff] [blame] | 38 | case 'interopthreadpage_mode': |
| 39 | return document.getElementById(opt).value || 'previous'; |
| 40 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 41 | default: |
| 42 | console.warn('Unrecognized option: ' + opt); |
| 43 | return undefined; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | return document.getElementById(opt).checked || false; |
| 48 | } |
| 49 | |
| 50 | // Returns whether the option is included in the current context |
| 51 | function isOptionShown(opt) { |
| 52 | if (!optionsPrototype.hasOwnProperty(opt)) return false; |
| 53 | return optionsPrototype[opt].context == window.CONTEXT; |
| 54 | } |
| 55 | |
| 56 | function save(e) { |
| 57 | // Validation checks before saving |
| 58 | if (isOptionShown('profileindicatoralt_months')) { |
| 59 | var months = document.getElementById('profileindicatoralt_months'); |
| 60 | if (!months.checkValidity()) { |
| 61 | console.warn(months.validationMessage); |
| 62 | return; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | e.preventDefault(); |
| 67 | |
| 68 | chrome.storage.sync.get(null, function(items) { |
| 69 | var options = cleanUpOptions(items, true); |
| 70 | |
| 71 | // Save |
| 72 | Object.keys(options).forEach(function(opt) { |
| 73 | if (!isOptionShown(opt)) return; |
| 74 | options[opt] = getOptionValue(opt); |
| 75 | }); |
| 76 | |
| 77 | chrome.storage.sync.set(options, function() { |
| 78 | window.close(); |
| 79 | |
| 80 | // In browsers like Firefox window.close is not supported: |
| 81 | if (savedSuccessfullyTimeout !== null) |
| 82 | window.clearTimeout(savedSuccessfullyTimeout); |
| 83 | |
| 84 | document.getElementById('save-indicator').innerText = |
| 85 | '✓ ' + chrome.i18n.getMessage('options_saved'); |
| 86 | savedSuccessfullyTimeout = window.setTimeout(_ => { |
| 87 | document.getElementById('save-indicator').innerText = ''; |
| 88 | }, 3699); |
| 89 | }); |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | function i18n() { |
| 94 | document.querySelectorAll('[data-i18n]') |
| 95 | .forEach( |
| 96 | el => el.innerHTML = chrome.i18n.getMessage( |
| 97 | 'options_' + el.getAttribute('data-i18n'))); |
| 98 | } |
| 99 | |
| 100 | window.addEventListener('load', function() { |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 101 | if (window.CONTEXT == 'options') { |
Adrià Vilanova Martínez | 0335b51 | 2022-01-21 13:34:59 +0100 | [diff] [blame] | 102 | if (!isProdVersion()) { |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 103 | var experimentsLink = document.querySelector('.experiments-link'); |
| 104 | experimentsLink.removeAttribute('hidden'); |
| 105 | experimentsLink.addEventListener('click', _ => chrome.tabs.create({ |
| 106 | url: chrome.runtime.getURL('options/experiments.html'), |
| 107 | })); |
| 108 | } |
| 109 | |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 110 | // Add options to page |
| 111 | let optionsContainer = document.getElementById('options-container'); |
| 112 | for (let section of optionsPage.sections) { |
| 113 | if (section?.name) { |
| 114 | let sectionHeader = document.createElement('h4'); |
| 115 | sectionHeader.setAttribute('data-i18n', section.name); |
| 116 | optionsContainer.append(sectionHeader); |
| 117 | } |
| 118 | |
| 119 | if (section?.options) { |
| 120 | for (let option of section.options) { |
| 121 | if (option?.customHTML) { |
| 122 | optionsContainer.insertAdjacentHTML('beforeend', option.customHTML); |
Adrià Vilanova Martínez | c591bf7 | 2021-09-06 20:23:06 +0200 | [diff] [blame] | 123 | } else { |
| 124 | let optionEl = document.createElement('div'); |
| 125 | optionEl.classList.add('option'); |
| 126 | |
| 127 | let checkbox = document.createElement('input'); |
| 128 | checkbox.setAttribute('type', 'checkbox'); |
| 129 | checkbox.id = option.codename; |
| 130 | |
| 131 | let label = document.createElement('label'); |
| 132 | label.setAttribute('for', checkbox.id); |
| 133 | label.setAttribute('data-i18n', option.codename); |
| 134 | |
| 135 | optionEl.append(checkbox, ' ', label); |
| 136 | |
| 137 | if (option?.experimental) { |
| 138 | let experimental = document.createElement('span'); |
| 139 | experimental.classList.add('experimental-label'); |
| 140 | experimental.setAttribute('data-i18n', 'experimental_label'); |
| 141 | |
| 142 | optionEl.append(' ', experimental); |
| 143 | } |
| 144 | |
| 145 | optionsContainer.append(optionEl); |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 148 | // Add optional permissions warning label and kill switch component |
| 149 | // after each option. |
| 150 | let optionalPermissionsWarningLabel = document.createElement('div'); |
| 151 | optionalPermissionsWarningLabel.classList.add( |
| 152 | 'optional-permissions-warning-label'); |
| 153 | optionalPermissionsWarningLabel.setAttribute('hidden', ''); |
| 154 | optionalPermissionsWarningLabel.setAttribute( |
| 155 | 'data-feature', option.codename); |
| 156 | optionalPermissionsWarningLabel.setAttribute( |
| 157 | 'data-i18n', 'optionalpermissionswarning_label'); |
| 158 | |
Adrià Vilanova Martínez | c591bf7 | 2021-09-06 20:23:06 +0200 | [diff] [blame] | 159 | let killSwitchComponent = document.createElement('div'); |
| 160 | killSwitchComponent.classList.add('kill-switch-label'); |
| 161 | killSwitchComponent.setAttribute('hidden', ''); |
| 162 | killSwitchComponent.setAttribute('data-feature', option.codename); |
| 163 | killSwitchComponent.setAttribute('data-i18n', 'killswitchenabled'); |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 164 | |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 165 | optionsContainer.append( |
| 166 | optionalPermissionsWarningLabel, killSwitchComponent); |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | if (section?.footerHTML) { |
| 171 | optionsContainer.insertAdjacentHTML('beforeend', section.footerHTML); |
| 172 | } |
| 173 | } |
| 174 | |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 175 | var featuresLink = document.querySelector('.features-link'); |
| 176 | featuresLink.href = getDocURL('features.md'); |
| 177 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 178 | var profileIndicatorLink = |
| 179 | document.getElementById('profileIndicatorMoreInfo'); |
Adrià Vilanova Martínez | e32adc4 | 2021-08-30 17:16:49 +0200 | [diff] [blame] | 180 | profileIndicatorLink.href = getDocURL('op_indicator.md'); |
avm99963 | 7309b06 | 2021-04-22 12:41:08 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Adrià Vilanova Martínez | 09f35be | 2021-09-06 19:50:09 +0200 | [diff] [blame] | 183 | i18n(); |
| 184 | |
Adrià Vilanova Martínez | 5f8ae6d | 2021-12-26 12:25:30 +0100 | [diff] [blame] | 185 | // Add custom handlers |
| 186 | let manageWorkflowsBtn = document.getElementById('manage-workflows'); |
| 187 | if (manageWorkflowsBtn) |
| 188 | manageWorkflowsBtn.addEventListener('click', e => { |
| 189 | e.preventDefault(); |
| 190 | chrome.tabs.create({ |
| 191 | url: chrome.runtime.getURL('options/workflows.html'), |
| 192 | }) |
| 193 | }); |
| 194 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 195 | chrome.storage.sync.get(null, function(items) { |
| 196 | items = cleanUpOptions(items, false); |
| 197 | |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 198 | // If some features have been force disabled, communicate this to the user. |
| 199 | if (items?._forceDisabledFeatures && |
| 200 | items._forceDisabledFeatures.length > 0) { |
| 201 | if (window.CONTEXT == 'options') { |
| 202 | document.getElementById('kill-switch-warning') |
| 203 | .removeAttribute('hidden'); |
| 204 | } |
Adrià Vilanova Martínez | 413cb44 | 2021-09-06 00:30:45 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Adrià Vilanova Martínez | 3465e77 | 2021-07-11 19:18:41 +0200 | [diff] [blame] | 207 | for (var entry of Object.entries(optionsPrototype)) { |
| 208 | var opt = entry[0]; |
| 209 | var optMeta = entry[1]; |
| 210 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 211 | if (!isOptionShown(opt)) continue; |
| 212 | |
| 213 | if (specialOptions.includes(opt)) { |
| 214 | switch (opt) { |
| 215 | case 'profileindicatoralt_months': |
| 216 | var input = document.createElement('input'); |
| 217 | input.type = 'number'; |
| 218 | input.id = 'profileindicatoralt_months'; |
| 219 | input.max = '12'; |
| 220 | input.min = '1'; |
| 221 | input.value = items[opt]; |
| 222 | input.required = true; |
| 223 | document.getElementById('profileindicatoralt_months--container') |
| 224 | .appendChild(input); |
| 225 | break; |
| 226 | |
| 227 | case 'ccdarktheme_mode': |
| 228 | var select = document.createElement('select'); |
| 229 | select.id = 'ccdarktheme_mode'; |
| 230 | |
| 231 | const modes = ['switch', 'system']; |
| 232 | for (const mode of modes) { |
| 233 | var modeOption = document.createElement('option'); |
| 234 | modeOption.value = mode; |
| 235 | modeOption.textContent = |
| 236 | chrome.i18n.getMessage('options_ccdarktheme_mode_' + mode); |
| 237 | if (items.ccdarktheme_mode == mode) modeOption.selected = true; |
| 238 | select.appendChild(modeOption); |
| 239 | } |
| 240 | |
| 241 | document.getElementById('ccdarktheme_mode--container') |
| 242 | .appendChild(select); |
| 243 | break; |
| 244 | |
Adrià Vilanova Martínez | 849ede6 | 2022-06-20 19:10:28 +0200 | [diff] [blame] | 245 | case 'interopthreadpage_mode': |
| 246 | var select = document.createElement('select'); |
| 247 | select.id = 'interopthreadpage_mode'; |
| 248 | |
| 249 | const threadPageModes = ['previous', 'next']; |
| 250 | for (const mode of threadPageModes) { |
| 251 | let modeOption = document.createElement('option'); |
| 252 | modeOption.value = mode; |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 253 | modeOption.textContent = chrome.i18n.getMessage( |
| 254 | 'options_interopthreadpage_mode_' + mode); |
| 255 | if (items.interopthreadpage_mode == mode) |
| 256 | modeOption.selected = true; |
Adrià Vilanova Martínez | 849ede6 | 2022-06-20 19:10:28 +0200 | [diff] [blame] | 257 | select.appendChild(modeOption); |
| 258 | } |
| 259 | |
| 260 | document.getElementById('interopthreadpage_mode--container') |
| 261 | .appendChild(select); |
| 262 | break; |
| 263 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 264 | default: |
| 265 | console.warn('Unrecognized option: ' + opt); |
| 266 | break; |
| 267 | } |
| 268 | continue; |
| 269 | } |
| 270 | |
| 271 | if (items[opt] === true) document.getElementById(opt).checked = true; |
Adrià Vilanova Martínez | c591bf7 | 2021-09-06 20:23:06 +0200 | [diff] [blame] | 272 | if (window.CONTEXT == 'options' && |
| 273 | items?._forceDisabledFeatures?.includes?.(opt)) |
| 274 | document.querySelector('.kill-switch-label[data-feature="' + opt + '"]') |
| 275 | .removeAttribute('hidden'); |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | exclusiveOptions.forEach(exclusive => { |
| 279 | if (!isOptionShown(exclusive[0]) || !isOptionShown(exclusive[1])) return; |
| 280 | |
| 281 | exclusive.forEach( |
| 282 | el => document.getElementById(el).addEventListener('change', e => { |
| 283 | if (document.getElementById(exclusive[0]).checked && |
| 284 | document.getElementById(exclusive[1]).checked) { |
| 285 | document |
| 286 | .getElementById( |
| 287 | exclusive[(e.currentTarget.id == exclusive[0] ? 1 : 0)]) |
| 288 | .checked = false; |
| 289 | } |
| 290 | })); |
| 291 | }); |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 292 | |
| 293 | // Handle options which need optional permissions. |
| 294 | grantedOptPermissions() |
| 295 | .then(grantedPerms => { |
| 296 | for (const [opt, optMeta] of Object.entries(optionsPrototype)) { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 297 | if (!optMeta.requiredOptPermissions || !isOptionShown(opt)) |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 298 | continue; |
| 299 | |
| 300 | let warningLabel = document.querySelector( |
| 301 | '.optional-permissions-warning-label[data-feature="' + opt + |
| 302 | '"]'); |
| 303 | |
| 304 | // Ensure we have the appropriate permissions when the checkbox |
| 305 | // switches from disabled to enabled. |
| 306 | // |
| 307 | // Also, if the checkbox was indeterminate because the feature was |
| 308 | // enabled but not all permissions had been granted, enable the |
| 309 | // feature in order to trigger the permission request again. |
| 310 | let checkbox = document.getElementById(opt); |
| 311 | if (!checkbox) { |
| 312 | console.error('Expected checkbox for feature "' + opt + '".'); |
| 313 | continue; |
| 314 | } |
| 315 | checkbox.addEventListener('change', () => { |
| 316 | if (checkbox.hasAttribute(kClickShouldEnableFeat)) { |
| 317 | checkbox.removeAttribute(kClickShouldEnableFeat); |
| 318 | checkbox.checked = true; |
| 319 | } |
| 320 | |
| 321 | if (checkbox.checked) |
| 322 | ensureOptPermissions(opt) |
| 323 | .then(granted => { |
| 324 | if (granted) { |
| 325 | warningLabel.setAttribute('hidden', ''); |
| 326 | if (!document.querySelector( |
| 327 | '.optional-permissions-warning-label:not([hidden])')) |
| 328 | document |
| 329 | .getElementById('optional-permissions-warning') |
| 330 | .setAttribute('hidden', ''); |
| 331 | } else |
Adrià Vilanova Martínez | d7ada32 | 2022-01-05 04:15:46 +0100 | [diff] [blame] | 332 | document.getElementById(opt).checked = false; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 333 | }) |
| 334 | .catch(err => { |
| 335 | console.error( |
| 336 | 'An error ocurred while ensuring that the optional ' + |
| 337 | 'permissions were granted after the checkbox ' + |
| 338 | 'was clicked for feature "' + opt + '":', |
| 339 | err); |
Adrià Vilanova Martínez | d7ada32 | 2022-01-05 04:15:46 +0100 | [diff] [blame] | 340 | document.getElementById(opt).checked = false; |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 341 | }); |
| 342 | }); |
| 343 | |
| 344 | // Add warning message if some permissions are missing and the |
| 345 | // feature is enabled. |
| 346 | if (items[opt] === true) { |
| 347 | let shownHeaderMessage = false; |
| 348 | missingPermissions(opt, grantedPerms) |
| 349 | .then(missingPerms => { |
Adrià Vilanova Martínez | 310c290 | 2022-07-04 00:31:41 +0200 | [diff] [blame] | 350 | if (!isPermissionsObjectEmpty(missingPerms)) { |
Adrià Vilanova Martínez | 5120dbb | 2022-01-04 03:21:17 +0100 | [diff] [blame] | 351 | checkbox.indeterminate = true; |
| 352 | checkbox.setAttribute(kClickShouldEnableFeat, ''); |
| 353 | |
| 354 | warningLabel.removeAttribute('hidden'); |
| 355 | |
| 356 | if (!shownHeaderMessage) { |
| 357 | shownHeaderMessage = true; |
| 358 | document.getElementById('optional-permissions-warning') |
| 359 | .removeAttribute('hidden'); |
| 360 | } |
| 361 | } |
| 362 | }) |
| 363 | .catch(err => console.error(err)); |
| 364 | } |
| 365 | } |
| 366 | }) |
| 367 | .catch(err => console.error(err)); |
| 368 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 369 | document.querySelector('#save').addEventListener('click', save); |
| 370 | }); |
| 371 | }); |