Skip to content

Instantly share code, notes, and snippets.

@csinchok
Last active August 29, 2015 13:56
Show Gist options
  • Save csinchok/9254216 to your computer and use it in GitHub Desktop.
Save csinchok/9254216 to your computer and use it in GitHub Desktop.
Just a hack that goes into the teaser field on a video
<script type="text/javascript">
var primaryId = "1960";
var alternateIds = ["1647", "1648", "1649", "1642", "1637", "1629"];
function chooseVideo(){
var watched = localStorage.getItem("mj_video_variants");
if (watched === null) {
watched = [];
} else {
watched = watched.split(",");
}
// If the primary video has been seen, or on 20% of new plays, show an alternate.
if (watched.indexOf(primaryId) !== -1 || Math.random() < 0.20) {
var unWatched = [];
for (var index in alternateIds) {
if (watched.indexOf(alternateIds[index]) === -1) {
unWatched.push(alternateIds[index]);
}
}
if (unWatched.length === 0) {
watched = alternateIds;
watched.push(primaryId);
unWatched = watched;
}
var randomIndex = Math.floor(Math.random() * unWatched.length);
videoId = unWatched[randomIndex];
var srcHTML = '<source src="http://v.theonion.com/onionmedia/videos/videometa/' + videoId + '/zen_mp4.mp4" type="video/mp4"><source src="http://v.theonion.com/onionmedia/videos/videometa/' + videoId + '/zen_webm.webm" type="video/webm">';
var videoEl = document.getElementById(primaryId);
videoEl.innerHTML = srcHTML;
watched.push(videoId);
} else {
watched = [primaryId];
}
localStorage.setItem("mj_video_variants", watched.join(","));
}
if (typeof videoId === 'undefined') {
var videoId = null;
chooseVideo();
}
</script>
@awentzonline
Copy link

Couple notes, I might be wrong:

  • line 23: do you need a watched = []; here?
  • line 25: i think it's just Math.floor(Math.random() * unWatched.length)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment