PageSourceSearch

https://www.pollsmax.com/assets/pollsmax-header-auth-early.js?v=35

js pollsmax.com collected 2026-09-28 06:38:11 UTC 19,548 bytes, 419 lines download raw bytes

1/*! Pollsmax header Login/Account — sync-friendly early paint from local session cache.
2 * Loaded from theme-boot in <head>. Hides CTA until resolved so Login never flashes to Account.
3 */
4(function () {
5    'use strict';
6
7    if (window.PollsmaxHeaderAuthEarly && window.PollsmaxHeaderAuthEarly.__booted) {
8        try { window.PollsmaxHeaderAuthEarly.resolve(); } catch (eRe) { /* ignore */ }
9        return;
10    }
11
12    var CACHE_KEY = 'pm-auth-user-v1';
13    var CSS_ID = 'pollsmax-header-auth-css';
14    var PENDING_CSS_ID = 'pollsmax-header-auth-pending-css';
15    var AUTH_SRC = '/assets/pollsmax-auth.js?v=36';
16    var resolved = false;
17    var LOGIN_ICON_SVG =
18        '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="16" height="16" fill="currentColor" aria-hidden="true">' +
19        '<path d="M12 12a4.25 4.25 0 1 0-4.25-4.25A4.25 4.25 0 0 0 12 12zm0 2.1c-3.55 0-8.1 1.78-8.1 4.05v1.35c0 .55.45 1 1 1h14.2c.55 0 1-.45 1-1v-1.35c0-2.27-4.55-4.05-8.1-4.05z"/>' +
20        '</svg>';
21
22    function injectPendingCss() {
23        try {
24            if (document.getElementById(PENDING_CSS_ID)) return;
25            var st = document.createElement('style');
26            st.id = PENDING_CSS_ID;
27            st.textContent =
28                '#mainHeader #pollsmax-header-donate:not([data-pm-auth-ready="1"]),' +
29                '#mainHeader a.pollsmax-header-donate:not([data-pm-auth-ready="1"]){' +
30                'visibility:hidden!important;}';
31            (document.head || document.documentElement).appendChild(st);
32        } catch (eCss) { /* ignore */ }
33    }
34
35    function injectAccountCss() {
36        try {
37            if (document.getElementById(CSS_ID)) return;
38            var st = document.createElement('style');
39            st.id = CSS_ID;
40            st.textContent =
41                'body.sleek-home #mainHeader a.pollsmax-header-donate.pollsmax-header-donate--account,' +
42                '#mainHeader a.pollsmax-header-donate.pollsmax-header-donate--account,' +
43                '#mainHeader .pollsmax-header-donate.pollsmax-header-donate--account{' +
44                'gap:.4rem!important;padding:0 .7rem 0 .3rem!important;' +
45                'border:1px solid var(--color-toggle-border,rgba(71,85,105,.55))!important;' +
46                'background:var(--color-toggle-bg,rgba(15,23,42,.35))!important;' +
47                'background-image:none!important;' +
48                'color:var(--color-toggle-fg,#fff)!important;' +
49                'box-shadow:none!important;filter:none!important;}' +
50                'body.sleek-home #mainHeader a.pollsmax-header-donate.pollsmax-header-donate--account:hover,' +
51                '#mainHeader a.pollsmax-header-donate.pollsmax-header-donate--account:hover,' +
52                '#mainHeader .pollsmax-header-donate.pollsmax-header-donate--account:hover{' +
53                'background:var(--color-toggle-bg-hover,rgba(30,41,59,.65))!important;' +
54                'background-image:none!important;filter:none!important;}' +
55                '#mainHeader .pollsmax-header-donate__avatar{width:1.25rem;height:1.25rem;border-radius:999px;object-fit:cover;display:block;flex-shrink:0;}' +
56                '#mainHeader .pollsmax-header-donate__initial{width:1.25rem;height:1.25rem;border-radius:999px;display:inline-flex;align-items:center;justify-content:center;flex-shrink:0;background:#334155;color:#fff;font-size:.65rem;font-weight:700;line-height:1;}' +
57                '#mainHeader .pollsmax-header-donate__label{line-height:1;}' +
58                '#mainHeader .pollsmax-header-donate__login-icon{display:inline-flex;align-items:center;justify-content:center;' +
59                'flex-shrink:0;width:1.05rem;height:1.05rem;line-height:0;color:#fff;}' +
60                '#mainHeader .pollsmax-header-donate__login-icon svg{width:100%;height:100%;display:block;}' +
61                '#mainHeader .pollsmax-header-donate:not(.pollsmax-header-donate--account){' +
62                'width:2rem!important;height:2rem!important;min-width:2rem!important;max-width:2rem!important;' +
63                'min-height:2rem!important;max-height:2rem!important;padding:0!important;gap:0!important;' +
64                'font-size:0!important;border-radius:999px!important;overflow:hidden!important;' +
65                'border:1px solid #a78bfa!important;' +
66                'background:linear-gradient(135deg,#7c3aed,#a855f7)!important;}' +
67                '#mainHeader .pollsmax-header-donate:not(.pollsmax-header-donate--account) .pollsmax-header-donate__label{display:none!important;}' +
68                'html[data-theme="light"] #mainHeader .pollsmax-header-donate.pollsmax-header-donate--account,' +
69                'html[data-pollsmax-theme="light"] #mainHeader .pollsmax-header-donate.pollsmax-header-donate--account{' +
70                'border-color:var(--color-toggle-border,#94a3b8)!important;' +
71                'background:var(--color-toggle-bg,#f1f5f9)!important;' +
72                'background-image:none!important;' +
73                'color:var(--color-toggle-fg,#000)!important;}' +
74                '@media (max-width:767px){' +
75                '#mainHeader .pollsmax-theme-toggle,#mainHeader #pollsmax-theme-toggle,' +
76                '#mainHeader #pollsmax-home-theme-toggle{display:none!important;visibility:hidden!important;' +
77                'pointer-events:none!important;opacity:0!important;}' +
78                /* Temporarily hide purple Login circle on mobile. */
79                '#mainHeader .pollsmax-header-donate:not(.pollsmax-header-donate--account),' +
80                '#mainHeader #pollsmax-header-donate:not(.pollsmax-header-donate--account),' +
81                '#mainHeader .flex.items-center.justify-between>.pollsmax-header-donate:not(.pollsmax-header-donate--account),' +
82                '#mainHeader .flex.items-center.justify-between>#pollsmax-header-donate:not(.pollsmax-header-donate--account){' +
83                'display:none!important;visibility:hidden!important;pointer-events:none!important;opacity:0!important;}' +
84                '#mainHeader .flex.items-center.justify-between>.pollsmax-header-donate--account,' +
85                '#mainHeader .flex.items-center.justify-between>#pollsmax-header-donate.pollsmax-header-donate--account{' +
86                'display:inline-flex!important;position:absolute!important;' +
87                /* Fallback must be 0px (not unitless 0) or calc() is invali
87d and right:auto centers the control. */
88                'right:calc(var(--et-mob-nav-btn-left,0px) + 2px)!important;' +
89                'top:50%!important;transform:translateY(-50%)!important;z-index:12!important;' +
90                'margin:0!important;visibility:visible!important;opacity:1!important;' +
91                'width:2rem!important;height:2rem!important;min-width:2rem!important;max-width:2rem!important;' +
92                'min-height:2rem!important;max-height:2rem!important;padding:0!important;gap:0!important;' +
93                'border-radius:999px!important;overflow:hidden!important;' +
94                'border:1px solid rgba(71,85,105,.55)!important;' +
95                'background:rgba(15,23,42,.55)!important;background-image:none!important;}' +
96                '#mainHeader .pollsmax-header-donate--account .pollsmax-header-donate__label{display:none!important;}' +
97                '#mainHeader .pollsmax-header-donate--account .pollsmax-header-donate__avatar,' +
98                '#mainHeader .pollsmax-header-donate--account .pollsmax-header-donate__initial{' +
99                'width:100%!important;height:100%!important;border-radius:999px!important;}' +
100                '#mainHeader .pollsmax-header-donate--account .pollsmax-header-donate__initial{font-size:.75rem!important;}' +
101                '}'
102            (document.head || document.documentElement).appendChild(st);
103        } catch (eAc) { /* ignore */ }
104    }
105
106    function userFromUnknown(data) {
107        if (!data || typeof data !== 'object') return null;
108        var user = data.user ||
109            (data.currentSession && data.currentSession.user) ||
110            (data.session && data.session.user) ||
111            null;
112        if (user && user.id) return user;
113        if (data.id && (data.email || data.user_metadata || data.aud)) return data;
114        return null;
115    }
116
117    function peekStoredUser() {
118        try {
119            if (!window.localStorage) return null;
120            var cached = localStorage.getItem(CACHE_KEY);
121            if (cached) {
122                var cu = userFromUnknown(JSON.parse(cached));
123                if (cu && cu.id) return cu;
124            }
125            var i;
126            for (i = 0; i < localStorage.length; i++) {
127                var key = localStorage.key(i);
128                if (!key || key.indexOf('sb-') !== 0) continue;
129                if (key.indexOf('-auth-token') < 0) continue;
130                if (key.indexOf('code-verifier') >= 0) continue;
131                var raw = localStorage.getItem(key);
132                if (!raw) continue;
133                var data;
134                try { data = JSON.parse(raw); } catch (eParse) { continue; }
135                if (typeof data === 'string') {
136                    try { data = JSON.parse(data); } catch (e2) { continue; }
137                }
138                var user = userFromUnknown(data);
139                if (user && user.id) {
140                    try { cacheUser(user); } catch (eCache) { /* ignore */ }
141                    return user;
142                }
143            }
144        } catch (ePeek) { /* ignore */ }
145        return null;
146    }
147
148    function cacheUser(user) {
149        try {
150            if (!window.localStorage) return;
151            if (!user || !user.id) {
152                localStorage.removeItem(CACHE_KEY);
153                return;
154            }
155            localStorage.setItem(CACHE_KEY, JSON.stringify({
156                id: user.id,
157                email: user.email || '',
158                user_metadata: user.user_metadata || {}
159            }));
160        } catch (e) { /* ignore */ }
161    }
162
163    function clearUserCache() {
164        try {
165            if (window.localStorage) localStorage.removeItem(CACHE_KEY);
166        } catch (e) { /* ignore */ }
167    }
168
169    function isMobileHeaderLayout() {
170        try {
171            return typeof window.matchMedia === 'function' &&
172                window.matchMedia('(max-width:767px)').matches;
173        } catch (eMq) {
174            return false;
175        }
176    }
177
178    function findHeaderMeta(hdr, row) {
179        var meta = hdr && hdr.querySelector('.sleek-home-header-meta');
180        if (meta) return meta;
181        if (!row) return null;
182        var kids = row.querySelectorAll(':scope > div');
183        if (kids.length > 1) return kids[kids.length - 1];
184        return null;
185    }
186
187    /**
188     * Desktop: Account/Login MUST stay inside .sleek-home-header-meta with
189     * latest-polls / X / theme.
190     * Mobile: hoist Login/avatar onto the row (theme toggle is hidden).
191     */
192    function placeLink(link) {
193        var hdr = document.getElementById('mainHeader');
194        if (!hdr || !link) return;
195        var row = hdr.querySelector('.flex.items-center.justify-between');
196        var meta = findHeaderMeta(hdr, row);
197        if (isMobileHeaderLayout() && row) {
198            if (link.parentNode !== row) row.appendChild(link);
199            return;
200        }
201        if (meta) {
202            var themeInMeta = meta.querySelector(
203                '#pollsmax-theme-toggle, #pollsmax-home-theme-toggle, .pollsmax-theme-toggle'
204            );
205            if (themeInMeta && themeInMeta.parentNode === meta) {
206                if (link.parentNode !== meta || link.previousElementSibling !== themeInMeta) {
207                    if (themeInMeta.nextSibling) meta.insertBefore(link, themeInMeta.nextSibling);
208                    else meta.appendChild(link);
209                }
210            } else if (link.parentNode !== meta || meta.lastElementChild !== link) {
211                meta.appendChild(link);
212            }
213            return;
214        }
215        if (row && link.parentNode !== row) row.appendChild(link);
216    }
217
218    function safeAvatarSrc(url) {
219        var avatar = String(url || '').trim();
220        if (!avatar) return '';
221        if (avatar.indexOf('http') !== 0) return '';
222        if (/twitter\.svg|pollsmax-x|\/images\//i.test(avatar)) return '';
223        return avatar;
224    }
225
226    function ensureLink() {
227        var link = document.getElementById('pollsmax-header-donate');
228        if (!link) {
229            link = document.createElement('a');
230            link.id = 'pollsmax-header-donate';
231        }
232        return link;
233    }
234
235    function markReady(link) {
236        if (!link) link = document.getElementById('pollsmax-header-donate');
237        if (link) {
238            try { link.setAttribute('data-pm-auth-ready', '1'); } catch (eM) { /* ignore */ }
239        }
240        resolved = true;
241    }
242
243    function paintAccount(user) {
244        var hdr = document.getElementById('mainHeader');
245        if (!hdr || !user) return false;
246        injectAccountCss();
247        var link = ensureLink();
248        var meta = user.user_metadata || {};
249        var name = meta.full_name || meta.name || meta.display_name ||
250            (user.email ? String(user.email).split('@')[0] : 'Account');
251        var avatar = safeAvatarSrc(meta.avatar_url || meta.picture || '');
252        link.className = 'pollsmax-header-donate pollsmax-header-donate--account';
253        link.href = '/account/';
254        link.setAttribute('aria-label', 'Account');
255        link.title = 'Account';
256        link.innerHTML = '';
257        if (avatar) {
258            var img = document.createElement('img');
259            img.className = 'pollsmax-header-donate__avatar';
260            img.src = avatar;
261            img.alt = '';
262            img.width = 20;
263            img.height = 20;
264            img.referrerPolicy = 'no-referrer';
265            img.decoding = 'async';
266            img.onerror = function () {
267                try {
268                    if (img.parentNode) img.parentNode.removeChild(img);
269                    if (!link.querySelector('.pollsmax-header-donate__initial')) {
270                        var init = document.createElement('span');
271                        init.className = 'pollsmax-header-donate__initial';
272                        init.setAttribute('aria-hidden', 'true');
273                        init.textContent = (String(name).charAt(0) || 'A').toUpperCase();
274                        link.insertBefore(init, link.firstChild);
275                    }
276                } catch (eImg) { /* ignore */ }
277            };
278            link.appendChild(img);
279        } else {
280            var init = document.createElement('span');
281            init.className = 'pollsmax-header-donate__initial';
282            init.setAttribute('aria-hidden', 'true');
283            init.textContent = (String(name).charAt(0) || 'A').toUpperCase();
284            link.appendChild(init);
285        }
286        var label = document.createElement('span');
287        label.className = 'pollsmax-header-donate__label';
288        label.textContent = 'Account';
289        link.appendChild(label);
290        placeLink(link);
291        markReady(link);
292        return true;
293    }
294
295    function paintLogin() {
296        var hdr = document.getElementById('mainHeader');
297        if (!hdr) return false;
298        var link = ensureLink();
299        /* Never downgrade an Account chip that another path already painted. */
300        if (link.classList && link.classList.contains('pollsmax-header-donate--account') &&
301            link.getAttribute('data-pm-auth-ready') === '1') {
302            return true;
303        }
304        link.className = 'pollsmax-header-donate';
305        link.href = '/login/';
306        link.setAttribute('aria-label', 'Login');
307        link.title = 'Login';
308        link.innerHTML = '';
309        var icon = document.createElement('span');
310        icon.className = 'pollsmax-header-donate__login-icon';
311        icon.setAttribute('aria-hidden', 'true');
312        icon.innerHTML = LOGIN_ICON_SVG;
313        link.appendChild(icon);
314        var label = document.createElement('span');
315        label.className = 'pollsmax-header-donate__label';
316        label.textContent = 'Login';
317        link.appendChild(label);
318        placeLink(link);
319        markReady(link);
320        return true;
321    }
322
323    function resolve(opts) {
324        opts = opts || {};
325        injectPendingCss();
326        injectAccountCss();
327        var hdr = document.getElementById('mainHeader');
328        if (!hdr) return false;
329        var user = peekStoredUser();
330        if (!user) {
331            try {
332                if (window.PollsmaxAuth && typeof window.PollsmaxAuth.peekUser === 'function') {
333                    user = window.PollsmaxAuth.peekUser();
334                }
335            } catch (ePeekAuth) { /* ignore */ }
336        }
337        if (user) {
338            cacheUser(user);
339            return paintAccount(user);
340        }
341        /* Logged-out (or still unknown): only show Login when forced / timed out.
342         * Avoids Login→Account flash while auth.js is still confirming. */
343        if (opts.forceLogin) {
344            return paintLogin();
345        }
346        return false;
347    }
348
349    function ensureAuthScript() {
350        try {
351            if (document.getElementById('pollsmax-auth-js')) return;
352            var s = document.createElement('script');
353            s.id = 'pollsmax-auth-js';
354            s.async = true;
355            s.src = AUTH_SRC;
356            s.onload = function () {
357                try {
358                    if (window.PollsmaxAuth && typeof window.PollsmaxAuth.bootHeader === 'function') {
359                        window.PollsmaxAuth.bootHeader();
360                    }
361                } catch (eBoot) { /* ignore */ }
362            };
363            (document.head || document.documentElement).appendChild(s);
364        } catch (eLoad) { /* ignore */ }
365    }
366
367    function watchHeader() {
368        try {
369            if (document.getElementById('mainHeader')) {
370                resolve();
371            }
372            if (typeof MutationObserver !== 'function') return;
373            var obs = new MutationObserver(function () {
374                if (!document.getElementById('mainHeader')) return;
375                resolve();
376                if (resolved) {
377                    try { obs.disconnect(); } catch (eD) { /* ignore */ }
378                }
379            });
380            obs.observe(document.documentElement, { childList: true, subtree: true });
381            setTimeout(function () {
382                try { obs.disconnect(); } catch (eD2) { /* ignore */ }
383                if (!resolved) resolve({ forceLogin: true });
384            }, 8000);
385        } catch (eWatch) { /* ignore */ }
386    }
387
388    injectPendingCss();
389    injectAccountCss();
390    watchHeader();
391    ensureAuthScript();
392
393    if (document.readyState === 'loading') {
394        document.addEventListener('DOMContentLoaded', function () { resolve(); });
395    } else {
396        resolve();
397    }
398
399    /* Safety: never leave the CTA invisible forever. */
400    setTimeout(function () {
401        if (!resolved) resolve({ forceLogin: true });
402        var link = document.getElementById('pollsmax-header-donate');
403        if (link && link.getAttribute('data-pm-auth-ready') !== '1') {
404            markReady(link);
405        }
406    }, 1200);
407
408    window.PollsmaxHeaderAuthEarly = {
409        __booted: true,
410        CACHE_KEY: CACHE_KEY,
411        peekStoredUser: peekStoredUser,
412        cacheUser: cacheUser,
413        clearUserCache: clearUserCache,
414        paintAccount: paintAccount,
415        paintLogin: paintLogin,
416        resolve: resolve,
417        markReady: markReady
418    };
419})();

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.