Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a js script to embed spotify playlists or albums when a spotify share url is present
// ==UserScript==
// @name spotify embedder
// @version 1
// @namespace https://gist.github.com/chadlavi/eba2e3420fb9c1a6a891f8ed2c29e2ae
// @downloadURL https://gist.github.com/chadlavi/eba2e3420fb9c1a6a891f8ed2c29e2ae/raw/spotify-embed.user.js
// @updateURL https://gist.github.com/chadlavi/eba2e3420fb9c1a6a891f8ed2c29e2ae/raw/spotify-embed.user.js
// @description embed spotify player when a url is present
// @author Chad Lavimoniere
// @exclude http*://*youtube.com*
// @exclude http*://*facebook.com*
// @exclude http*://*twitter.com*
// @exclude http*://mltshp.com*
// @exclude http*://news.ycombinator.com*
// @include http*://*.*
// @grant none
// ==/UserScript==
// playlist link pattern:
// https://open.spotify.com/user/[user]/playlist/[id]
// album link pattern:
// https://open.spotify.com/album/[id]
// embed pattern:
//<iframe src="https://open.spotify.com/embed?uri=spotify:[album/playlist]:[id]" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>
(function() {
'use strict';
function create(htmlStr) {
var frag = document.createDocumentFragment(),
temp = document.createElement('div')
temp.innerHTML = htmlStr
while (temp.firstChild) {
frag.appendChild(temp.firstChild)
}
return frag;
}
function spotifyPlaylistEmbed(link) {
var url = link.href.replace(/\?.*/, '')
var playlistId = url.replace(/https?:\/\/open\.spotify\.com\/user\/[^\/]*\/playlist\//, '')
var embed = create('\<br>\
<iframe src="https://open.spotify.com/embed?uri=spotify:playlist:' + playlistId + '" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>\
<br>')
link.parentNode.insertBefore(embed, link)
}
function spotifyAlbumEmbed(link) {
var url = link.href.replace(/\?.*/, '')
var albumId = url.replace(/https?:\/\/open\.spotify\.com\/album\//, '')
var embed = create('\<br>\
<iframe src="https://open.spotify.com/embed?uri=spotify:album:' + albumId + '" width="300" height="380" frameborder="0" allowtransparency="true" allow="encrypted-media"></iframe>\
<br>')
link.parentNode.insertBefore(embed, link)
}
var links = document.links
for (var i=0; i<links.length; i++){
var link = links[i]
if (link.href.match(/https?:\/\/open\.spotify\.com\/user\/[^\/]*\/playlist/g)){
spotifyPlaylistEmbed(link)
}
if (link.href.match(/https?:\/\/open\.spotify\.com\/album/g)){
spotifyAlbumEmbed(link)
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.