X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=resources%2Fsrc%2Fmediawiki%2Fmediawiki.storage.js;h=20f8efb659ad8c9c4912fbaf52e4e3cf4a933f0c;hb=c2a681ba58c8718242d39450c34a603346126b98;hp=39583926bbf32ed4205746090883db57bd96ee9e;hpb=ba1daa54611411dd3c01344e0e7a0422ff0e76c4;p=lhc%2Fweb%2Fwiklou.git diff --git a/resources/src/mediawiki/mediawiki.storage.js b/resources/src/mediawiki/mediawiki.storage.js index 39583926bb..20f8efb659 100644 --- a/resources/src/mediawiki/mediawiki.storage.js +++ b/resources/src/mediawiki/mediawiki.storage.js @@ -1,58 +1,91 @@ ( function ( mw ) { 'use strict'; + // Catch exceptions to avoid fatal in Chrome's "Block data storage" mode + // which throws when accessing the localStorage property itself, as opposed + // to the standard behaviour of throwing on getItem/setItem. (T148998) + var + localStorage = ( function () { + try { + return window.localStorage; + } catch ( e ) {} + }() ), + sessionStorage = ( function () { + try { + return window.sessionStorage; + } catch ( e ) {} + }() ); + /** - * Library for storing device specific information. It should be used for storing simple - * strings and is not suitable for storing large chunks of data. + * A wrapper for an HTML5 Storage interface (`localStorage` or `sessionStorage`) + * that is safe to call on all browsers. * - * @class mw.storage - * @singleton + * @class mw.SafeStorage + * @private */ - mw.storage = { - localStorage: window.localStorage, + /** + * @ignore + * @param {Object|undefined} store The Storage instance to wrap around + */ + function SafeStorage( store ) { + this.store = store; + } - /** - * Retrieve value from device storage. - * - * @param {string} key Key of item to retrieve - * @return {string|boolean} False when localStorage not available, otherwise string - */ - get: function ( key ) { - try { - return mw.storage.localStorage.getItem( key ); - } catch ( e ) {} - return false; - }, - - /** - * Set a value in device storage. - * - * @param {string} key Key name to store under - * @param {string} value Value to be stored - * @returns {boolean} Whether the save succeeded or not - */ - set: function ( key, value ) { - try { - mw.storage.localStorage.setItem( key, value ); - return true; - } catch ( e ) {} - return false; - }, - - /** - * Remove a value from device storage. - * - * @param {string} key Key of item to remove - * @returns {boolean} Whether the save succeeded or not - */ - remove: function ( key ) { - try { - mw.storage.localStorage.removeItem( key ); - return true; - } catch ( e ) {} - return false; - } + /** + * Retrieve value from device storage. + * + * @param {string} key Key of item to retrieve + * @return {string|boolean} False when localStorage not available, otherwise string + */ + SafeStorage.prototype.get = function ( key ) { + try { + return this.store.getItem( key ); + } catch ( e ) {} + return false; + }; + + /** + * Set a value in device storage. + * + * @param {string} key Key name to store under + * @param {string} value Value to be stored + * @return {boolean} Whether the save succeeded or not + */ + SafeStorage.prototype.set = function ( key, value ) { + try { + this.store.setItem( key, value ); + return true; + } catch ( e ) {} + return false; }; + /** + * Remove a value from device storage. + * + * @param {string} key Key of item to remove + * @return {boolean} Whether the save succeeded or not + */ + SafeStorage.prototype.remove = function ( key ) { + try { + this.store.removeItem( key ); + return true; + } catch ( e ) {} + return false; + }; + + /** + * @class + * @singleton + * @extends mw.SafeStorage + */ + mw.storage = new SafeStorage( localStorage ); + + /** + * @class + * @singleton + * @extends mw.SafeStorage + */ + mw.storage.session = new SafeStorage( sessionStorage ); + }( mediaWiki ) );