11import { DEFAULT_SETTINGS , DEFAULT_THEME } from '../defaults' ;
2+ import { debounce } from '../utils/debounce' ;
23import { isURLMatched } from '../utils/url' ;
34import { UserSettings } from '../definitions' ;
5+ import { readSyncStorage , readLocalStorage , writeSyncStorage , writeLocalStorage } from './utils/extension-api' ;
46
57const SAVE_TIMEOUT = 1000 ;
68
@@ -15,91 +17,71 @@ export default class UserStorage {
1517 this . settings = await this . loadSettingsFromStorage ( ) ;
1618 }
1719
18- cleanup ( ) {
19- chrome . storage . local . remove ( [ 'activationTime' , 'deactivationTime' ] ) ;
20- chrome . storage . sync . remove ( [ 'activationTime' , 'deactivationTime' ] ) ;
20+ private fillDefaults ( settings : UserSettings ) {
21+ settings . theme = { ...DEFAULT_THEME , ...settings . theme } ;
22+ settings . time = { ...DEFAULT_SETTINGS . time , ...settings . time } ;
23+ settings . presets . forEach ( ( preset ) => {
24+ preset . theme = { ...DEFAULT_THEME , ...preset . theme } ;
25+ } ) ;
26+ settings . customThemes . forEach ( ( site ) => {
27+ site . theme = { ...DEFAULT_THEME , ...site . theme } ;
28+ } ) ;
2129 }
2230
23- private loadSettingsFromStorage ( ) {
24- return new Promise < UserSettings > ( ( resolve ) => {
25- chrome . storage . local . get ( DEFAULT_SETTINGS , ( local : UserSettings ) => {
26- local . syncSettings = local . syncSettings || DEFAULT_SETTINGS . syncSettings ;
27- if ( ! local . syncSettings ) {
28- local . theme = { ...DEFAULT_SETTINGS . theme , ...local . theme } ;
29- local . time = { ...DEFAULT_SETTINGS . time , ...local . time } ;
30- local . customThemes . forEach ( ( site ) => {
31- site . theme = { ...DEFAULT_SETTINGS . theme , ...site . theme } ;
32- } ) ;
33- resolve ( local ) ;
34- return ;
35- }
31+ private async loadSettingsFromStorage ( ) {
32+ const local = await readLocalStorage ( DEFAULT_SETTINGS ) ;
33+ if ( local . syncSettings == null ) {
34+ local . syncSettings = DEFAULT_SETTINGS . syncSettings ;
35+ }
36+ if ( ! local . syncSettings ) {
37+ this . fillDefaults ( local ) ;
38+ return local ;
39+ }
3640
37- chrome . storage . sync . get ( { ...DEFAULT_SETTINGS , config : 'empty' } , ( $sync : UserSettings & { config : any } ) => {
38- let sync : UserSettings ;
39- if ( ! $sync ) {
40- this . saveSyncSetting ( false ) ;
41- resolve ( this . loadSettingsFromStorage ( ) ) ;
42- return ;
43- }
44- if ( $sync . config === 'empty' ) {
45- delete $sync . config ;
46- sync = $sync ;
47- } else {
48- sync = this . migrateSettings_4_6_2 ( $sync ) as UserSettings ;
49- }
50- sync . theme = { ...DEFAULT_SETTINGS . theme , ...sync . theme } ;
51- sync . time = { ...DEFAULT_SETTINGS . time , ...sync . time } ;
52- sync . presets . forEach ( ( preset ) => {
53- preset . theme = { ...DEFAULT_SETTINGS . theme , ...preset . theme } ;
54- } ) ;
55- sync . customThemes . forEach ( ( site ) => {
56- site . theme = { ...DEFAULT_SETTINGS . theme , ...site . theme } ;
57- } ) ;
58- resolve ( sync ) ;
59- } ) ;
60- } ) ;
61- } ) ;
62- }
41+ const $sync = await readSyncStorage ( DEFAULT_SETTINGS ) ;
42+ if ( ! $sync ) {
43+ console . warn ( 'Sync settings are missing' ) ;
44+ local . syncSettings = false ;
45+ this . set ( { syncSettings : false } ) ;
46+ this . saveSyncSetting ( false ) ;
47+ return local ;
48+ }
6349
64- async saveSettings ( ) {
65- const saved = await this . saveSettingsIntoStorage ( this . settings ) ;
66- this . settings = saved ;
50+ const sync = await readSyncStorage ( DEFAULT_SETTINGS ) ;
51+ this . fillDefaults ( sync ) ;
52+ return sync ;
6753 }
6854
69- saveSyncSetting ( sync : boolean ) {
70- chrome . storage . sync . set ( { syncSettings : sync } , ( ) => {
71- if ( chrome . runtime . lastError ) {
72- console . warn ( 'Settings synchronization was disabled due to error:' , chrome . runtime . lastError ) ;
73- }
74- } ) ;
75- chrome . storage . local . set ( { syncSettings : sync } ) ;
55+ async saveSettings ( ) {
56+ await this . saveSettingsIntoStorage ( ) ;
7657 }
7758
78- private saveSettingsIntoStorage ( settings : UserSettings ) {
79- if ( this . timeout ) {
80- clearInterval ( this . timeout ) ;
59+ async saveSyncSetting ( sync : boolean ) {
60+ const obj = { syncSettings : sync } ;
61+ await writeLocalStorage ( obj ) ;
62+ try {
63+ await writeSyncStorage ( obj ) ;
64+ } catch ( err ) {
65+ console . warn ( 'Settings synchronization was disabled due to error:' , chrome . runtime . lastError ) ;
66+ this . set ( { syncSettings : false } ) ;
8167 }
82- return new Promise < UserSettings > ( ( resolve ) => {
83- this . timeout = setTimeout ( ( ) => {
84- this . timeout = null ;
85- if ( settings . syncSettings ) {
86- chrome . storage . sync . set ( settings , ( ) => {
87- if ( chrome . runtime . lastError ) {
88- console . warn ( 'Settings synchronization was disabled due to error:' , chrome . runtime . lastError ) ;
89- const local : UserSettings = { ...settings , syncSettings : false } ;
90- chrome . storage . local . set ( local , ( ) => resolve ( local ) ) ;
91- } else {
92- resolve ( settings ) ;
93- }
94- } ) ;
95- } else {
96- chrome . storage . local . set ( settings , ( ) => resolve ( settings ) ) ;
97- }
98- } , SAVE_TIMEOUT ) ;
99- } ) ;
10068 }
10169
102- private timeout : number = null ;
70+ private saveSettingsIntoStorage = debounce ( SAVE_TIMEOUT , async ( ) => {
71+ const settings = this . settings ;
72+ if ( settings . syncSettings ) {
73+ try {
74+ await writeSyncStorage ( settings ) ;
75+ } catch ( err ) {
76+ console . warn ( 'Settings synchronization was disabled due to error:' , chrome . runtime . lastError ) ;
77+ this . set ( { syncSettings : false } ) ;
78+ await this . saveSyncSetting ( false ) ;
79+ await writeLocalStorage ( settings ) ;
80+ }
81+ } else {
82+ await writeLocalStorage ( settings ) ;
83+ }
84+ } ) ;
10385
10486 set ( $settings : Partial < UserSettings > ) {
10587 if ( $settings . siteList ) {
@@ -128,47 +110,4 @@ export default class UserStorage {
128110 }
129111 this . settings = { ...this . settings , ...$settings } ;
130112 }
131-
132- private migrateSettings_4_6_2 ( settings_4_6_2 : any ) {
133- function migrateTheme ( filterConfig_4_6_2 : any ) {
134- const f = filterConfig_4_6_2 ;
135- return {
136- ...DEFAULT_THEME ,
137- mode : f . mode ,
138- brightness : f . brightness ,
139- contrast : f . contrast ,
140- grayscale : f . grayscale ,
141- sepia : f . sepia ,
142- useFont : f . useFont ,
143- fontFamily : f . fontFamily ,
144- textStroke : f . textStroke ,
145- engine : f . engine ,
146- stylesheet : f . stylesheet ,
147- } ;
148- }
149-
150- try {
151- const s = settings_4_6_2 ;
152- const settings : UserSettings = {
153- ...DEFAULT_SETTINGS ,
154- enabled : s . enabled ,
155- theme : migrateTheme ( s . config ) ,
156- customThemes : s . config . custom ? s . config . custom . map ( ( c ) => {
157- return {
158- url : c . url ,
159- theme : migrateTheme ( c . config ) ,
160- } ;
161- } ) : [ ] ,
162- siteList : s . config . siteList ,
163- applyToListedOnly : s . config . invertListed ,
164- changeBrowserTheme : s . config . changeBrowserTheme ,
165- } ;
166- chrome . storage . sync . remove ( 'config' ) ;
167- chrome . storage . sync . set ( settings ) ;
168- return settings ;
169- } catch ( err ) {
170- console . error ( 'Settings migration error:' , err , 'Loaded settings:' , settings_4_6_2 ) ;
171- return DEFAULT_SETTINGS ;
172- }
173- }
174113}
0 commit comments