JavaScript preprocessors can help make authoring JavaScript easier and more convenient. For instance, CoffeeScript can help prevent easy-to-make mistakes and offer a cleaner syntax and Babel can bring ECMAScript 6 features to browsers that only support ECMAScript 5.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
HTML Settings
Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.
<form class="search_bar" id="#form">
<input placeholder="Darude - Sandstorm" class="search_box" id="query" />
<button type="button" class="go" id="go">Search</button>
</form>
<audio src="" id="preview"></audio>
<div class="songs" id="songs"></div>
/*
To Do
Clean Up
*/
::-webkit-scrollbar {
width: 6px;
position: absolute;
top:2em;
}
::-webkit-scrollbar-track {
-webkit-box-shadow: inset 0 0 6px rgba(255,255,255,0.75);
border-radius: 10px;
}
::-webkit-scrollbar-thumb {
border-radius: 10px;
-webkit-box-shadow: inset 0 0 6px rgba(255,255,255,0.75);
}
body{
width:360px;
height:540px;
margin:0 auto;
background-color: rgb(32,32,32);
}
/*Search Bar items*/
.search_bar{
position:fixed;
z-index:1000;
}
.search_box{
width:287px;
}
.go{
width:69px;
float:right;
z-index:10;
}
.search_box, .go{
font-size:1.5em;
}
/*Song items*/
ul{
padding-left:0.2em;
}
li{
list-style-type: none;
}
.songs{
padding-top: 2em;
}
.song{
height:80px;
border-bottom-style: solid;
border-bottom-width:0.1em;
display: flex;
}
.cover{
width:80px;
height:100%;
}
.song_info{
margin: 0;
width: 58%;
}
.song_name{
font-size:1em;
}
.song_artist{
font-size: 1em;
}
.spotify{
background-color:rgb(32,32,32);
color:white;
}
.fa-spotify{
color:rgb(129,183,26);
font-size: 1em;
}
.btn{
font-size: 1em;
/*off until I finish testing*/
}
// Todo
// 1. Prettify
function init() {
//Main starts here
var results = document.getElementById("songs");
var query = document.getElementById("query");
var send = document.getElementById("go");
var spotifySearchUrl = "https://api.spotify.com/v1/search?query=$query&offset=0&limit=15&type=track"; //This is Spotify's search uri
var preview = document.getElementById("preview");
var currentlyPlaying = "";
send.addEventListener("click", submitSearch);
query.addEventListener("keyup", submitSearch);
function submitSearch() {
//This listener waits for the search key to be pressed and searches for the song
results.innerHTML = "";
var searchTerm = encodeURIComponent(query.value);
var search = spotifySearchUrl.replace(/\$query/, searchTerm);
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
var responseText = JSON.parse(xhttp.responseText);
renderSpotifySong(responseText);
}
};
xhttp.open("GET", search, true);
xhttp.send(null);
}
function addToDOM(provider, cover, songName, songArtist, trackURL, previewURL) {
//attached the song widget onto the div class songs
var div = document.createElement("div");
var innerDiv = '<div class="song $provider animated fadeInLeft"><img class="cover" src="$cover"><ul class="song_info"><li class="song_name">$songName</li><li class="song_artist">$songArtist</li><li class="provider"><a href="$trackURL" title="See on $provider" target="_blank"><i class="fa fa-$provider"></i></a></li></ul><div class="actions"><button type="button" class="btn addSong"><i class="fa fa-plus"></i></button><button type="button" class="btn preview play" id="$previewURL"><i class="fa fa-play"></i></button></div></div>';
innerDiv = innerDiv.replace(/\$provider/g, provider);
innerDiv = innerDiv.replace(/\$cover/, cover);
innerDiv = innerDiv.replace(/\$songName/, songName);
innerDiv = innerDiv.replace(/\$songArtist/, songArtist);
innerDiv = innerDiv.replace(/\$trackURL/, trackURL);
innerDiv = innerDiv.replace(/\$previewURL/, previewURL);
div.innerHTML = innerDiv;
var previewNode = div.getElementsByClassName("preview")[0];
previewNode.addEventListener("click", audioHandler);
results.appendChild(div);
}
function renderSpotifySong(response) {
var tracks = response.tracks.items;
for (var x = 0; x < tracks.length; x++) {
setTimeout(function (i) {
return function () {
var currentSong = tracks[i];
var service = "spotify";
var art = currentSong.album.images[0].url;
var song = currentSong.name;
var author = currentSong.artists[0].name;
var url = currentSong.external_urls.spotify;
var preview = currentSong.preview_url;
addToDOM(service, art, song, author, url, preview);
};
}(x), 200 * 0);
}
}
function audioHandler() {
if (currentlyPlaying === "") {
currentlyPlaying = this.id;
preview.setAttribute("src", currentlyPlaying);
preview.play();
}
else {
if(currentlyPlaying === this.id){
if(!preview.paused){
preview.pause();
}
else{
preview.play();
}
}
else{
currentlyPlaying = this.id;
preview.setAttribute("src", currentlyPlaying);
preview.play();}
}
}
}
window.onload = init;
Also see: Tab Triggers