2018-12-14 09:25:12 +01:00
|
|
|
|
2018-04-15 00:50:16 +02:00
|
|
|
// For perf reasons, this script is run inline to quickly set certain styles.
|
|
|
|
// To allow CSP to work correctly, we also calculate a sha256 hash during
|
|
|
|
// the build process and write it to inline-script-checksum.json.
|
2018-12-08 20:21:54 +01:00
|
|
|
|
2018-12-11 16:31:48 +01:00
|
|
|
import { testHasLocalStorageOnce } from './src/routes/_utils/testStorage'
|
|
|
|
import { switchToTheme } from './src/routes/_utils/themeEngine'
|
2018-12-16 02:13:34 +01:00
|
|
|
import { basename } from './src/routes/_api/utils'
|
2018-12-16 21:39:16 +01:00
|
|
|
import { onUserIsLoggedOut } from './src/routes/_actions/onUserIsLoggedOut'
|
2018-12-08 20:21:54 +01:00
|
|
|
|
2018-09-23 21:26:01 +02:00
|
|
|
window.__themeColors = process.env.THEME_COLORS
|
|
|
|
|
2018-12-16 02:13:34 +01:00
|
|
|
const safeParse = str => (typeof str === 'undefined' || str === 'undefined') ? undefined : JSON.parse(str)
|
2018-12-08 20:21:54 +01:00
|
|
|
const hasLocalStorage = testHasLocalStorageOnce()
|
2018-12-16 02:13:34 +01:00
|
|
|
const currentInstance = hasLocalStorage && safeParse(localStorage.store_currentInstance)
|
|
|
|
|
|
|
|
if (currentInstance) {
|
|
|
|
// Do prefetch if we're logged in, so we can connect faster to the other origin.
|
|
|
|
// Note that /api/v1/instance is basically the only URL that doesn't require credentials,
|
|
|
|
// which is why we can do this. Also we do end up calling this on loading the home page,
|
|
|
|
// so it's not a wasted request.
|
|
|
|
let link = document.createElement('link')
|
|
|
|
link.setAttribute('rel', 'prefetch')
|
|
|
|
link.setAttribute('href', `${basename(currentInstance)}/api/v1/instance`)
|
|
|
|
link.setAttribute('crossorigin', 'anonymous')
|
|
|
|
document.head.appendChild(link)
|
|
|
|
}
|
2018-12-06 06:34:30 +01:00
|
|
|
|
2018-12-14 09:25:12 +01:00
|
|
|
if (currentInstance && localStorage.store_instanceThemes) {
|
2018-12-16 02:13:34 +01:00
|
|
|
// switch theme ASAP to minimize flash of default theme
|
2018-04-15 00:50:16 +02:00
|
|
|
let theme = safeParse(localStorage.store_instanceThemes)[safeParse(localStorage.store_currentInstance)]
|
2018-04-21 09:33:42 +02:00
|
|
|
if (theme && theme !== 'default') {
|
2018-12-08 20:21:54 +01:00
|
|
|
switchToTheme(theme)
|
2018-04-15 00:50:16 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 21:26:01 +02:00
|
|
|
|
2018-12-14 09:25:12 +01:00
|
|
|
if (!hasLocalStorage || !currentInstance) {
|
2018-04-15 00:50:16 +02:00
|
|
|
// if not logged in, show all these 'hidden-from-ssr' elements
|
2018-12-16 21:39:16 +01:00
|
|
|
onUserIsLoggedOut()
|
2018-04-15 00:50:16 +02:00
|
|
|
}
|
2018-08-19 08:14:30 +02:00
|
|
|
|
2018-12-06 06:34:30 +01:00
|
|
|
if (hasLocalStorage && localStorage.store_disableCustomScrollbars === 'true') {
|
2018-11-28 04:17:18 +01:00
|
|
|
// if user has disabled custom scrollbars, remove this style
|
|
|
|
let theScrollbarStyle = document.getElementById('theScrollbarStyle')
|
|
|
|
theScrollbarStyle.setAttribute('media', 'only x') // disables the style
|
|
|
|
}
|
|
|
|
|
|
|
|
// hack to make the scrollbars rounded only on macOS
|
|
|
|
if (/mac/i.test(navigator.platform)) {
|
|
|
|
document.documentElement.style.setProperty('--scrollbar-border-radius', '50px')
|
|
|
|
}
|
|
|
|
|
2018-08-19 08:14:30 +02:00
|
|
|
// TODO: remove this hack when Safari works with cross-origin window.open()
|
|
|
|
// in a PWA: https://github.com/nolanlawson/pinafore/issues/45
|
|
|
|
if (/iP(?:hone|ad|od)/.test(navigator.userAgent)) {
|
|
|
|
document.head.removeChild(document.getElementById('theManifest'))
|
|
|
|
}
|