forked from yogeshmundada/appu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage.js
More file actions
90 lines (74 loc) · 2.36 KB
/
Copy pathstorage.js
File metadata and controls
90 lines (74 loc) · 2.36 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
// Storage to local unlimited storage.
// None of the sensitive data is stored here.
// I am maintaining this because on Chrome on MacOS, a call to
// get all keys currently on harddisk does not succeed for extention Appu.
// Works for other sample extention that I wrote to test this feature and also works
// on other platforms (Win, Linux) for Appu extention. For now, this is a workaround.
var storage_meta = {
"storage_meta" : []
};
function print_storage_keys() {
for (var i = 0; i < storage_meta.storage_meta.length; i++) {
console.log("APPU DEBUG: " + (i+1) + ". Key: '" + storage_meta.storage_meta[i] + "'");
}
print_storage_size();
}
function init_storage_meta() {
read_from_local_storage("storage_meta", function(rc) {
if (rc != undefined &&
JSON.stringify(rc) != JSON.stringify({})) {
storage_meta = rc;
}
});
}
function cb_print(msg, convert_to_readable) {
convert_to_readable = (convert_to_readable == undefined) ? false: true;
if (msg == undefined) {
msg = "APPU DEBUG: (localStorage callback)";
}
return function (rc) {
if (rc != undefined) {
if (convert_to_readable) {
rc = get_human_readable_size(rc);
msg += rc;
}
else {
msg += JSON.stringify(rc);
}
}
console.log(msg);
}
}
function print_storage_size() {
get_storage_size(cb_print("APPU DEBUG: Local Storage Size: ", true));
}
function get_storage_size(cb) {
return chrome.storage.local.getBytesInUse(null, cb);
}
function expunge_local_storage() {
var b = storage_meta["storage_meta"].slice();
for (var i = 0; i < b.length; i++) {
delete_from_local_storage(b[i]);
}
// chrome.storage.local.clear(cb);
}
function write_to_local_storage(data) {
chrome.storage.local.set(data);
data_key = Object.keys(data)[0];
if (storage_meta["storage_meta"].indexOf(data_key) == -1) {
storage_meta["storage_meta"].push(data_key);
}
chrome.storage.local.set(storage_meta);
}
function read_from_local_storage(key, cb) {
if (cb == undefined) {
cb = cb_print("APPU DEBUG: Reading from local storage key: " + key + "\n")
}
chrome.storage.local.get(key, cb);
}
function delete_from_local_storage(key) {
chrome.storage.local.remove(key);
storage_meta["storage_meta"].splice(storage_meta["storage_meta"].indexOf(key), 1);
chrome.storage.local.set(storage_meta);
}
init_storage_meta();