Last active
January 20, 2023 09:53
-
-
Save chadlavi/eba2e3420fb9c1a6a891f8ed2c29e2ae to your computer and use it in GitHub Desktop.
a js script to embed spotify playlists or albums when a spotify share url is present
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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