-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer_inject.js
More file actions
61 lines (54 loc) · 1.77 KB
/
player_inject.js
File metadata and controls
61 lines (54 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
player_inject.js
- Injected into the active tab to load Spotify Web Playback SDK
- Creates a player and forwards 'player_state_changed' events to the extension via postMessage
- Also exposes a simple window.__loopify_playerReady flag
*/
if (!window.__loopify_loopifyInjected) {
window.__loopify_loopifyInjected = true;
const script = document.createElement('script');
script.src = 'https://sdk.scdn.co/spotify-player.js';
document.head.appendChild(script);
}
let initAttempted = false;
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.type === 'SET_TOKEN') {
// wait for Spotify SDK to be ready
waitForSpotify(msg.token);
}
});
function waitForSpotify(token) {
if (initAttempted) return;
initAttempted = true;
const interval = setInterval(() => {
if (window.Spotify) {
clearInterval(interval);
startPlayer(token);
}
}, 200);
}
function startPlayer(token) {
if (window.__loopify_player) return;
window.__loopify_player = new Spotify.Player({
name: 'Loopify Web Player',
getOAuthToken: cb => { cb(token); },
volume: 0.8
});
window.__loopify_player.addListener('ready', ({ device_id }) => {
window.__loopify_player_ready = true;
window.__loopify_device_id = device_id;
window.postMessage({ type: 'LOOPIFY_PLAYER_READY', device_id }, '*');
});
window.__loopify_player.addListener('player_state_changed', state => {
// forward minimal state to extension popup via window.postMessage
if (!state) return;
const s = {
paused: state.paused,
position: state.position,
duration: state.duration,
track_window: state.track_window
};
window.postMessage({ type: 'LOOPIFY_PLAYER_STATE', state: s }, '*');
});
window.__loopify_player.connect();
}