-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorageHelper.js
More file actions
119 lines (114 loc) · 2.93 KB
/
storageHelper.js
File metadata and controls
119 lines (114 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* This class solution for the problem when the code runs in different environments.
* If it is running in the browser use the localStorage when the Grease monkey is there using GM.setValue/GM.getValue,
* if runs on nodes and the temprc is there it falls back to that.
* The last option if no other uses a dummy solution.
* @todo temprc support
*
* @param {?number} //{?uint8_t}
* @class
**/
const StorageHelperClass = class{
/**
* 0 = dummy storage
* 1 = localStorage
* 2 = Grease monkey value
*
* @type {number}// {uint8_t}
**/
#type = 0;
/** @type {Object.<string, any>}// {map<str, any>} **/
#values = {};
/**
*
* @param {string}
* @param {any}
* @private
**/
#set(name_, val_){
this.#values[name_] = val_;
}
/**
*
* @param {string}
* @private
* @return {any}
**/
#get(name_, val_){
if (typeof this.#values[name_] !== 'undefined')
return this.#values[name_];
if (typeof val_ !== 'undefined')
return val_;
return null;
}
/**
*
* @param {?number} //{?uint8_t}
* @constructs
**/
constructor(type_) {
if (
(typeof type_ === 'number') &&
(type_ < 3) &&
(type_ > -1)
) {
this.#type = parseInt(type_);
return ;
}
if (
(typeof localStorage === 'undefined') &&
(typeof GM === 'undefined')
) return;
if (
(typeof GM !== 'undefined') &&
(typeof GM.setValue === 'function') &&
(typeof GM.getValue === 'function')
){
this.#type = 2;
return;
}
if (
(typeof localStorage !== 'undefined') &&
(typeof localStorage.setItem === 'function') &&
(typeof localStorage.getItem === 'function')
) this.#type = 1;
};
/**
*
* @param {string}
* @param {any}
* @async
* @public
**/
async set(name_, val_){
if (this.#type === 1)
return await localStorage.setItem(name_, val_);
if (this.#type === 2)
return await GM.setValue(name_, val_);
//dummy solution
return await this.#set(name_, val_);
};
/**
*
* @param {string}
* @async
* @public
* @return {any}
**/
async get(name_, val_){
if (this.#type === 1){
if (typeof val_ === 'undefined')
return await localStorage.getItem(name_);
return await localStorage.getItem(name_, val_);
}
if (this.#type === 2){
if (typeof val_ === 'undefined')
return await GM.getValue(name_);
return await GM.getValue(name_, val_);
}
//dummy solution
if (typeof val_ === 'undefined')
return await this.#get(name_);
return await this.#get(name_, val_);
};
};