Path: csiph.com!weretis.net!feeder8.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED.178.197.213.187!not-for-mail From: Thomas 'PointedEars' Lahn Newsgroups: comp.lang.javascript Subject: Re: Create cookie from ID of tag Date: Wed, 31 Mar 2021 00:58:01 +0200 Organization: PointedEars Software (PES) Lines: 295 Message-ID: <11728310.O9o76ZdvQC@PointedEars.de> References: <06c15260-54cb-43fd-99f0-cba71b0caa36n@googlegroups.com> <01863a61-d1bf-47ec-9955-1a1579e9aca2n@googlegroups.com> <48e39d8c-ec2d-4759-8ddd-8ac063e510c3n@googlegroups.com> <2578953.mvXUDI8C0e@PointedEars.de> <4641170.31r3eYUQgx@PointedEars.de> <2196370.ElGaqSPkdT@PointedEars.de> Reply-To: Thomas 'PointedEars' Lahn Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit Injection-Info: gwaiyur.mb-net.net; posting-host="178.197.213.187"; logging-data="22140"; mail-complaints-to="abuse@open-news-network.org" User-Agent: KNode/4.14.10 Cancel-Lock: sha1:KOOvYCCJOjVtCAlFTQ2ZRfhdcHM= X-User-ID: U2FsdGVkX1/B6jO5OC8gOVXaPIDJZd/jCcMQbShiKIRX7nd/G88h8g== Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== X-Face: %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&. On Friday, March 26, 2021 at 5:22:39 PM UTC-5, Thomas 'PointedEars' Lahn > wrote: >> As I keep thinking about this, one would hope that after all this time >> that cookies have been around, and given my impression that cookies are >> not going away anytime soon, a standard DOM attribute and built-in >> property will finally be introduced to make dealing with cookies easier >> and less error- prone. > > I've use the following for quite some time. I'd write it a bit differently > today but it's been a good enough solution thus far for me: > > Thank you. My retrieval/storage code (specifically written backwards compatible) looks similar, but I found it a good idea to include the possibility of the cookie data not being live so as to make access to it more efficient. If you have suggestions for improvement, please let me know: -- cookie.js ------------------------------------------------------------ /** * This library is intended to provide easy access to cookies. * * Read cookie by name: * cookies['$NAME'] === value * Save cookie/update value/properties: * cookies['name'] = value * cookies['name'] = {value: value, properties} * cookies['name'].property = propertyValue [not yet implemented] * Delete cookie (set expires to 0): * delete cookies['name'] (for the current domain) * cookies.removeItem('name', {domain: '$DOMAIN', properties}) * (for allowed domains) * @type {Object} * * // begin WebIDL * * [Exposed=jsx.dom] * interface mixin DocumentStorage { * readonly attribute Cookies cookies; * } * * [Exposed=DocumentStorage] * interface Cookies { * readonly attribute unsigned long length; * DOMString? name(unsigned long index); * sequence names(); * sequence values(); * sequence > items(); * getter DOMString? getItem(DOMString name); * boolean hasItem(DOMString name); * setter boolean setItem(DOMString name, DOMString value); * setter boolean setItem(DOMString name, CookieAttributes attributes); * deleter boolean removeItem(DOMString name); * boolean clear(); * } * * dictionary CookieAttributes { * required DOMString value; * DOMString path = Location::pathname; * unsigned long? maxAge = null; // default: end of session * * // or unsigned long, but Date would be more convenient * Date? expires = null; // default: at the end of the session * * DOMString domain = Location::hostname; * boolean secure = false; * boolean sameSite = false; * } * * // end WebIDL */ /*eslint-env es6 */ jsx.dom.cookies = { /** Internal dictionary of cookies */ _items: Object.create(null), /** * Determines whether this object uses live data. * * If true, methods of this object update the internal * dictionary from document.cookie; otherwise they only * read from the internal dictionary which can be updated manually * by calling {@link #update()}. * * Set this property to false to make repeated access * more efficient. * * @type {Boolean} */ live: true, Cookie: function (properties) { var proto = Object.getPrototypeOf(this); Object.keys(properties).forEach(function (key) { if (key in proto && (typeof proto[key] != 'function')) { var value = properties[key]; var setterName = 'set' + key.charAt(0).toUpperCase() + key.substring(1); if (typeof this[setterName] == 'function') { this[setterName](value); } else { this[key] = value; } } }.bind(this)); }.extend(null, { name: '', value: null, path: '', maxAge: null, expires: null, domain: '', secure: false, sameSite: '', setMaxAge: function (value) { this.maxAge = parseInt(value, 10); }, setExpires: function (value) { this.expires = (value instanceof Date) ? value : new Date(value); }, setSecure: function (value) { this.secure = !!value; }, setSameSite: function (value) { if ([void 0, 'lax', 'strict', 'none'].indexof(value) < 0) { throw new jsx.InvalidArgumentError( 'Expected: samesite=(lax | strict | none)'); } this.sameSite = value; }, toString: function () { return [this.name, this.value].map(encodeURIComponent).join('=') + (this.path ? ';path=' + this.path : '') + (this.maxAge ? ';max-age=' + this.maxAge : '') + (this.expires ? ';expires=' + this.expires.toUTCString() : '') + (this.domain ? ';domain=' + this.domain : '') + (this.secure ? ';secure' : '') + (this.sameSite ? ';samesite=' + this.sameSite : ''); } }), /** * Update the internal dictionary from document.cookie. * * @return {Object} The updated dictionary */ update: function () { this._items = Object.create(null); var cookies = document.cookie; if (cookies.indexOf('=') > 0) { cookies.split(/\s*;\s*/) .forEach(function (cookie) { var parts = cookie.split('=').map(decodeURIComponent); var name = parts[0]; var value = parts.slice(1).join('='); this._items[name] = new this.Cookie({name: name, value: value}); }.bind(this)); } return this._items; }, items: function () { if (this.live) this.update(); return this._items; }, /** @return int */ length: function () { return this.names().length; }, /** @return String */ name: function (/** int */ index) { return this.names()[index]; }, /** @return Array[String] */ names: function () { return Object.keys(this.items()); }, /** @return sequence */ values: function () { return this.names().map(function (name) { return this.getItem(name).value; }.bind(this)); }, /** @return getter DOMString? */ getItem: function (/** DOMString */ name) { return this.items()[name]; }, /** @return boolean */ hasItem: function (/** DOMString */ name) { return (name in this.items()); }, /** @type setter boolean */ setItem: function (/** DOMString */ name, /** DOMString */ value) { var cookie; if (jsx.object.isObject(value)) { cookie = new this.Cookie(value); } else { cookie = new this.Cookie({name: name, value: value}); } this._items[name] = cookie; document.cookie = cookie.toString(); }, /** * Delete a cookie * * @deleter * @param {DOMString} name * @return {[type]} * true if successful, false otherwise. */ removeItem: function (name) { document.cookie = (new this.Cookie({ name: name, expires: 0 })).toString(); this.update(); return !this.hasItem(name); }, /** @return boolean */ clear: function () { this.names().forEach(function (name) { this.removeItem(name); }.bind(this)); } }; if (typeof Proxy == 'function') { jsx.dom.cookies = new Proxy(jsx.dom.cookies, { get: function (target, propertyName) { if (propertyName in target) { return target[propertyName]; } return target.getItem(propertyName); }, set: function (target, propertyName, value) { if (propertyName in target) { target[propertyName] = value; } else { target.setItem(propertyName, value); } }, deleteProperty: function (target, propertyName) { if (!(propertyName in target)) { return target.removeItem(propertyName); } } }); } ------------------------------------------------------------------------- -- PointedEars FAQ: | | Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.