Skip to content

Instantly share code, notes, and snippets.

@hon2a
Last active December 5, 2018 09:30
Show Gist options
  • Save hon2a/0679f72bd990fc72cc91046455f736e6 to your computer and use it in GitHub Desktop.
Save hon2a/0679f72bd990fc72cc91046455f736e6 to your computer and use it in GitHub Desktop.
Switch Slack to dark mode at night
// append this to /Applications/Slack.app/Contents/Resources/app.asar.unpacked/src/static/ssb-interop.js
document.addEventListener('DOMContentLoaded', async function() {
// supply your location here, use e.g. https://www.latlong.net/ to find it
const LOCATION = [50.075539, 14.437800]
const MS_IN_DAY = 24 * 60 * 60 * 1000
const initTheme = themeCssUrl => new Promise(resolve => $.ajax({
url: themeCssUrl,
success: css => {
const styleJqueryEl = $('<style>').html(css).appendTo('head')
const styleElement = styleJqueryEl[0]
styleElement.disabled = true
resolve(styleElement)
}
}))
const loadTimeInfo = ([latitude, longitude]) => new Promise(resolve => $.ajax({
// courtesy of https://sunrise-sunset.org/api
url: `https://api.sunrise-sunset.org/json?lat=${latitude}&lng=${longitude}&formatted=0`,
success: ({ results: { sunrise, sunset } }) => resolve({
sunrise: Number(new Date(sunrise)),
sunset: Number(new Date(sunset)),
expires: Math.ceil(Date.now() / MS_IN_DAY) * MS_IN_DAY
})
}))
const updateTheme = (styleElement, timeInfo) => {
const now = Date.now()
const { sunrise, sunset } = timeInfo
styleElement.disabled = now >= sunrise && now < sunset
}
const darkModeStyle = await initTheme('https://raw.githubusercontent.com/mattiacantalu/Slack-Dark-Mode/master/dark-mode.css')
let timeInfo = await loadTimeInfo(LOCATION)
updateTheme(darkModeStyle, timeInfo)
// can't simply `setTimeout` to the next update time - if the app is sleeping at that time, the call seems to be lost
window.setInterval(async () => {
if (Date.now() > timeInfo.expires) {
timeInfo = await loadTimeInfo(LOCATION)
}
updateTheme(darkModeStyle, timeInfo)
}, 5 * 60 * 1000)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment