From bb6241dedf405e750c8c627d6633dfafa8baf383 Mon Sep 17 00:00:00 2001 From: oc Date: Tue, 5 Mar 2019 23:48:37 +0800 Subject: [PATCH] compatible for LZString.decompress --- libs/utils.js | 57 ++++++++++++++++++++++----------------------------- 1 file changed, 25 insertions(+), 32 deletions(-) diff --git a/libs/utils.js b/libs/utils.js index 94cfd340..c2e63e24 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -163,28 +163,30 @@ utils.prototype.setLocalStorage = function(key, value) { } } +utils.prototype.decompress = function (value) { + try { + var output = lzw_decode(value); + if (core.isset(output) && output.length > 0) + return JSON.parse(output); + } + catch (e) {} + try { + var output = LZString.decompress(value); + if (core.isset(output) && output.length > 0) + return JSON.parse(output); + } + catch (e) {} + try { + return JSON.parse(value); + } + catch (e) {main.log(e);} + return null; +} + ////// 获得本地存储 ////// utils.prototype.getLocalStorage = function(key, defaultValue) { - try { - var value = localStorage.getItem(core.firstData.name+"_"+key); - if (core.isset(value)) { - var output = lzw_decode(value); - if (core.isset(output) && output.length>0) { - try { - return JSON.parse(output); - } - catch (ee) { - // Ignore, use default value - } - } - return JSON.parse(value); - } - return defaultValue; - } - catch (e) { - main.log(e); - return defaultValue; - } + var res = this.decompress(localStorage.getItem(core.firstData.name+"_"+key)); + return res==null?defaultValue:res; } ////// 移除本地存储 ////// @@ -244,18 +246,9 @@ utils.prototype.getLocalForage = function (key, defaultValue, successCallback, e else { if (!core.isset(successCallback)) return; if (core.isset(value)) { - try { - var output = lzw_decode(value); - if (core.isset(output) && output.length>0) { - try { - successCallback(JSON.parse(output)); - return; - } catch (ee) {main.log(ee);} - } - successCallback(JSON.parse(value)); - return; - } - catch (e) {main.log(e);} + var res = core.utils.decompress(value); + successCallback(res==null?defaultValue:res); + return; } successCallback(defaultValue); }