-
Notifications
You must be signed in to change notification settings - Fork 0
/
appStorage.js
131 lines (118 loc) · 3.47 KB
/
appStorage.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
//
// Copyright (c) 2014 daiz. All rights reserved.
// This code may only be used under the BSD style license.
//
var appStorage = (function(key_value_json, operation, callback) {
var json = key_value_json;
var key = "APPSTORAGE_DEFAULT";
if(typeof json == "object") {
key = key_value_json.key;
}else if(typeof json == "string"){
key = key_value_json;
}
var val = key_value_json.value || "";
function isChromeApp() {
var res = false;
if(chrome != undefined) {
if(chrome.app.window != undefined) {
// 「chrome アプリ」である
res = true;
}
}
return res;
}
//
// chrome app の場合の処理
//
var chrome_item = {};
function chromeApp() {
var storageArea = chrome.storage.local;
switch(operation) {
case "set":
var item = {};
item[key] = val;
chrome_item = item;
storageArea.set(item, chrome_transactionCompleted);
break;
case "get":
storageArea.get(key, chrome_transactionCompleted);
break;
case "remove":
chrome_item[key] = null;
storageArea.remove(key, chrome_transactionCompleted);
break;
default: break;
}
}
function chrome_transactionCompleted(e) {
var res;
if(e != undefined) {
res = e;
}else {
res = chrome_item;
}
transactionCompleted(res);
}
//
// browser の場合の処理
//
function browser() {
// checkDegitを付加しておく
if(typeof val == "object" || typeof val == "number") {
val = JSON.stringify(val);
val = val + "1";
}else {
val = val + "0";
}
var item = {};
switch(operation) {
case "set":
localStorage.setItem(key, val);
item[key] = json.value;
transactionCompleted(item);
break;
case "get":
var v = localStorage.getItem(key);
if(v != undefined) {
var checkDigit = +(v[v.length - 1]);
var v = v.slice(0, -1);
if(checkDigit == 1) {
v = JSON.parse(v);
}
}
item[key] = v;
transactionCompleted(item);
break;
case "remove":
localStorage.removeItem(key);
item[key] = null;
transactionCompleted(item);
break;
default: break;
}
}
//
// どちらの場合でも実行する処理
//
function transactionCompleted(j) {
//console.group("["+operation+"] transaction completed.");
//console.dirxml(j);
//console.dirxml(Object.keys(j));
if(callback != undefined) {
//console.groupEnd("["+operation+"] transaction completed.");
callback(j, Object.keys(j));
}else {
//console.info("'callback' does not exist.");
//console.groupEnd("["+operation+"] transaction completed.");
}
}
function main() {
if(isChromeApp() == true) {
chromeApp();
}else {
browser();
}
}
main();
//transactionCompleted();
});