avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 1 | const optionsPrototype = { |
| 2 | // Available options: |
| 3 | 'list': { |
| 4 | defaultValue: true, |
| 5 | context: 'options', |
| 6 | }, |
| 7 | 'thread': { |
| 8 | defaultValue: true, |
| 9 | context: 'options', |
| 10 | }, |
| 11 | 'threadall': { |
| 12 | defaultValue: false, |
| 13 | context: 'options', |
| 14 | }, |
| 15 | 'fixedtoolbar': { |
| 16 | defaultValue: false, |
| 17 | context: 'options', |
| 18 | }, |
| 19 | 'redirect': { |
| 20 | defaultValue: false, |
| 21 | context: 'options', |
| 22 | }, |
| 23 | 'history': { |
| 24 | defaultValue: false, |
| 25 | context: 'options', |
| 26 | }, |
| 27 | 'loaddrafts': { |
| 28 | defaultValue: false, |
| 29 | context: 'options', |
| 30 | }, |
| 31 | 'increasecontrast': { |
| 32 | defaultValue: false, |
| 33 | context: 'options', |
| 34 | }, |
| 35 | 'stickysidebarheaders': { |
| 36 | defaultValue: false, |
| 37 | context: 'options', |
| 38 | }, |
| 39 | 'profileindicator': { |
| 40 | defaultValue: false, |
| 41 | context: 'options', |
| 42 | }, |
| 43 | 'profileindicatoralt': { |
| 44 | defaultValue: false, |
| 45 | context: 'options', |
| 46 | }, |
| 47 | 'profileindicatoralt_months': { |
| 48 | defaultValue: 12, |
| 49 | context: 'options', |
| 50 | }, |
| 51 | 'ccdarktheme': { |
| 52 | defaultValue: false, |
| 53 | context: 'options', |
| 54 | }, |
| 55 | 'ccdarktheme_mode': { |
| 56 | defaultValue: 'switch', |
| 57 | context: 'options', |
| 58 | }, |
| 59 | 'ccforcehidedrawer': { |
| 60 | defaultValue: false, |
| 61 | context: 'options', |
| 62 | }, |
| 63 | 'ccdragndropfix': { |
| 64 | defaultValue: false, |
| 65 | context: 'options', |
| 66 | }, |
| 67 | 'batchlock': { |
| 68 | defaultValue: false, |
| 69 | context: 'options', |
| 70 | }, |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 71 | 'enhancedannouncementsdot': { |
| 72 | defaultValue: false, |
| 73 | context: 'options', |
| 74 | }, |
| 75 | 'repositionexpandthread': { |
| 76 | defaultValue: false, |
| 77 | context: 'options', |
| 78 | }, |
Adrià Vilanova Martínez | 2b87179 | 2021-06-09 12:35:06 +0200 | [diff] [blame] | 79 | 'disableunifiedprofiles': { |
| 80 | defaultValue: false, |
| 81 | context: 'options', |
| 82 | }, |
Adrià Vilanova Martínez | a10fff2 | 2021-06-29 21:07:40 +0200 | [diff] [blame] | 83 | 'forcemarkasread': { |
| 84 | defaultValue: false, |
| 85 | context: 'options', |
| 86 | }, |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 87 | |
| 88 | // Experiments: |
avm99963 | 3eae452 | 2021-04-22 01:14:27 +0200 | [diff] [blame] | 89 | 'threadlistavatars': { |
| 90 | defaultValue: false, |
| 91 | context: 'experiments', |
| 92 | }, |
avm99963 | a007d49 | 2021-05-02 12:32:03 +0200 | [diff] [blame] | 93 | 'autorefreshlist': { |
| 94 | defaultValue: false, |
| 95 | context: 'experiments', |
| 96 | }, |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 97 | |
| 98 | // Internal options: |
| 99 | 'ccdarktheme_switch_enabled': { |
| 100 | defaultValue: true, |
| 101 | context: 'internal', |
| 102 | }, |
| 103 | |
| 104 | // Deprecated options: |
| 105 | 'escalatethreads': { |
| 106 | defaultValue: false, |
| 107 | context: 'deprecated', |
| 108 | }, |
| 109 | 'movethreads': { |
| 110 | defaultValue: false, |
| 111 | context: 'deprecated', |
| 112 | }, |
| 113 | 'batchduplicate': { |
| 114 | defaultValue: false, |
| 115 | context: 'deprecated', |
| 116 | }, |
avm99963 | a858ef7 | 2021-04-22 01:18:31 +0200 | [diff] [blame] | 117 | 'smei_sortdirection': { |
| 118 | defaultValue: false, |
| 119 | context: 'deprecated', |
| 120 | }, |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 121 | }; |
| 122 | |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 123 | const specialOptions = [ |
| 124 | 'profileindicatoralt_months', |
avm99963 | 0bc113a | 2020-09-07 13:02:11 +0200 | [diff] [blame] | 125 | 'ccdarktheme_mode', |
| 126 | 'ccdarktheme_switch_enabled', |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 127 | 'ccdragndropfix', |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 128 | ]; |
| 129 | |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 130 | function isEmpty(obj) { |
| 131 | return Object.keys(obj).length === 0; |
| 132 | } |
| 133 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 134 | // Adds missing options with their default value. If |dryRun| is set to false, |
| 135 | // they are also saved to the sync storage area. |
| 136 | function cleanUpOptions(options, dryRun = false) { |
| 137 | console.log('[cleanUpOptions] Previous options', JSON.stringify(options)); |
avm99963 | ad65e75 | 2020-09-01 00:13:59 +0200 | [diff] [blame] | 138 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 139 | if (typeof options !== 'object' || options === null) options = {}; |
| 140 | |
| 141 | var ok = true; |
| 142 | for (const [opt, optMeta] of Object.entries(optionsPrototype)) { |
| 143 | if (!(opt in options)) { |
| 144 | ok = false; |
| 145 | options[opt] = optMeta['defaultValue']; |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 149 | console.log('[cleanUpOptions] New options', JSON.stringify(options)); |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 150 | |
avm99963 | bf8eece | 2021-04-22 00:27:03 +0200 | [diff] [blame] | 151 | if (!ok && !dryRun) { |
avm99963 | 4c1a679 | 2020-08-31 21:30:42 +0200 | [diff] [blame] | 152 | chrome.storage.sync.set(options); |
| 153 | } |
| 154 | |
| 155 | return options; |
| 156 | } |
avm99963 | 8e0c100 | 2020-12-03 16:54:20 +0100 | [diff] [blame] | 157 | |
| 158 | // This method is based on the fact that when building the extension for Firefox |
| 159 | // the browser_specific_settings.gecko entry is included. |
| 160 | function isFirefox() { |
| 161 | var manifest = chrome.runtime.getManifest(); |
| 162 | return manifest.browser_specific_settings !== undefined && |
| 163 | manifest.browser_specific_settings.gecko !== undefined; |
| 164 | } |
avm99963 | 7309b06 | 2021-04-22 12:41:08 +0200 | [diff] [blame] | 165 | |
| 166 | // Returns whether the extension is a release version. |
| 167 | function isReleaseVersion() { |
| 168 | var manifest = chrome.runtime.getManifest(); |
| 169 | return ('version' in manifest) && manifest.version != '0'; |
| 170 | } |