NPR.org HTML5 player

Listen to NPR without having to install Flash, downloads, no ads.

2016-03-08 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

You will need to install an extension such as Tampermonkey to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name        NPR.org HTML5 player
// @description Listen to NPR without having to install Flash, downloads, no ads.
// @namespace   https://greatest.deepsurf.us/users/4813-swyter
// @match       *://www.npr.org/player/v2/mediaPlayer.html*
// @version     2016.03.08
// @grant       GM_addStyle
// @run-at      document-start
// ==/UserScript==

// https://api.npr.org/query?id=466555217&format=json&apiKey=MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010

// http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=466555217&m=468149502
// http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=468901493&m=468940337
// http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=468933562&m=469337177&live=1
// http://www.npr.org/player/v2/mediaPlayer.html?action=2&t=1&islist=false&id=469027281&m=469383445

if (!(id = location.search.split('id=')[1].split('&')[0]))
  throw "Invalid identifier, it's not possible to guess what item we want.";

window.xhr = new XMLHttpRequest();
xhr.open('GET', 'https://api.npr.org/query?id=' + id + '&format=json&apiKey=MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010');
xhr.responseType = 'json';
xhr.onload = function(e)
{
  console.log(this.response);

  container = document.createElement("fieldset");
  selector = document.createElement("select");
  aplayer = document.createElement("audio");

  flash_sucks = document.querySelector('#homepageFlash, body');
  flash_sucks.parentElement.replaceChild(container, flash_sucks);

  legend = document.createElement("legend");
  legend.textContent = this.response.list.story[0].title.$text;

  
  container.style.backgroundImage = "url(" + this.response.list.story[0].image[0].src + ")";
  aplayer.src=this.response.list.story[0].audio[0].format.mp3[0].$text;
  aplayer.controls=true;

  selector.size=10;

  audios=this.response.list.story[0].audio;

  for(var entry in audios)
  {
    console.log("=> ", audios[entry]);

    elem = document.createElement("option");
    elem.value = audios[entry].title.$text;
    
    len = audios[entry].duration.$text;
    
    len = (len / 60|0) + ":" + ('00' + (len % 60 | 0)).substr(-2);
    

    selector.add(new Option((entry|0 + 1) + ". " + audios[entry].title.$text + " — " + len, entry));
  }

  container.appendChild(legend);

  container.appendChild(selector);
  container.appendChild(aplayer);
  
  document.addEventListener('change', function(e)
  {
    index = e.explicitOriginalTarget.value;
    
    console.log("(*)", e, xhr.response.list.story[0].audio[index].format.mp3[0].$text);

    aplayer.src = xhr.response.list.story[0].audio[index].format.mp3[0].$text;
    aplayer.play();
  });
};

xhr.send();

GM_addStyle("audio, select {display:block; width:100%;} \
                  fieldset {padding-left:240px; background-size: 232px auto; background-repeat: no-repeat; background-position: 8px 14px; margin: 10px;} \
                    legend {font-size: medium; padding: 5px;} \
                         * {font-family: 'Gotham SSm',Helvetica,Arial,sans-serif !important;}");

/* will this generated stylesheet used in the main site last? who knows */
document.querySelector("link[rel=stylesheet]").href = 'https://s.npr.org/templates/css/generated/fingerprint/persistent-73cb243d716b971f095c14e7dfdb3432d2ecb623.css';