NPR.org HTML5 player

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

当前为 2016-03-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name NPR.org HTML5 player
  3. // @description Listen to NPR without having to install Flash, downloads, no ads.
  4. // @namespace https://greasyfork.org/users/4813-swyter
  5. // @match *://www.npr.org/player/v2/mediaPlayer.html*
  6. // @version 1
  7. // @grant none
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. // https://api.npr.org/query?id=466555217&format=json&apiKey=MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010
  12.  
  13. // http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=466555217&m=468149502
  14. // http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=468901493&m=468940337
  15. // http://www.npr.org/player/v2/mediaPlayer.html?action=1&t=1&islist=false&id=468933562&m=469337177&live=1
  16.  
  17. //id = location.search.slice(1).split('&').filter(function(obj){ return obj.contains('id') }).toString().split('=')[1];
  18.  
  19.  
  20. if (!(id = location.search.split('id=')[1].split('&')[0]))
  21. throw "Invalid identifier, it's not possible to guess what item we want.";
  22.  
  23. window.xhr = new XMLHttpRequest();
  24. xhr.open('GET', 'https://api.npr.org/query?id=' + id + '&format=json&apiKey=MDAzMzQ2MjAyMDEyMzk4MTU1MDg3ZmM3MQ010');
  25. xhr.responseType = 'json';
  26. xhr.onload = function(e)
  27. {
  28. console.log(this.response);
  29.  
  30. container = document.createElement("fieldset");
  31. selector = document.createElement("select");
  32. aplayer = document.createElement("audio");
  33. poster = document.createElement("img");
  34. flash_sucks = document.querySelector('#homepageFlash, body');
  35. flash_sucks.parentElement.replaceChild(container, flash_sucks);
  36. legend = document.createElement("legend");
  37. legend.textContent = this.response.list.story[0].title.$text;
  38. poster.src=this.response.list.story[0].image[0].src;
  39. aplayer.src=this.response.list.story[0].audio[0].format.mp3[0].$text;
  40. aplayer.controls=true;
  41. selector.size=10;
  42. audios=this.response.list.story[0].audio;
  43. for(var entry in audios)
  44. {
  45. console.log("=> ", audios[entry]);
  46. elem = document.createElement("option");
  47. elem.value = audios[entry].title.$text;
  48.  
  49. selector.add(new Option(audios[entry].title.$text, "LOL"));
  50. }
  51. container.appendChild(legend);
  52. container.appendChild(poster);
  53. container.appendChild(selector);
  54. container.appendChild(aplayer);
  55. };
  56.  
  57. xhr.send();