(function () { const cacheName = "sls4all-service-worker"; const cacheKey = "/cache/v1"; var allCookiesForOrigins = null; var defaultParseOptions = { decodeValues: true, map: false, }; function isNonEmptyString(str) { return typeof str === "string" && !!str.trim(); } function parseString(setCookieValue, options) { var parts = setCookieValue.split(";").filter(isNonEmptyString); var nameValuePairStr = parts.shift(); var parsed = parseNameValuePair(nameValuePairStr); var name = parsed.name; var value = parsed.value; options = options ? Object.assign({}, defaultParseOptions, options) : defaultParseOptions; try { value = options.decodeValues ? decodeURIComponent(value) : value; } catch { } var cookie = { name: name, value: value, }; parts.forEach(function (part) { var sides = part.split("="); var key = sides.shift().trimLeft().toLowerCase(); var value = sides.join("="); if (key === "expires") { cookie.expires = new Date(value); } else if (key === "max-age") { cookie.maxAge = parseInt(value, 10); } else if (key === "secure") { cookie.secure = true; } else if (key === "httponly") { cookie.httpOnly = true; } else if (key === "samesite") { cookie.sameSite = value; } else if (key === "partitioned") { cookie.partitioned = true; } else { cookie[key] = value; } }); return cookie; } function parseNameValuePair(nameValuePairStr) { var name = ""; var value = ""; var nameValueArr = nameValuePairStr.split("="); if (nameValueArr.length > 1) { name = nameValueArr.shift(); value = nameValueArr.join("="); } else { value = nameValuePairStr; } return { name: name, value: value }; } function splitCookiesString(cookiesString) { if (Array.isArray(cookiesString)) { return cookiesString; } if (typeof cookiesString !== "string") { return []; } var cookiesStrings = []; var pos = 0; var start; var ch; var lastComma; var nextStart; var cookiesSeparatorFound; function skipWhitespace() { while (pos < cookiesString.length && /\s/.test(cookiesString.charAt(pos))) { pos += 1; } return pos < cookiesString.length; } function notSpecialChar() { ch = cookiesString.charAt(pos); return ch !== "=" && ch !== ";" && ch !== ","; } while (pos < cookiesString.length) { start = pos; cookiesSeparatorFound = false; while (skipWhitespace()) { ch = cookiesString.charAt(pos); if (ch === ",") { lastComma = pos; pos += 1; skipWhitespace(); nextStart = pos; while (pos < cookiesString.length && notSpecialChar()) { pos += 1; } if (pos < cookiesString.length && cookiesString.charAt(pos) === "=") { cookiesSeparatorFound = true; pos = nextStart; cookiesStrings.push(cookiesString.substring(start, lastComma)); start = pos; } else { pos = lastComma + 1; } } else { pos += 1; } } if (!cookiesSeparatorFound || pos >= cookiesString.length) { cookiesStrings.push(cookiesString.substring(start, cookiesString.length)); } } return cookiesStrings; } function getPseudoOrigin(url) { const remoteText = '/remote-printer/'; const remoteIndex = url.indexOf(remoteText); if (remoteIndex != -1) { var pos = url.indexOf('/', remoteIndex + remoteText.length); if (pos == -1) pos = url.length; return url.substring(0, pos); } else return new URL(url).origin } async function wrapFetch(request) { if (allCookiesForOrigins === null) { const cache = await caches.open(cacheName); const data = await caches.match(cacheKey); allCookiesForOrigins = (data != null ? JSON.parse(await data.text()) : null) || {}; } var pseudoOrigin = getPseudoOrigin(request.url); var xcookies = allCookiesForOrigins[pseudoOrigin]; if (xcookies == null) allCookiesForOrigins[pseudoOrigin] = xcookies = {}; const siteMeta = request.headers.get('sec-fetch-site'); const sameOrigin = new URL(request.url).origin === self.location.origin; const isSameSite = siteMeta ? (siteMeta === 'same-origin' || siteMeta === 'same-site') : sameOrigin; if (isSameSite) { const headers = new Headers(request.headers); for (const [key, value] of Object.entries(xcookies)) headers.append("X-Cookie", key + "=" + encodeURIComponent(value)); request = new Request(request, { headers: headers, mode: 'same-origin', cache: 'default' }); } const response = await fetch(request); if (response.type !== 'basic') return response; const headers = new Headers(response.headers); const setCookie = headers.get("X-Set-Cookie"); if (setCookie) { const cookies = splitCookiesString(setCookie); for (const cookie of cookies) { const item = parseString(cookie); if (item && item.name) { xcookies[item.name] = item.value; } } const cache = await caches.open(cacheName); await cache.put(cacheKey, new Response(JSON.stringify(allCookiesForOrigins))); } return new Response(response.body, { status: response.status, statusText: response.statusText, headers: headers }); } self.addEventListener('install', (event) => { event.waitUntil(self.skipWaiting()); }); self.addEventListener("activate", (event) => { event.waitUntil((async () => { await clients.claim(); if (self.registration.navigationPreload) { await self.registration.navigationPreload.disable(); } })()); }); self.addEventListener('fetch', (event) => { event.respondWith(wrapFetch(event.request)); }); })();