2018-12-14 00:25:12 -08:00
|
|
|
|
2018-04-14 15:50:16 -07: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
|
2019-01-26 10:14:15 -08:00
|
|
|
// the build process and write it to checksum.js.
|
2018-12-08 11:21:54 -08:00
|
|
|
|
2019-05-25 08:19:11 -07:00
|
|
|
import { INLINE_THEME, DEFAULT_THEME, switchToTheme } from '../routes/_utils/themeEngine'
|
2019-01-26 10:14:15 -08:00
|
|
|
import { basename } from '../routes/_api/utils'
|
|
|
|
import { onUserIsLoggedOut } from '../routes/_actions/onUserIsLoggedOut'
|
2019-06-01 13:07:31 -07:00
|
|
|
import { storeLite } from '../routes/_store/storeLite'
|
2018-12-08 11:21:54 -08:00
|
|
|
|
2018-09-23 12:26:01 -07:00
|
|
|
window.__themeColors = process.env.THEME_COLORS
|
|
|
|
|
2019-06-01 13:07:31 -07:00
|
|
|
const {
|
|
|
|
currentInstance,
|
|
|
|
instanceThemes,
|
|
|
|
disableCustomScrollbars,
|
2019-06-19 23:00:27 -07:00
|
|
|
enableGrayscale,
|
|
|
|
pushSubscription,
|
|
|
|
loggedInInstancesInOrder
|
2019-06-01 13:07:31 -07:00
|
|
|
} = storeLite.get()
|
|
|
|
|
|
|
|
const theme = (instanceThemes && instanceThemes[currentInstance]) || DEFAULT_THEME
|
2018-12-15 17:13:34 -08:00
|
|
|
|
|
|
|
if (currentInstance) {
|
2019-08-03 12:02:06 -07:00
|
|
|
// Do preconnect if we're logged in, so we can connect faster to the other origin.
|
2019-08-03 13:49:37 -07:00
|
|
|
const link = document.createElement('link')
|
2019-08-03 12:02:06 -07:00
|
|
|
link.setAttribute('rel', 'preconnect')
|
|
|
|
link.setAttribute('href', basename(currentInstance))
|
2018-12-15 17:13:34 -08:00
|
|
|
link.setAttribute('crossorigin', 'anonymous')
|
|
|
|
document.head.appendChild(link)
|
|
|
|
}
|
2018-12-05 21:34:30 -08:00
|
|
|
|
2019-05-25 08:19:11 -07:00
|
|
|
if (theme !== INLINE_THEME) {
|
2018-12-15 17:13:34 -08:00
|
|
|
// switch theme ASAP to minimize flash of default theme
|
2019-06-01 13:07:31 -07:00
|
|
|
switchToTheme(theme, enableGrayscale)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (enableGrayscale) {
|
2019-06-01 15:51:46 -07:00
|
|
|
document.getElementById('theGrayscaleStyle')
|
|
|
|
.setAttribute('media', 'all') // enables the style
|
2018-04-14 15:50:16 -07:00
|
|
|
}
|
2018-09-23 12:26:01 -07:00
|
|
|
|
2019-06-01 13:07:31 -07:00
|
|
|
if (!currentInstance) {
|
2018-04-14 15:50:16 -07:00
|
|
|
// if not logged in, show all these 'hidden-from-ssr' elements
|
2018-12-16 12:39:16 -08:00
|
|
|
onUserIsLoggedOut()
|
2018-04-14 15:50:16 -07:00
|
|
|
}
|
2018-08-18 23:14:30 -07:00
|
|
|
|
2019-06-01 13:07:31 -07:00
|
|
|
if (disableCustomScrollbars) {
|
|
|
|
document.getElementById('theScrollbarStyle')
|
|
|
|
.setAttribute('media', 'only x') // disables the style
|
2018-11-27 19:17:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// hack to make the scrollbars rounded only on macOS
|
|
|
|
if (/mac/i.test(navigator.platform)) {
|
|
|
|
document.documentElement.style.setProperty('--scrollbar-border-radius', '50px')
|
|
|
|
}
|
|
|
|
|
2019-03-31 19:56:21 +02:00
|
|
|
// Versions of iOS Safari before iOS 12.2 do not work properly as a PWA
|
|
|
|
// for cross-origin authentication: https://github.com/nolanlawson/pinafore/issues/45
|
|
|
|
// Here we sniff for iOS <12.2 by checking for the existence of a native IntersectionObserver
|
|
|
|
// function, which was added in 12.2.
|
|
|
|
if (/iP(?:hone|ad|od)/.test(navigator.userAgent) &&
|
|
|
|
!(typeof IntersectionObserver === 'function' &&
|
|
|
|
IntersectionObserver.toString().includes('[native code]'))) {
|
2018-08-18 23:14:30 -07:00
|
|
|
document.head.removeChild(document.getElementById('theManifest'))
|
|
|
|
}
|
2019-06-19 23:00:27 -07:00
|
|
|
|
|
|
|
if (pushSubscription) {
|
|
|
|
// Fix a bug in Pinafore <=v1.9.0 if we only have one instance we're logged in to
|
|
|
|
// (https://github.com/nolanlawson/pinafore/issues/1274)
|
|
|
|
if (loggedInInstancesInOrder && loggedInInstancesInOrder.length === 1) {
|
|
|
|
storeLite.set({
|
|
|
|
pushSubscriptions: {
|
|
|
|
[currentInstance]: pushSubscription
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
storeLite.set({
|
|
|
|
pushSubscription: null
|
|
|
|
})
|
|
|
|
}
|