css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

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.

            
              <body>
  <div class="container">
    <div id="header">
      <h3 class="headText">
        Who's on&nbsp;
      </h3>
      <a href="https://www.twitch.tv/" target="_blank">
        <!-- Twitch's logo in svg -->
        <svg class="svg-logo_twitch" role="img" version="1.1" viewBox="0 0 94 32" width="94px" x="0px" y="0px">
          <title>Twitch</title>
          <desc>Main Page</desc>
          <path clip-rule="evenodd" d="M88,5h-6V0h-9l-6,5h-5.5L59,7.5V5h-5V0H36v5H16l-5-5H0v22l5,5.25L14,32h6v-1.5l3,1.5h12l2-3l1,3h7v-3l3,3h8l0.5-3l2.5,3h10l3-3v3h4l3-3v3h7l7-6V10L88,5z M13,13H8v6h5v6H6l-4-4V2h6v5h5V13z M36,21.5L32.5,25H15V7h6v12h2V7h6v12h2V7h5V21.5z M44,25h-6V7h6V25z M44,5h-6V2h6V5z M57,13h-5v6h5v6h-7l-4-4V2h6v5h5V13z M72,13h-7v6h7v6h-9l-4-4V11l4-4h9V13z M91,25h-6V13h-5v12h-6V2h6v5h7l4,4V25z" fill-rule="evenodd"></path>
</svg>
      </a>
      <h3 class="headText">
              ?
      </h3>
    </div>

    <div id="searchBox" class="input-field">
      <form>
        <i class="material-icons prefix">search</i>
        <input id="search" type="text" autocomplete="off">
        <label for="search">Find and add channels</label>
      </form>
    </div>

    <!-- The following modal starts off hidden, and is opened on search     -->
    <div id="searchModal" class="modal">
      <div class="modal-content">
        <h5>Click to add channels to the list. Close this box to refresh.</h5>
        <div class="row">
          <div class="col s1"></div>
          <div class="col s2">(Channel name)</div>
          <div class="col s9">(Topic category)</div>
        </div>
        <div id="searchResults"></div>
      </div>
      <div class="modal-footer">
        <a href="#!" class="modal-action modal-close btn-flat">OK</a>
      </div>
    </div>

    <div id="chipList">
      <!-- container div for chips -->
    </div>
    <div>
      <a id="reset" class="waves-effect waves-light btn">reset list to default</a>
    </div>
  </div>
</body>
            
          
!
            
              body {
  background-color: #F1F1F1;
}

#header {
  margin-top: 10px;
}

.svg-logo_twitch {
  fill: #6441A5;
  display: inline-block;
  height: 2.5em;
}

.headText {
  display: inline-block;
}

h3 {
  color: #6441A5;
}

.input-field input[type=text]:focus + label {
  color: #6441A5;
}

.input-field input[type=text]:focus {
  border-bottom: 1px solid #6441A5;
  box-shadow: 0 1px 0 0 #6441A5;
}

.input-field .prefix {
 color: #262626;
}

.input-field .prefix.active {
 color: #6441A5;
}

.modal {
  background-color: #F1F1F1;
  color: #6441A5;
}

.container .row {
  margin-left: 0 !important;
}

.chip {
  background-color: #262626;
  color: #B9A3E3;
  width: 100%;
  box-shadow: 0 2px 5px 0 rgba(0, 0, 0, 0.16), 0 2px 10px 0 rgba(0, 0, 0, 0.12);
}

a {
  color: #B9A3E3
}

.offline {
  font-style: italic;
}

.btn {
  width: 100%;
}

.waves-effect {
  background-color: #6441A5;
}

.waves-effect:hover {
  background-color: #B9A3E3;
}
            
          
!
            
              // to do: refactor using fragments to load the lists

// first, check to see whether localStorage even exists, and alert if not. Next, check to see if it holds data from a previous visit, and use that, splitting it back into an array because it saves as a string. Finally, if localStorage.nameList is empty, declare nameList; we'll save it to localStorage later if changes are made to it.
if (!window.localStorage) {
  alert("Sorry, we can't store your data locally. Changes to your channel list will be lost when you exit.");
} else if (localStorage.nameList !== undefined) {
  var nameList = localStorage.nameList.split(",");
} else {
  var nameList = ["OgamingSC2", "FreeCodeCamp", "ESL_SC2", "ThatMadProgrammer", "codingrainbow", "Programming"];
}

// Twitch doesn't have one API that accepts bulk submission of channel names and returns all of the data we need. It does have one, below, that takes bulk names, but only returns data on the ones that are online. Great, now we know what's online!
function buildList() {
  $.ajax({
    url: "https://api.twitch.tv/kraken/streams/?channel=" + nameList.toString() + "&stream_type=all",
    type: "GET",
    dataType: "json",
    headers: {
      "Client-ID": "r0a4q2vzw4htcq5h9hl5sl6r4ykk6vw"
    },
    success: function(data) {
      $("#chipList").html(""); //clear out contents from previous call
      for (var i = 0; i < data.streams.length; i++) {
        $("#chipList").append("<div class='chip row hoverable'> <a href='" + data.streams[i].channel.url + "' target='_blank'> <div class='col s1 logo'> <img src='" + data.streams[i].channel.logo + "'> </div><div class='col s2 truncate'>" + data.streams[i].channel.display_name + "</div><div class='col s8 truncate'>Online: " + data.streams[i].channel.status + "</div></a><div class='col s1'> <i id='" + data.streams[i].channel.display_name + "' class='close material-icons'>close</i> </div></div>");
      }
      // For the others, we'll need an individual call for each channel to get their logo, etc. Pseudocode: iterate through nameList. If the entry is the name of a div generated by the previous for loop, then "continue". Otherwise run a new API call and build a new div.
      for (var i = 0; i < nameList.length; i++) {
        if ($('#chipList').find('#' + nameList[i]).length) { // A jquery way of saying, "If a div by that ID has already been created in the DOM..."
          continue;
        } else {
          $.ajax({
            url: "https://api.twitch.tv/kraken/channels/" + nameList[i],
            type: "GET",
            headers: {
              'Client-ID': 'r0a4q2vzw4htcq5h9hl5sl6r4ykk6vw'
            },
            dataType: "json",
            success: function(data) {
              $("#chipList").append("<div class='chip row hoverable'><a href='" + data.url + "' target='_blank'><div class='col s1 logo'> <img src='" + data.logo + "'> </div><div class='col s2 truncate'>" + data.display_name + "</div><div class='col s8 truncate offline'>Offline: " + data.status + "</div></a><div class='col s1'> <i id='" + data.display_name + "' class='close material-icons'>close</i></div></div>");
            },
            error: function(jqXHR, textStatus, errorThrown) {
              $("#chipList").append("<div class='chip row hoverable'><a href='https://www.twitch.tv/' target='_blank'><div class='col s1 logo'> <img src='//static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_150x150.png'></div><div class='col s2 truncate'>" + errorThrown + "</div><div class='col s8 truncate offline'>Error: " + jqXHR.responseJSON.message + "</div></a><div class='col s1'> <i id='" + nameList[i] + "' class='close material-icons'>close</i></div></div>");
            }
          });
        }
      }
    },
    error: function() {
      alert("Error, please try again");
    }

  });
}

// A function to search for channels:
$("#searchBox").submit(function(event) {
  event.preventDefault();
  $.ajax({
    url: "https://api.twitch.tv/kraken/search/channels?q=" + $("#search").val(),
    method: "GET",
    dataType: "json",
    headers: {
      "Client-ID": "r0a4q2vzw4htcq5h9hl5sl6r4ykk6vw"
    },
    success: function(data) {
      $("#searchResults").html(""); //clear out any results from previous searches
      for (var i = 0; i < data.channels.length; i++) {
        if (data.channels[i].logo === null) { //Some Twitch users never set a profile pic. Replace with the default one that Twitch uses.
          var logo = "//static-cdn.jtvnw.net/jtv_user_pictures/xarth/404_user_150x150.png";
        } else {
          logo = data.channels[i].logo;
        }
        if ((data.channels[i].game === null) || (data.channels[i].game === "")) {
          var game = "Topic not specified"; //Some users never set a "game", and at least one managed to set it to an empty string.
        } else {
          game = data.channels[i].game;
        }
        $("#searchResults").append("<div class='chip row hoverable result'><a href='" + data.channels[i].url + "' target='_blank'><div class='col s1 logo'><img src='" + logo + "'></div><div class='col s2 truncate'>" + data.channels[i].display_name + "</div><div class='col s8 truncate'>" + game + "</div></a><div class='col s1'> <i id='" + data.channels[i].display_name + "' class='close material-icons'>close</i></div></div>");
      }
      $('#searchModal').openModal({ //...open the modal...
        complete: buildList //... and, when you close it, run the list again
      });
    }
  });
})

// A function to update the nameList array when a search result is clicked:
$("#searchResults").on("click", ".result", function(event) {
  event.preventDefault(); //Don't open the link!
  $(this).hide();
  nameList.push(this.lastChild.lastChild.id); //You're clicking the chip (.result). The ID is on the <i>, inside the last <div>.
  localStorage.nameList = nameList;
});

// To remove IDs from nameList when someone clicks their close button:
$('#chipList').on('click', ".close", function() {
  nameList.splice(nameList.indexOf(this.id), 1);
  localStorage.nameList = nameList;
});

// To restore the starting contents of the list:
$("#reset").click(function() {
  nameList = ["OgamingSC2", "FreeCodeCamp", "ESL_SC2", "ThatMadProgrammer", "codingrainbow", "Programming"];
  localStorage.nameList = nameList;
  buildList();
});

$(document).ready(buildList); //This is where it all starts!
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console