Last active
April 9, 2018 17:54
-
-
Save defaultvoice/8643ecf4c4871be1919e293e66ca9f64 to your computer and use it in GitHub Desktop.
Fallback для localStorage в private mode мобильного сафари
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
| /** | |
| * Create instance of Cookie. | |
| * | |
| * @constructor | |
| * @this {Cookie} | |
| */ | |
| function Cookie(name) { | |
| /** **/ | |
| } | |
| /** | |
| * getter | |
| * | |
| * @this {Cookie} | |
| * @return {string} value | |
| * | |
| * @static | |
| */ | |
| Cookie.prototype.getItem = function(name) { | |
| name = name.toString(); | |
| var matches = document.cookie.match(new RegExp( | |
| "(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)" | |
| )); | |
| return matches ? decodeURIComponent(matches[1]) : ''; | |
| }; | |
| /** | |
| * setter | |
| * | |
| * @this {Cookie} | |
| * | |
| * @static | |
| */ | |
| Cookie.prototype.setItem = function(name, value, options) { | |
| options = options || {expires: '', path: '/'}; | |
| name = name.toString(); | |
| value = value.toString(); | |
| var updatedCookie = name + "=" + encodeURIComponent(value) + | |
| ((options.expires) ? "; expires=" + options.expires : "") + | |
| ((options.path) ? "; path=" + options.path : "") + | |
| ((options.domain) ? "; domain=" + options.domain : "") + | |
| ((options.secure) ? "; secure" : ""); | |
| if (byteLength(document.cookie) <= 4000) { | |
| document.cookie = updatedCookie; | |
| } else { | |
| alert(window['overQuotaMessage'] || 'Вы превысили максимальный размер cookies') | |
| } | |
| }; | |
| /** | |
| * Create instance of Storage. | |
| * | |
| * @constructor | |
| * @this {Storage} | |
| */ | |
| function Storage() { | |
| try { | |
| localStorage.setItem('foo', 'bar'); | |
| localStorage.removeItem('foo'); | |
| this.store = localStorage | |
| } catch (e) { | |
| this.store = new Cookie(); | |
| } | |
| } | |
| /** | |
| * getter | |
| * | |
| * @this {Storage} | |
| * @return {string} value | |
| */ | |
| Storage.prototype.getItem = function(name) { | |
| return this.store.getItem(name); | |
| }; | |
| /** | |
| * setter | |
| * | |
| * @this {Storage} | |
| */ | |
| Storage.prototype.setItem = function(name, value) { | |
| return this.store.setItem(name, value); | |
| }; | |
| /** | |
| * returns the byte length of an utf8 string | |
| * | |
| * @param {string} str - utf8 string | |
| * @return {number} byte length | |
| */ | |
| function byteLength(str) { | |
| var s = str.length; | |
| for (var i=str.length-1; i>=0; i--) { | |
| var code = str.charCodeAt(i); | |
| if (code > 0x7f && code <= 0x7ff) s++; | |
| else if (code > 0x7ff && code <= 0xffff) s+=2; | |
| if (code >= 0xDC00 && code <= 0xDFFF) i--; //trail surrogate | |
| } | |
| return s; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment