Skip to content
Snippets Groups Projects
Select Git revision
  • de186e04c947aff342cd3bd3642a0d6f237897cd
  • master default protected
  • 63-upgrade-framework-and-dependencies
  • 45-improve-app-metadata
  • Release_0.9.0_57 protected
  • Release_0.8.2_56 protected
  • Release_0.8.1_55 protected
  • Release_0.8.0_54 protected
  • Release_0.7.0_53 protected
  • Release_0.6.0_52 protected
  • Release_0.5.0_51 protected
  • Release_0.4.2_50 protected
  • Release_0.4.1_49 protected
  • Release_0.4.0_48 protected
  • Release_0.3.1_47 protected
  • Release_0.3.0_46 protected
  • Release_0.2.2_45 protected
  • Release_0.2.1_44 protected
  • Release_0.2.0_43 protected
  • Release_0.1.2_42 protected
  • Release_0.1.1_41 protected
  • Release_0.1.0_40 protected
  • Release_0.0.39_39 protected
  • Release_0.0.38_38 protected
24 results

main.dart

Blame
  • now-playing.js 1.79 KiB
    function updateNowPlaying() {
        var apiUrl = "now-playing?api";
    
        var newContent = fetch(apiUrl)
            .then(function (response) {
                return response.json();
            })
            .then(function (data) {
                var nowPlayingBlock = document.getElementById("now-playing");
                newContent = buildNowPlayingBlock(data);
                nowPlayingBlock.innerHTML = newContent;
            })
            .catch(function (err) {
                console.warn("Something went wrong.", err);
                newContent =
                    '<div class="alert alert-warning">Error fetching API data.<br/>You may need to logout and refresh page.</div>';
                var nowPlayingBlock = document.getElementById("now-playing");
                nowPlayingBlock.innerHTML = newContent;
            });
    }
    
    function buildNowPlayingBlock(apiResponse) {
        var html = `
      <div class="container-fluid now-playing">
        <div class="row">
          <div class="col">
            <div class="now-playing-cover"><img src="COVER_URL" class="img-fluid"></div>
          </div>
          <div class="col">
            <div class="now-playing-title">TITLE</div>
            <div class="now-playing-artists">ARTISTS</div>
          </div>
        </div>
      </div>`;
    
        var nowPlayingData = apiResponse["playing"];
    
        if (!nowPlayingData) {
            return '<div class="alert alert-info">No currently playing track...</div>';
        }
    
        var item = nowPlayingData["item"];
        var album = item["album"];
        var artists = album["artists"];
    
        var artistNames = artists.map((artist) => artist["name"]).join(", ");
        var trackName = item["name"];
        var coverUrl = album["images"][0]["url"];
    
        html = html.replace("TITLE", trackName);
        html = html.replace("ARTISTS", artistNames);
        html = html.replace("COVER_URL",  coverUrl);
    
        return html;
    }
    
    setInterval(updateNowPlaying, 2000);