-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spotify.js
112 lines (97 loc) · 3.06 KB
/
spotify.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
/**
* @copyright 2023 Chris Zuber <admin@kernvalley.us>
*/
import { createPolicy } from './trust.js';
import { createIframe } from './elements.js';
const SPOTIFY = 'https://open.spotify.com/embed/';
const SANDBOX = ['allow-scripts', 'allow-popups', 'allow-same-origin'];
const ALLOW = ['encrypted-media'];
const TYPES = [
'album',
'artist',
'playlist',
'show',
'track',
];
export const policy = createPolicy('spotify#script-url', {
createScriptURL: input => {
if (input.startsWith(SPOTIFY)) {
return input;
} else {
throw new TypeError(`Not a Spotify embed URL: ${input}`);
}
}
});
export function parseURI(uri) {
if (typeof uri !== 'string') {
throw new Error('URI is not a string');
} else if (! uri.startsWith('spotify:')) {
throw new Error('URI is not a valid Spotify URI');
} else {
const [, type = '', id = ''] = uri.split(':');
if (! TYPES.includes(type)) {
throw new Error(`Unsupported type: ${type}`);
} else {
return { type, id };
}
}
}
export function linkToUri(link) {
if (typeof link === 'string' && link.startsWith('https:')) {
const url = new URL(link);
const [type = null, id = null] = url.pathname.substr(1).split('/');
if (
url.host.toLowerCase() === 'open.spotify.com'
&& TYPES.includes(type)
&& typeof id === 'string'
) {
return `spotify:${type}:${id}`;
} else {
return null;
}
} else {
return null;
}
}
export function uriToLink(uri) {
if (typeof uri === 'string' && uri.startsWith('spotify:')) {
const [, type = null, id = null] = uri.split(':');
if (TYPES.includes(type) && typeof id === 'string') {
const url = new URL(`${type}/${id}`, 'https://open.spotify.com');
return url.href;
} else {
return null;
}
} else {
return null;
}
}
function createSpotifyIframe(type, id, { title = 'Spotify Player', large = false, credentialless = false, slot, part } = {}) {
return createIframe(policy.createScriptURL(new URL(`./${encodeURIComponent(type)}/${encodeURIComponent(id)}`, SPOTIFY)), {
width: 300,
height: large ? 380 : 80,
referrerPolicy: 'origin',
credentialless,
slot,
part,
sandbox: SANDBOX,
allow: ALLOW,
title,
});
}
export function createSpotifyAlbum(id, { title, large, slot, part, credentialless } = {}) {
return createSpotifyIframe('album', id, { title, large, slot, part, credentialless });
}
export function createSpotifyArtist(id, { title, large, slot, part, credentialless } = {}) {
return createSpotifyIframe('artist', id, { title, large, slot, part, credentialless });
}
export function createSpotifyPlaylist(id, { title, large, slot, part, credentialless } = {}) {
return createSpotifyIframe('playlist', id, { title, large, slot, part, credentialless });
}
export function createSpotifyShow(id, { title, large, slot, part, credentialless } = {}) {
return createSpotifyIframe('show', id, { title, large, slot, part, credentialless });
}
export function createSpotifyTrack(id, { title, large, slot, part, credentialless } = {}) {
return createSpotifyIframe('track', id, { title, large, slot, part, credentialless });
}
export const trustPolicies = [policy.name];