Skip to content

Instantly share code, notes, and snippets.

@TristanWiley
Last active March 9, 2019 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TristanWiley/be83377f3294499a5ce5edefbf75331c to your computer and use it in GitHub Desktop.
Save TristanWiley/be83377f3294499a5ce5edefbf75331c to your computer and use it in GitHub Desktop.
Script that updates Slack status with what you're currently listening to on Google Play Music
let currentSongTitle = ""
let currentSongPart = 0
let songLength = ''
const token = ""
const target = document.querySelector('#player')
const observer = new WebKitMutationObserver(() => {
const currentlyPlayingTitle = document.getElementById('currently-playing-title')
if (!currentlyPlayingTitle) return
const newSongTitle = currentlyPlayingTitle.innerText
const songTime = toSeconds(document.getElementById('time_container_current').innerText)
songLength = toSeconds(document.getElementById('time_container_duration').innerText)
const newSongPart = Math.trunc((songTime/songLength) * 10)
if (songLength === 0) return
if (currentSongTitle !== newSongTitle) {
currentSongTitle = newSongTitle
currentSongPart = newSongPart
updateSlack()
} else if (currentSongPart !== newSongPart) {
currentSongPart = newSongPart
updateSlack()
}
})
observer.observe(target, { childList: true, characterData: true, subtree: true })
function updateSlack() {
const newSongArtist = document.getElementById('player-artist').innerText
const newSongLength = document.getElementById('time_container_duration').innerText
let musicParts = Array(10).fill(':mi:')
musicParts[currentSongPart] = ':bl:'
const encodedProfile = encodeURI(JSON.stringify({
"status_text": `:pl:${musicParts.join('')} ${newSongLength} - ${currentSongTitle} by ${newSongArtist}`,
"status_emoji": ":musical_note:",
}))
fetch(`https://slack.com/api/users.profile.set?profile=${encodedProfile}&token=${token}`)
.then(response => response.json())
.then(json => console.log("Updated song"))
}
function toSeconds(time) {
const parts = time.split(":");
const minutes = Number(parts[0]);
const seconds = Number(parts[1]);
return seconds + minutes * 60;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment