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.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html>
<head>
<link href='https://fonts.googleapis.com/css?family=Oxygen:400,700,300' rel='stylesheet' type='text/css'>
<link href='https://cdn.jsdelivr.net/animatecss/3.2.0/animate.min.css' rel='stylesheet' type='text/css'>
</head>
<title>Twitter Albums Streams - Powered by PubNub</title>
<body>
<div id="output"></div>
<div id="tweet"></div>
<a id="brand" href="https://pubnub.com"><img src="https://brandfolder.com/pubnub/assets/xemvalf8" alt="Powered By PubNub Color"></a>
<script src="https://cdn.pubnub.com/pubnub.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script src="https://cdn.embed.ly/jquery.embedly-3.1.1.min.js" type="text/javascript"></script>
</body>
</html>
* {
margin: 0px;
padding: 0px;
font-family: 'Oxygen', sans-serif;
color: #fff;
}
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
body {
background-color: #000;
overflow: hidden;
}
.info {
text-align: left;
}
.album {
opacity: .9;
float: left;
width: 20%;
margin-bottom: -3px;
}
.album img {
width: 100%;
}
#output {
width: 100%;
height: 100%;
}
#tweet {
display: none;
position: fixed;
left: 0px;
width: 100px;
z-index: 2;
text-align: left;
width: 80%;
color: #fff;
font-size: 24px;
text-shadow: 0px 0px 10px #000;
bottom: 0px;
background-color: #111;
opacity: .5;
padding: 20px 0px;
padding-right: 15%;
padding-left: 5%;
}
.album:hover {
opacity: 1;
}
.album:hover .tweet-show {
display: block;
}
#brand {
position: fixed;
z-index: 3;
right: 10px;
bottom: 10px;
width: 100px;
background-color: white;
border-radius: 3px;
padding: 5px;
opacity: .5;
}
#brand:hover {
opacity: 1;
}
#brand img {
max-width: 100%;
}
// store jQuery output containers
var $output = $('#output');
var $tweet = $('#tweet');
// configure # of albums to display
var limit = 20;
// initialize PubNub
var pubnub = PUBNUB.init({
subscribe_key: 'sub-c-78806dd4-42a6-11e4-aed8-02ee2ddab7fe'
});
// retrieve the last link provided in a tweet
var findLink = function(tweet) {
console.log(tweet);
if(tweet.entities.hasOwnProperty('urls')) {
var links = tweet.entities.urls;
var link = false;
for(var i = 0; i < links.length; i++) {
if (
links[i].expanded_url.indexOf('spoti.fi') > 0 ||
links[i].expanded_url.indexOf('spotify.com') > 0
) {
link = links[i].expanded_url;
}
}
return link;
} else {
return false;
}
};
// create a hashtable for Album objects
var albums = {};
// this object represents an album Album and tweet
var Album = function(tweet, link) {
var self = this;
self.render = function(image) {
var $html = $('\
<a class="album" href="' + link + '" target="_blank"> \
<img src="' + image + '" class="animated flipInY"> \
</a>');
var tweet_html = '<a href="//twitter.com/' + tweet.user.screen_name + '/status/' + tweet.id + '" ">' + tweet.text + '</a>';
$html.hover(function(){
$tweet.html(tweet_html).show();
}, function(){
$tweet.hide();
});
var num_albums = $('.album');
if(num_albums.length < limit) {
// we haven't populated the page yet, append
$output.append($html);
} else {
// take a random album element and replace it with this one
var randomel = Math.floor((Math.random() * num_albums.length ) + 1);
var $replace = $('.album:nth-child(' + randomel + ')');
$replace.addClass('animated flipOutY').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', function() {
$(this).replaceWith($html);
});
}
};
};
// this is a function turns an array of tweets into Album objects
var handleTweets = function(tweets) {
// create an array of links from provided tweets
var links = [];
for(var i = 0; i < tweets.length; i++) {
var link = findLink(tweets[i]);
if(link) {
links.push(link);
// maps the link to a Album object
albums[link] = new Album(tweets[i], link);
}
}
// pass the array of links to embedly where album information is retrieved
$.embedly.extract(links, {key: 'e8214d5cec7844388e11c4413689986b'}).progress(function(data){
var image = data.images[0];
if(typeof image !== 'undefined' && (image.width / image.height == 1)) {
// use the link to render the Album
albums[data.original_url].render(image.url);
}
});
};
// populate the page with the last x tweets
pubnub.history({
channel: 'pubnub-twitter-spotify',
count: limit * 1.5,
callback: function(data){
handleTweets(data[0]);
},
});
// render tweets in real time
pubnub.subscribe({
channel: 'pubnub-twitter-spotify',
message: function(tweet) {
handleTweets([tweet]);
}
});
Also see: Tab Triggers