forked from muxinc/media-elements
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiframe-api.js
294 lines (249 loc) · 8.67 KB
/
iframe-api.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import { parseURI, URITypeMap } from '@spotify-internal/uri';
import {
EMBED_REQUIRED_IFRAME_ATTRIBUTES,
EMBED_DEFAULT_HEIGHT,
} from '../../src/utils/constants';
import { IframeAPIEvent, IframeCommands } from './types';
type EventHandler = (eventObject: {
data: Record<string, string | number | boolean | null>;
}) => void;
type EmbedTheme = 'colorExtraction' | 'dark';
type EmbedIframeOptions = {
width?: string;
height?: string;
uri?: string;
startAt?: string;
preferVideo?: boolean;
theme?: EmbedTheme;
};
type IframeCommandMessage = {
command: IframeCommands;
[key: string]: string | number;
};
type PlaybackState = {
isPaused: boolean;
isBuffering: boolean;
duration: number | null;
positionAsOfTimestamp: number | null;
};
// eslint-disable-next-line prefer-const
let SpotifyIframeApi: SpotifyIframeApiType;
class SpotifyEmbedController {
private _listeners: Record<IframeAPIEvent, Array<EventHandler>> = {
[IframeAPIEvent.READY]: [],
[IframeAPIEvent.PLAYBACK_UPDATE]: [],
};
private iframeElement: HTMLIFrameElement;
private host: string;
private options: EmbedIframeOptions;
private currentUri: string = '';
private loading: boolean = false;
private _commandQ: IframeCommandMessage[] = [];
constructor(targetElement: HTMLElement, options: EmbedIframeOptions) {
// eslint-disable-next-line local-rules/ssr-friendly/no-dom-globals-in-constructor
this.host = window.SpotifyIframeConfig?.host;
this.options = options;
// Make sure that the host has not been overridden to point to a
// spurious domain
const url = new URL(this.host);
if (
!url.hostname.endsWith('.spotify.com') &&
!url.hostname.endsWith('.spotify.net')
) {
throw new Error(`It appears that the hostname for the Spotify embed player has been overridden.
Please make sure that "SpotifyEmbedConfig" is not being overridden.`);
}
// eslint-disable-next-line local-rules/ssr-friendly/no-dom-globals-in-constructor
const iframeElement = document.createElement('iframe');
Object.entries(EMBED_REQUIRED_IFRAME_ATTRIBUTES).forEach(([attr, val]) => {
const htmlAttr = attr.toLowerCase();
if (typeof val === 'boolean') {
iframeElement.setAttribute(htmlAttr, '');
} else {
iframeElement.setAttribute(htmlAttr, val);
}
});
this.iframeElement = iframeElement;
const width = options.width ?? '100%';
const height = options.height ?? EMBED_DEFAULT_HEIGHT.toString();
this.setIframeDimensions(width, height);
targetElement.parentElement?.replaceChild(iframeElement, targetElement);
// eslint-disable-next-line local-rules/ssr-friendly/no-dom-globals-in-constructor
window.addEventListener('message', this.onWindowMessages);
}
setIframeDimensions = (width: string, height: string): void => {
// TODO: Should we check for min (max?) dimensions?
this.iframeElement.setAttribute('width', width);
this.iframeElement.setAttribute('height', height);
};
onWindowMessages = (e: MessageEvent): void => {
if (e.source === this.iframeElement.contentWindow) {
if (e.data?.type === IframeAPIEvent.READY) {
this.onPlayerReady();
}
if (e.data?.type === IframeAPIEvent.PLAYBACK_UPDATE) {
const playbackState = e.data?.payload;
this.onPlaybackUpdate(playbackState);
}
}
};
addListener = (
eventName: IframeAPIEvent,
handler: EventHandler,
): VoidFunction => {
if (!this._listeners[eventName]) {
this._listeners[eventName] = [];
}
this._listeners[eventName].push(handler);
return (): void => {
this.removeListener(eventName, handler);
};
};
removeListener = (eventName: IframeAPIEvent, handler: EventHandler): void => {
if (!this._listeners[eventName] || !this._listeners[eventName].length) {
this._listeners[eventName] = this._listeners[eventName].filter(
storedHandler => handler !== storedHandler,
);
}
};
emitEvent = (
eventName: IframeAPIEvent,
eventData: Record<string, string | number | boolean | null>,
): void => {
this._listeners[eventName]?.forEach(handler =>
handler({ data: eventData }),
);
};
onPlayerReady = (): void => {
this.loading = false;
this.flushCommandQ();
this.playerReadyAck();
this.emitEvent(IframeAPIEvent.READY, {});
};
onPlaybackUpdate = (playbackState: PlaybackState): void => {
this.emitEvent(IframeAPIEvent.PLAYBACK_UPDATE, playbackState);
};
loadUri = (uriString: string, preferVideo: boolean = false): void => {
if (uriString !== this.currentUri) {
const uri = parseURI(uriString);
if (
uri &&
(uri.type === URITypeMap.EPISODE ||
uri.type === URITypeMap.SHOW ||
uri.type === URITypeMap.ALBUM ||
uri.type === URITypeMap.ARTIST ||
uri.type === URITypeMap.PLAYLIST_V2 ||
uri.type === URITypeMap.TRACK)
) {
this.loading = true;
const type =
uri.type === URITypeMap.PLAYLIST_V2 ? 'playlist' : uri.type;
const embedURL = new URL(`${this.host}/embed/${type}/${uri.id}`);
if (this.options.startAt) {
const startAt = parseInt(this.options.startAt, 10);
if (!isNaN(startAt))
embedURL.searchParams.append('t', startAt.toString());
}
if (this.options.theme === 'dark') {
embedURL.searchParams.append('theme', '0');
}
this.iframeElement.src = embedURL.href;
if (
(uri.type === URITypeMap.EPISODE || uri.type === URITypeMap.SHOW) &&
preferVideo
) {
SpotifyIframeApi.supportsVideo(uriString).then(isVideoContent => {
if (isVideoContent) {
embedURL.pathname += '/video';
this.iframeElement.src = embedURL.href;
}
});
}
} else {
throw new Error(`${uriString} cannot be embedded.`);
}
}
};
// We use this message to instrument the loading of content using the iframe API
playerReadyAck = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.LOAD_COMPLETE_ACK });
};
play = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.PLAY });
};
playFromStart = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.PLAY_FROM_START });
};
pause = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.PAUSE });
};
resume = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.RESUME });
};
togglePlay = (): void => {
this.sendMessageToEmbed({ command: IframeCommands.TOGGLE_PLAY });
};
seek = (timestampInSeconds: number): void => {
this.sendMessageToEmbed({
command: IframeCommands.SEEK,
timestamp: timestampInSeconds,
});
};
sendMessageToEmbed = (messageToSend: IframeCommandMessage): void => {
if (this.loading) {
this._commandQ.push(messageToSend);
return;
}
if (this.iframeElement.contentWindow) {
this.iframeElement.contentWindow.postMessage(messageToSend, this.host);
} else {
// eslint-disable-next-line no-console
console.error(`Spotify Embed: Failed to send message ${messageToSend}.
Most likely this is because the iframe containing the embed player
has not finished loading yet.`);
}
};
flushCommandQ = (): void => {
if (this._commandQ.length) {
this._commandQ.forEach(command => {
setTimeout(() => {
this.sendMessageToEmbed(command);
}, 0);
});
this._commandQ = [];
}
};
destroy = (): void => {
this.iframeElement.parentElement?.removeChild(this.iframeElement);
window.removeEventListener('message', this.onWindowMessages);
};
}
SpotifyIframeApi = {
createController: (targetElement, options = {}, callback): void => {
const apiInstance = new SpotifyEmbedController(targetElement, options);
if (options.uri) {
apiInstance.loadUri(options.uri, options.preferVideo);
}
callback(apiInstance);
},
supportsVideo: async (uri: string): Promise<boolean> => {
const host = window.SpotifyIframeConfig?.host;
const response = await fetch(
`${host}/oembed?url=${encodeURIComponent(uri)}`,
{
method: 'GET',
},
);
const data = await response.json();
return data.type === 'video';
},
};
export default SpotifyIframeApi;
// eslint-disable-next-line local-rules/ssr-friendly/no-dom-globals-in-module-scope
if (!window.onSpotifyIframeApiReady) {
// eslint-disable-next-line no-console
console.warn(`SpotifyIframeApi: "onSpotifyIframeApiReady" has not been defined.
Please read the docs to see why you are seeing this warning.`);
} else {
window.onSpotifyIframeApiReady(SpotifyIframeApi);
}