@@ -4,6 +4,10 @@ import {Application} from "../application";
4
4
export interface ConfigItems extends Config {
5
5
SetNamespace ( namespace : string ) : void
6
6
7
+ SetNamespaceConfig ( namespace : string , key : string , val : string ) : Promise < any >
8
+
9
+ GetNamespaceConfig ( namespace : string , key : string , defaultVal ?: string ) : string
10
+
7
11
vtoken : string
8
12
rand_answer : boolean
9
13
auto : boolean
@@ -40,7 +44,15 @@ export class ChromeConfigItems implements ConfigItems {
40
44
return this . config . ConfigList ( ) ;
41
45
}
42
46
43
- public GetConfig ( key : string , defaultVal ?: string ) : any {
47
+ public SetNamespaceConfig ( namespace : string , key : string , val : string ) : Promise < any > {
48
+ return this . config . SetConfig ( namespace + key , val ) ;
49
+ }
50
+
51
+ public GetNamespaceConfig ( namespace : string , key : string , defaultVal ?: string ) : string {
52
+ return this . config . GetConfig ( namespace + key , defaultVal ) ;
53
+ }
54
+
55
+ public GetConfig ( key : string , defaultVal ?: string ) : string {
44
56
let val = this . config . GetConfig ( this . Namespace + key ) ;
45
57
if ( val == undefined && this . Namespace != "" ) {
46
58
return this . config . GetConfig ( key , defaultVal ) ;
@@ -81,7 +93,11 @@ export class ChromeConfigItems implements ConfigItems {
81
93
}
82
94
83
95
public get video_cdn ( ) {
84
- return this . GetConfig ( "video_cdn" ) ;
96
+ let val = this . GetConfig ( "video_cdn" ) ;
97
+ if ( val == "默认" ) {
98
+ return ""
99
+ }
100
+ return val ;
85
101
}
86
102
87
103
public get video_multiple ( ) {
@@ -125,8 +141,12 @@ export interface ConfigWatchCallback {
125
141
}
126
142
127
143
// 后台环境中使用
128
- export function NewBackendConfig ( onlyRead : boolean ) : backendConfig {
129
- return new backendConfig ( onlyRead ) ;
144
+ export function NewBackendConfig ( onlyRead : boolean ) : Promise < backendConfig > {
145
+ return new Promise ( async resolve => {
146
+ let ret = new backendConfig ( onlyRead ) ;
147
+ await ret . updateCache ( ) ;
148
+ resolve ( ret ) ;
149
+ } ) ;
130
150
}
131
151
132
152
class configWatch {
@@ -182,7 +202,6 @@ class backendConfig implements Config {
182
202
constructor ( onlyRead : boolean ) {
183
203
this . onlyRead = onlyRead ;
184
204
this . watch = new configWatch ( ) ;
185
- this . updateCache ( ) ;
186
205
chrome . runtime . onMessage . addListener ( ( request ) => {
187
206
if ( request . type && request . type == "cxconfig" ) {
188
207
this . cache [ request . key ] = request . value ;
@@ -200,7 +219,7 @@ class backendConfig implements Config {
200
219
chrome . storage . sync . set ( { "config_storage" : txt } ) ;
201
220
}
202
221
203
- protected updateCache ( ) : Promise < any > {
222
+ public updateCache ( ) : Promise < any > {
204
223
return new Promise ( resolve => {
205
224
let configDefaultValue = new Map < string , any > ( )
206
225
. set ( "vtoken" , "" ) . set ( "rand_answer" , false ) . set ( "auto" , true )
@@ -224,15 +243,12 @@ class backendConfig implements Config {
224
243
} ) ;
225
244
}
226
245
227
- public GetConfig ( key : string , defaultVal ?: string ) : any {
228
- return new Promise < any > ( async resolve => {
229
- if ( this . cache == undefined ) {
230
- await this . updateCache ( ) ;
231
- resolve ( this . cache [ key ] || defaultVal ) ;
232
- return ;
233
- }
234
- resolve ( this . cache [ key ] || defaultVal ) ;
235
- } ) ;
246
+ public GetConfig ( key : string , defaultVal ?: string ) : string {
247
+ if ( this . cache == undefined ) {
248
+ Application . App . log . Fatal ( "缓存失败!!!" ) ;
249
+ return "" ;
250
+ }
251
+ return this . cache [ key ] || defaultVal ;
236
252
}
237
253
238
254
public Watch ( key : Array < string > | string , callback : ConfigWatchCallback ) : void {
0 commit comments