1(function (root, doc) { 2 'use strict'; 3 4 var Storage; 5 6 try { 7 if (!root.localStorage || !root.sessionStorage) { 8 throw new Error(); 9 } 10 11 localStorage.setItem('storage_test', 1); 12 localStorage.removeItem('storage_test'); 13 } catch (e) { 14 /** 15 * Returns a storage object to shim local or sessionStorage 16 * @param {String} type - either 'local' or 'session' 17 */ 18 Storage = function (type) { 19 var data; 20 21 /** 22 * Creates a cookie 23 * @param {String} name 24 * @param {String} value 25 * @param {Integer} days 26 */ 27 function createCookie(name, value, days) { 28 var date, expires; 29 30 if (days) { 31 date = new Date(); 32 date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000); 33 expires = '; expires=' + date.toGMTString(); 34 } else { 35 expires = ''; 36 } 37 doc.cookie = name + '=' + value + expires + '; path=/'; 38 } 39 40 /** 41 * Reads value of a cookie 42 * @param {String} name 43 */ 44 function readCookie(name) { 45 var nameEQ = name + '=', 46 ca = doc.cookie.split(';'), 47 i = 0, 48 c; 49 50 for (i = 0; i < ca.length; i++) { 51 c = ca[i]; 52 53 while (c.charAt(0) === ' ') { 54 c = c.substring(1, c.length); 55 } 56 57 if (c.indexOf(nameEQ) === 0) { 58 return c.substring(nameEQ.length, c.length); 59 } 60 } 61 62 return null; 63 } 64 65 /** 66 * Returns cookie name based upon the storage type. 67 * If this is session storage, the function returns a unique cookie per tab 68 */ 69 function getCookieName() { 70 71 if (type !== 'session') { 72 return 'localstorage'; 73 } 74 75 if (!root.name) { 76 root.name = new Date().getTime(); 77 } 78 79 return 'sessionStorage' + root.name; 80 } 81 82 /** 83 * Sets storage cookie to a data object 84 * @param {Object} dataObject 85 */ 86 function setData(dataObject) { 87 data = encodeURIComponent(JSON.stringify(dataObject)); 88 createCookie(getCookieName(), data, 365); 89 } 90 91 /** 92 * Clears value of cookie data 93 */ 94 function clearData() { 95 createCookie(getCookieName(), '', 365); 96 } 97 98 /** 99 * @returns value of cookie data 100 */ 101 function getData() { 102 var dataResponse = readCookie(getCookieName()); 103 104 return dataResponse ? JSON.parse(decodeURIComponent(dataResponse)) : {}; 105 } 106 107 data = getData(); 108 109 return { 110 length: 0, 111 112 /** 113 * Clears data from storage 114 */ 115 clear: function () { 116 data = {}; 117 this.length = 0; 118 clearData(); 119 }, 120 121 /** 122 * Gets an item from storage 123 * @param {String} key 124 */ 125 getItem: function (key) { 126 return data[key] === undefined ? null : data[key]; 127 }, 128 129 /** 130 * Gets an item by index from storage 131 * @param {Integer} i 132 */ 133 key: function (i) { 134 var ctr = 0, 135 k; 136 137 for (k in data) { 138 139 if (data.hasOwnProperty(k)) { 140 141 // eslint-disable-next-line max-depth 142 if (ctr.toString() === i.toString()) { 143 return k; 144 } 145 ctr++; 146 } 147 } 148 149 return null; 150 }, 151 152 /** 153 * Removes an item from storage 154 * @param {String} key 155 */ 156 removeItem: function (key) { 157 delete data[key]; 158 this.length--; 159 setData(data); 160 }, 161 162 /** 163 * Sets an item from storage 164 * @param {String} key 165 * @param {String} value 166 */ 167 setItem: function (key, value) { 168 data[key] = value.toString(); 169 this.length++; 170 setData(data); 171 } 172 }; 173 }; 174 175 root.localStorage.prototype = root.localStorage = new Storage('local'); 176 root.sessionStorage.prototype = root.sessionStorage = new Storage('session'); 177 } 178})(window, document);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.