pinafore/static/inline-script.js.map

1 line
6.1 KiB
Plaintext
Raw Normal View History

{"version":3,"file":"inline-script.js","sources":["src/routes/_utils/themeEngine.js","inline-script.js","src/routes/_utils/testStorage.js"],"sourcesContent":["let meta = process.browser && document.getElementById('theThemeColor')\nlet offlineStyle = process.browser && document.getElementById('theOfflineStyle')\n\nfunction getExistingThemeLink () {\n return document.head.querySelector('link[rel=stylesheet][href^=\"/theme-\"]')\n}\n\nfunction resetExistingTheme () {\n let existingLink = getExistingThemeLink()\n if (existingLink) {\n document.head.removeChild(existingLink)\n }\n}\n\nfunction loadCSS (href) {\n let existingLink = getExistingThemeLink()\n\n let link = document.createElement('link')\n link.rel = 'stylesheet'\n link.href = href\n\n link.addEventListener('load', function onload () {\n link.removeEventListener('load', onload)\n if (existingLink) { // remove after load to avoid flash of default theme\n document.head.removeChild(existingLink)\n }\n })\n\n // inserting before the offline <style> ensures that the offline style wins when offline\n document.head.insertBefore(link, offlineStyle)\n}\n\nexport function switchToTheme (themeName) {\n let themeColor = window.__themeColors[themeName]\n meta.content = themeColor || window.__themeColors['default']\n if (themeName !== 'default') {\n loadCSS(`/theme-${themeName}.css`)\n } else {\n resetExistingTheme()\n }\n}\n","// For perf reasons, this script is run inline to quickly set certain styles.\n// To allow CSP to work correctly, we also calculate a sha256 hash during\n// the build process and write it to inline-script-checksum.json.\n\nimport { testHasLocalStorageOnce } from './src/routes/_utils/testStorage'\nimport { switchToTheme } from './src/routes/_utils/themeEngine'\n\nwindow.__themeColors = process.env.THEME_COLORS\n\nconst hasLocalStorage = testHasLocalStorageOnce()\n\nif (hasLocalStorage && localStorage.store_currentInstance && localStorage.store_instanceThemes) {\n let safeParse = (str) => str === 'undefined' ? undefined : JSON.parse(str)\n let theme = safeParse(localStorage.store_instanceThemes)[safeParse(localStorage.store_currentInstance)]\n if (theme && theme !== 'default') {\n switchToTheme(theme)\n }\n}\n\nif (!hasLocalStorage || !localStorage.store_currentInstance) {\n // if not logged in, show all these 'hidden-from-ssr' elements\n let style = document.createElement('style')\n style.textContent = '.hidden-from-ssr { opacity: 1 !important; }'\n document.head.appendChild(style)\n}\n\nif (hasLocalStorage && localStorage.store_disableCustomScrollbars === 'true') {\n // if user has disabled custom scrollbars, remove this style\n let theScrollbarStyle = document.getElementById('theScrollbarStyle')\n theScrollbarStyle.setAttribute('media', 'only x') // disables the style\n}\n\n// hack to make the scrollbars rounded only on macOS\nif (/mac/i.test(navigator.platform)) {\n document.documentElement.style.setProperty('--scrollbar-border-radius', '50px')\n}\n\n// TODO: remove this hack when Safari works with cross-origin window.open()\n// in a PWA: https://github.com/nolanlawson/pinafore/issues/45\nif (/iP(?:hone|ad|od)/.test(navigator.userAgent)) {\n document.head.removeChild(document.getElementById('theManifest'))\n}\n","// LocalStorage and IDB may be disabled in private mode, when \"blocking cookies\" in Safari,\n// or other cases\n\nimport { thunk } from './thunk'\n\nconst testKey = '__test__'\n\nexport const testHasLocalStorageOnce = () => {\n try {\n localStorage.setItem(testKey, testKey)\n if (!localStorage.length || localStorage.getItem(testKey) !== testKey) {\n return false\n }\n localStorage.removeItem(testKey)\n } catch (e) {\n return false\n }\n return true\n}\n\nexport const testHasLocalStorage = thunk(testHasLocalStorageOnce)\n\nexport const testHasIndexedDB = thunk(async () => {\n if (typeof indexedDB === 'undefined') {\n return false\n }\n\n try {\n let idbFailed = await new Promise(resolve => {\n let db = indexedDB.open(testKey)\n db.onerror = () =