oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1// fixtures/pref-cookie.mjs — a per-person editor preference kept in a
2// cookie the server reads, so it renders the preference and the first
3// paint is the final one: the inspector card's state (head.mjs,
4// slides-inspector) and the notes card's state and height
5// (notes-card.mjs, slides-notes and slides-notes-height). Never document
6// state.
7//
8// Every cookie is Path=/; Max-Age=31536000; SameSite=Lax, and Secure
9// on https. Where cookies cannot be read or written (blocked), reading
10// is as if absent and writing does nothing: the preference holds for
11// the page. Tested in pref-cookie.test.mjs.
12
13/** The cookie's value, or null when it is absent or unreadable. */
14export function readCookie (name, doc = globalThis.document) {
15 try {
16 const all = String(doc?.cookie || '')
17 for (const part of all.split(';')) {
18 const at = part.indexOf('=')
19 if (at > 0 && part.slice(0, at).trim() === name) return part.slice(at + 1).trim()
20 }
21 } catch { /* unreadable: as if absent */ }
22 return null
23}
24
25/** Writes the cookie for a year, site-wide; nothing where it cannot. */
26export function writeCookie (name, value, doc = globalThis.document, loc = globalThis.location) {
27 const secure = loc?.protocol === 'https:' ? '; Secure' : ''
28 try {
29 if (doc) doc.cookie = `${name}=${value}; Path=/; Max-Age=31536000; SameSite=Lax${secure}`
30 } catch { /* cookies blocked: this page only */ }
31}
32