forked from greatsuspender/thegreatsuspender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsStorage.js
167 lines (129 loc) · 5.18 KB
/
gsStorage.js
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*global window, document, chrome, console, localStorage */
(function (window) {
"use strict";
var gsStorage = {
fetchPreviewImage : function (tabUrl, callback) {
chrome.storage.local.get(null, function (items) {
if (typeof (items.gsPreviews) === 'undefined') {
items.gsPreviews = {};
chrome.storage.local.set(items);
callback(null);
} else if (typeof (items.gsPreviews[tabUrl]) === 'undefined') {
callback(null);
} else {
callback(items.gsPreviews[tabUrl]);
}
});
},
setPreviewImage : function (tabUrl, previewUrl) {
chrome.storage.local.get(null, function (items) {
if (typeof (items.gsPreviews) === 'undefined') {
items.gsPreviews = {};
}
items.gsPreviews[tabUrl] = previewUrl;
chrome.storage.local.set(items);
});
},
clearPreviews : function () {
chrome.storage.local.get(null, function (items) {
items.gsPreviews = {};
chrome.storage.local.set(items);
});
},
fetchPreviewOption : function () {
return localStorage.getItem("preview") ? localStorage.getItem("preview") === "true" : false;
},
setPreviewOption : function (preview) {
localStorage.setItem("preview", preview);
},
fetchTimeToSuspendOption : function () {
return localStorage.getItem("gsTimeToSuspend") || 0;
},
setTimeToSuspendOption : function (timeToSuspend) {
localStorage.setItem("gsTimeToSuspend", timeToSuspend);
},
fetchUnsuspendOnFocusOption : function () {
return localStorage.getItem("gsUnsuspendOnFocus") ? localStorage.getItem("gsUnsuspendOnFocus") === "true" : false;
},
setUnsuspendOnFocusOption : function (unsuspendOnFocus) {
localStorage.setItem("gsUnsuspendOnFocus", unsuspendOnFocus);
},
fetchDontSuspendPinnedOption : function () {
var hasOption = localStorage.getItem("gsDontSuspendPinned");
if(hasOption) {
var val = localStorage.getItem("gsDontSuspendPinned");
// console.log("gsStorage has option dontSuspend, value: " + val)
}
return hasOption ? localStorage.getItem("gsDontSuspendPinned") === "true" : false;
},
setDontSuspendPinnedOption : function (dontSuspendPinned) {
localStorage.setItem("gsDontSuspendPinned", dontSuspendPinned);
},
fetchVersion : function () {
return localStorage.getItem('gsVersion');
},
setVersion : function (newVersion) {
localStorage.setItem('gsVersion', newVersion);
},
fetchWhitelist : function () {
return localStorage.getItem('gsWhitelist') || "";
},
setWhitelist : function (whitelist) {
localStorage.setItem("gsWhitelist", whitelist);
},
saveToWhitelist : function (newString) {
var whitelist = localStorage.getItem("gsWhitelist") || "";
localStorage.setItem("gsWhitelist", whitelist + " " + newString);
},
fetchOldGsHistory : function () {
var result = localStorage.getItem('gsHistory');
if (result !== null) {
result = JSON.parse(result);
}
return result;
},
removeOldGsHistory : function () {
localStorage.removeItem('gsHistory');
},
fetchGsHistory : function () {
var result = localStorage.getItem('gsHistory2');
if (result === null) {
result = [];
} else {
result = JSON.parse(result);
}
return result;
},
setGsHistory : function (gsHistory) {
localStorage.setItem('gsHistory2', JSON.stringify(gsHistory));
},
clearGsHistory : function (gsHistory) {
this.setGsHistory([]);
},
fetchTabFromHistory : function (tabUrl) {
var gsHistory = this.fetchGsHistory(),
i;
for (i = 0; i < gsHistory.length; i++) {
if (gsHistory[i].url === tabUrl) {
return gsHistory[i];
}
}
return false;
},
saveTabToHistory : function (tabUrl, tabProperties) {
var gsHistory = this.fetchGsHistory(),
i;
for (i = 0; i < gsHistory.length; i++) {
if (gsHistory[i].url === tabUrl) {
gsHistory[i] = tabProperties;
localStorage.setItem('gsHistory2', JSON.stringify(gsHistory));
//break; dont break anymore. want to update them all.
}
}
},
generateSuspendedUrl : function (tabUrl) {
return chrome.extension.getURL("suspended.html" + "#url=" + encodeURIComponent(tabUrl));
}
};
window.gsStorage = gsStorage;
}(window));