-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.ts
72 lines (58 loc) · 1.78 KB
/
storage.ts
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
class TabStorage {
private static _instance:TabStorage;
private constructor()
{ }
public static get Instance() {
return this._instance || (this._instance = new this());
}
private _getHost(url:string):string {
let anchor = document.createElement('a');
anchor.href = url;
if (anchor.host.length <= 0) {
throw new URIError('Unable to get hostname from the URL');
}
return anchor.host;
}
getData():Promise<FontData> {
return new Promise((resolve, reject) => {
Utility.onActiveTab().then((tab) => {
let host = this._getHost(tab.url);
return browser.storage.local.get(host);
})
.then((value) => {
if (Object.keys(value).length === 0) {
reject('No stored value found');
return;
}
resolve(value[Object.keys(value)[0]]);
});
});
}
setData(data:FontData):Promise<void> {
return Utility.onActiveTab().then((tab) => {
let host = this._getHost(tab.url);
let dataObj = {};
dataObj[host] = data;
return browser.storage.local.set(dataObj);
});
}
remove():Promise<void> {
return Utility.onActiveTab().then((tab) => {
let host = this._getHost(tab.url);
return browser.storage.local.remove(host);
});
}
clear():Promise<void> {
return browser.storage.local.clear();
}
}
class FontData {
constructor(index:number, size:number, isEnable:boolean) {
this.index = index;
this.size = size;
this.isEnable = isEnable;
}
index:number;
size:number;
isEnable:boolean;
}