Skip to content
This repository was archived by the owner on Mar 13, 2018. It is now read-only.

Commit 319c501

Browse files
committed
Use chrome packaged app storage apis
- Required when in packaged apps
1 parent 8ae5b55 commit 319c501

1 file changed

Lines changed: 59 additions & 40 deletions

File tree

polymer-localstorage/polymer-localstorage.html

Lines changed: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,47 +29,66 @@
2929
</style>
3030
</template>
3131
<script>
32-
Polymer('polymer-localstorage', {
33-
/**
34-
* The key to the data stored in localStorage.
35-
*
36-
* @attribute name
37-
* @type string
38-
* @default null
39-
*/
40-
/**
41-
* The data associated with the specified name.
42-
*
43-
* @attribute value
44-
* @type object
45-
* @default null
46-
*/
47-
/**
48-
* If true, the value is stored and retrieved without JSON processing.
49-
*
50-
* @attribute useRaw
51-
* @type boolean
52-
* @default false
53-
*/
54-
useRaw: false,
55-
ready: function() {
56-
this.load();
57-
},
58-
valueChanged: function() {
59-
this.save();
60-
},
61-
load: function() {
62-
var s = window.localStorage.getItem(this.name);
63-
if (s && !this.useRaw) {
64-
this.value = JSON.parse(s);
65-
} else {
66-
this.value = s;
32+
(function() {
33+
var CHROME_STORAGE = typeof window.chrome.storage !== 'undefined';
34+
Polymer('polymer-localstorage', {
35+
/**
36+
* The key to the data stored in localStorage.
37+
*
38+
* @attribute name
39+
* @type string
40+
* @default null
41+
*/
42+
/**
43+
* The data associated with the specified name.
44+
*
45+
* @attribute value
46+
* @type object
47+
* @default null
48+
*/
49+
/**
50+
* If true, the value is stored and retrieved without JSON processing.
51+
*
52+
* @attribute useRaw
53+
* @type boolean
54+
* @default false
55+
*/
56+
useRaw: false,
57+
ready: function() {
58+
this.load();
59+
if (CHROME_STORAGE) {
60+
this._boundLoaded = this._loaded.bind(this);
61+
}
62+
},
63+
valueChanged: function() {
64+
this.save();
65+
},
66+
_loaded: function(s) {
67+
if (s && !this.useRaw) {
68+
this.value = JSON.parse(s);
69+
} else {
70+
this.value = s;
71+
}
72+
},
73+
load: function() {
74+
if (CHROME_STORAGE) {
75+
window.chrome.storage.local.get(this.name, this._boundLoaded);
76+
} else {
77+
var s = window.localStorage.getItem(this.name);
78+
this._loaded(s);
79+
}
80+
},
81+
save: function() {
82+
var val = this.useRaw ? this.value : JSON.stringify(this.value);
83+
if (CHROME_STORAGE) {
84+
var obj = {};
85+
obj[name] = val;
86+
window.chrome.storage.local.set(obj);
87+
} else {
88+
window.localStorage.setItem(this.name, val);
89+
}
6790
}
68-
},
69-
save: function() {
70-
window.localStorage.setItem(this.name,
71-
this.useRaw ? this.value : JSON.stringify(this.value));
72-
}
91+
});
7392
});
7493
</script>
7594
</polymer-element>

0 commit comments

Comments
 (0)