blob: 205cdc96c5848241289fb210f0dab6119c170c04 [file] [log] [blame]
avm99963bf8eece2021-04-22 00:27:03 +02001const 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 },
71 'smei_sortdirection': {
72 defaultValue: false,
73 context: 'options',
74 },
75 'enhancedannouncementsdot': {
76 defaultValue: false,
77 context: 'options',
78 },
79 'repositionexpandthread': {
80 defaultValue: false,
81 context: 'options',
82 },
83
84 // Experiments:
85
86
87 // Internal options:
88 'ccdarktheme_switch_enabled': {
89 defaultValue: true,
90 context: 'internal',
91 },
92
93 // Deprecated options:
94 'escalatethreads': {
95 defaultValue: false,
96 context: 'deprecated',
97 },
98 'movethreads': {
99 defaultValue: false,
100 context: 'deprecated',
101 },
102 'batchduplicate': {
103 defaultValue: false,
104 context: 'deprecated',
105 },
avm999634c1a6792020-08-31 21:30:42 +0200106};
107
avm99963ad65e752020-09-01 00:13:59 +0200108const specialOptions = [
109 'profileindicatoralt_months',
avm999630bc113a2020-09-07 13:02:11 +0200110 'ccdarktheme_mode',
111 'ccdarktheme_switch_enabled',
avm999638e0c1002020-12-03 16:54:20 +0100112 'ccdragndropfix',
avm99963ad65e752020-09-01 00:13:59 +0200113];
114
avm999634c1a6792020-08-31 21:30:42 +0200115function isEmpty(obj) {
116 return Object.keys(obj).length === 0;
117}
118
avm99963bf8eece2021-04-22 00:27:03 +0200119// Adds missing options with their default value. If |dryRun| is set to false,
120// they are also saved to the sync storage area.
121function cleanUpOptions(options, dryRun = false) {
122 console.log('[cleanUpOptions] Previous options', JSON.stringify(options));
avm99963ad65e752020-09-01 00:13:59 +0200123
avm99963bf8eece2021-04-22 00:27:03 +0200124 if (typeof options !== 'object' || options === null) options = {};
125
126 var ok = true;
127 for (const [opt, optMeta] of Object.entries(optionsPrototype)) {
128 if (!(opt in options)) {
129 ok = false;
130 options[opt] = optMeta['defaultValue'];
avm999634c1a6792020-08-31 21:30:42 +0200131 }
132 }
133
avm99963bf8eece2021-04-22 00:27:03 +0200134 console.log('[cleanUpOptions] New options', JSON.stringify(options));
avm999634c1a6792020-08-31 21:30:42 +0200135
avm99963bf8eece2021-04-22 00:27:03 +0200136 if (!ok && !dryRun) {
avm999634c1a6792020-08-31 21:30:42 +0200137 chrome.storage.sync.set(options);
138 }
139
140 return options;
141}
avm999638e0c1002020-12-03 16:54:20 +0100142
143// This method is based on the fact that when building the extension for Firefox
144// the browser_specific_settings.gecko entry is included.
145function isFirefox() {
146 var manifest = chrome.runtime.getManifest();
147 return manifest.browser_specific_settings !== undefined &&
148 manifest.browser_specific_settings.gecko !== undefined;
149}