4 // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode
5 // which throws when accessing the localStorage property itself, as opposed
6 // to the standard behaviour of throwing on getItem/setItem. (T148998)
8 localStorage
= ( function () {
10 return window
.localStorage
;
13 sessionStorage
= ( function () {
15 return window
.sessionStorage
;
20 * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`)
21 * that is safe to call on all browsers.
23 * @class mw.SafeStorage
25 * @param {Object|undefined} store The Storage instance to wrap around
27 function SafeStorage( store
) {
32 * Retrieve value from device storage.
34 * @param {string} key Key of item to retrieve
35 * @return {string|boolean} False when localStorage not available, otherwise string
37 SafeStorage
.prototype.get = function ( key
) {
39 return this.store
.getItem( key
);
45 * Set a value in device storage.
47 * @param {string} key Key name to store under
48 * @param {string} value Value to be stored
49 * @return {boolean} Whether the save succeeded or not
51 SafeStorage
.prototype.set = function ( key
, value
) {
53 this.store
.setItem( key
, value
);
60 * Remove a value from device storage.
62 * @param {string} key Key of item to remove
63 * @return {boolean} Whether the save succeeded or not
65 SafeStorage
.prototype.remove = function ( key
) {
67 this.store
.removeItem( key
);
74 * A wrapper for the HTML5 `localStorage` interface
75 * that is safe to call on all browsers.
79 * @extends mw.SafeStorage
81 mw
.storage
= new SafeStorage( localStorage
);
84 * A wrapper for the HTML5 `sessionStorage` interface
85 * that is safe to call on all browsers.
89 * @extends mw.SafeStorage
91 mw
.storage
.session
= new SafeStorage( sessionStorage
);