Skip to content

Instantly share code, notes, and snippets.

@sbolel
Last active August 18, 2023 17:36
Show Gist options
  • Save sbolel/0fb34218daf93b427a2c7d294f4c9306 to your computer and use it in GitHub Desktop.
Save sbolel/0fb34218daf93b427a2c7d294f4c9306 to your computer and use it in GitHub Desktop.
Reload Slack unreads view whenever there are new unreads
;(function (d) {
if (!slackDebug?.activeTeamId) {
slackDebug?.enable()
}
const UPDATE_ACTION = 'ACTION_UPDATE_UNREAD_COUNTS'
const NEW_DNM_BTN_SEL = '.c-link--button.p-unreads_view__show_newer'
const NEW_BTN_SEL = '.c-button.c-button--primary.c-button--medium'
const HEADER_BTN_SEL =
'.c-button-unstyled.p-ia__view_header__button.p-ia__view_header__button--with_label'
const getUnreads = () => team?.redux?.getState?.()?.unreadCounts?.totalUnreads ?? 0
const isUnreads = () => location.pathname.match(/unread/i)
const getBtns = [NEW_BTN_SEL, HEADER_BTN_SEL, NEW_DNM_BTN_SEL].map(
(sel) => d.querySelector(sel) ?? []
)
const onMessage = ({ type }, payload) => {
if (!getUnreads() || !isUnreads() || type !== UPDATE_ACTION) {
return
}
console.debug('New unread messages! Refreshing...')
getBtns()
.flat()
?.forEach((btn) => {
if (btn?.innerHTML?.match(/new message/i)) {
btn?.click?.()
}
})
}
const subscribe = () => team.redux.subscribe(onMessage)
const subscription = subscribe
})(document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment