Last active
March 9, 2019 22:44
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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