|
29 | 29 | </style> |
30 | 30 | </template> |
31 | 31 | <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 | + } |
67 | 90 | } |
68 | | - }, |
69 | | - save: function() { |
70 | | - window.localStorage.setItem(this.name, |
71 | | - this.useRaw ? this.value : JSON.stringify(this.value)); |
72 | | - } |
| 91 | + }); |
73 | 92 | }); |
74 | 93 | </script> |
75 | 94 | </polymer-element> |
0 commit comments