Created
October 13, 2019 18:40
-
-
Save da411d/0ed5f42d22a6d91985d731f29e381ff7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let localForage = { | |
getItem: function (sKey) { | |
if (!sKey || !this.hasOwnProperty(sKey)) { | |
return null; | |
} | |
return unescape(document.cookie.replace(new RegExp("(?:^|.*;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*"), "$1")); | |
}, | |
key: function (nKeyId) { | |
return unescape(document.cookie.replace(/\s*\=(?:.(?!;))*$/, "").split(/\s*\=(?:[^;](?!;))*[^;]?;\s*/)[nKeyId]); | |
}, | |
setItem: function (sKey, sValue) { | |
if(!sKey) { | |
return; | |
} | |
document.cookie = escape(sKey) + "=" + escape(sValue) + "; expires=Tue, 19 Jan 2038 03:14:07 GMT; path=/"; | |
this.length = document.cookie.match(/\=/g).length; | |
}, | |
length: 0, | |
removeItem: function (sKey) { | |
if (!sKey || !this.hasOwnProperty(sKey)) { | |
return; | |
} | |
document.cookie = escape(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/"; | |
this.length--; | |
}, | |
hasOwnProperty: function (sKey) { | |
return (new RegExp("(?:^|;\\s*)" + escape(sKey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=")).test(document.cookie); | |
} | |
}; | |
localForage.length = (document.cookie.match(/\=/g) || []).length; | |
let localStorageAdapter = { | |
get: (key) => { | |
const sortListDataRaw = (localStorage || localForage).getItem('OnoriStorage') || '{}'; | |
const sortListData = JSON.parse(sortListDataRaw); | |
return sortListData[key]; | |
}, | |
set: (key, value) => { | |
const sortListDataRaw = (localStorage || localForage).getItem('OnoriStorage') || '{}'; | |
const sortListData = JSON.parse(sortListDataRaw); | |
sortListData[key] = value; | |
(localStorage || localForage).setItem('OnoriStorage', JSON.stringify(sortListData)); | |
}, | |
}; | |
window.addEventListener("message", (e) => { | |
let payload = JSON.parse(e.data); | |
if(!payload._id){ | |
return; | |
} | |
let response = { | |
_id: payload._id, | |
}; | |
switch(payload.action) { | |
case 'set': | |
localStorageAdapter.set(payload.key, payload.value); | |
response.data = true; | |
break; | |
case 'get': | |
response.data = localStorageAdapter.get(payload.key); | |
break; | |
case 'remove': | |
localStorageAdapter.set(payload.key, null); | |
response.data = true; | |
break; | |
} | |
let parent = window.parent; | |
parent.postMessage(JSON.stringify(response), "*"); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment