Skip to content

Commit

Permalink
feat: save connection_id value in store
Browse files Browse the repository at this point in the history
  • Loading branch information
cdrani committed May 15, 2024
1 parent d0da40a commit ed15c7c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/actions/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ async function load() {
sessionStorage.setItem('device_id', device_id)
})

document.addEventListener('app.connection_id', async e => {
const { connection_id } = e.detail
sessionStorage.setItem('connection_id', connection_id)
})

document.addEventListener('app.auth_token', async e => {
const { auth_token } = e.detail
sessionStorage.setItem('auth_token', auth_token)
Expand Down
7 changes: 5 additions & 2 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function updateBadgeState({ changes, changedKey }) {
chrome.storage.onChanged.addListener(async changes => {
const keys = Object.keys(changes)
const changedKey = keys.find(key => (
['now-playing', 'enabled', 'auth_token', 'device_id'].includes(key)
['now-playing', 'enabled', 'auth_token', 'device_id', 'connection_id'].includes(key)
))

if (!changedKey) return
Expand All @@ -109,7 +109,10 @@ chrome.webRequest.onBeforeRequest.addListener(details => {

const text = new TextDecoder('utf-8').decode(new Uint8Array(rawBody))
const data = JSON.parse(text)
chrome.storage.local.set({ device_id: data.device.device_id.toString() })
chrome.storage.local.set({
device_id: data.device.device_id.toString(),
connection_id: data.connection_id.toString()
})
},
{ urls: ['https://guc3-spclient.spotify.com/track-playback/v1/devices'] },
['requestBody']
Expand Down
2 changes: 1 addition & 1 deletion src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ window.addEventListener('message', async (event) => {

chrome.runtime.onMessage.addListener(message => {
const messageKey = Object.keys(message)
const changedKey = messageKey.find(key => ['enabled', 'auth_token', 'device_id'].includes(key))
const changedKey = messageKey.find(key => ['connection_id', 'enabled', 'auth_token', 'device_id'].includes(key))

if (!changedKey) return

Expand Down
4 changes: 3 additions & 1 deletion src/stores/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Dispatcher from '../events/dispatcher.js'
import { currentSongInfo } from '../utils/song.js'
import { playback } from '../utils/playback.js'

const DO_NOT_INCLUDE = ['reverb', 'now-playing', 'device_id', 'auth_token', 'enabled', 'globals', 'chorus-seek']
const DO_NOT_INCLUDE = ['connection_id', 'reverb', 'now-playing', 'device_id', 'auth_token', 'enabled', 'globals', 'chorus-seek']

class DataStore {
#cache
Expand Down Expand Up @@ -35,6 +35,8 @@ class DataStore {
Object.keys(response).forEach(key => {
const value = response[key]

// TODO: parse the values instead. If not parseable it will not have
// and not require a isSkipped value
if (!DO_NOT_INCLUDE.includes(key) && !value.hasOwnProperty('isSkipped')) {
value.isSkipped = value?.endTime == 0
}
Expand Down

0 comments on commit ed15c7c

Please sign in to comment.