Inline Audio Player

Add to every link to an audio file (.mp3 .wav .ogg .m4a .mp4) on page a tiny button for play music with inline player. Use Html5 <audio> tag.

当前为 2015-02-26 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Inline Audio Player
  3. // @version 1.10
  4. // @description Add to every link to an audio file (.mp3 .wav .ogg .m4a .mp4) on page a tiny button for play music with inline player. Use Html5 <audio> tag.
  5. // @author Restpeace
  6. // @match *
  7. // @include *
  8. // @exclude http?://www.inoreader.com/*
  9. // @grant none
  10. // @require http://code.jquery.com/jquery-2.1.3.js
  11. // @namespace https://gf.qytechs.cn/users/8668
  12. // ==/UserScript==
  13.  
  14. $("body").before ("<style>" +
  15. ".buttonPlay {font-size: 11px; background-color: #fff0a0; font-family: Trebuchet MS; padding: 2px 5px; margin-right: 6px;}" +
  16. ".infoSong { margin: 4px; font-size: 11px; font-family: Trebuchet MS; font-style: italic;}" +
  17. "</style>");
  18.  
  19. var audio_links = $("a[href*='.mp3'], a[href*='.wav'], a[href*='.ogg'], a[href*='.m4a'], a[href*='.mp4']");
  20. var hasMp3 = audio_links.length > 0;
  21. var NrAudio = audio_links.length;
  22. // console.log("Inline Mp3 Player start.... N. page links: " + audio_links.length);
  23. if (hasMp3) {
  24. for (var i = 0; i < audio_links.length; i++) {
  25. $(audio_links[i]).before ("<button id='B"+i+"' class=\"buttonPlay\">Play</Button>");
  26. $("#B"+i).attr("formaction", audio_links[i].href);
  27. $("#B"+i).click (startPlay);
  28. }
  29. } //if hasMp3
  30.  
  31. function DestroyPlayer() {
  32. if ( $("#NewAudioPlayer").size() > 0) {
  33. var buttonId = $("#NewAudioPlayer").attr("buttonId");
  34. $("#"+buttonId).html("Play")
  35. $("#"+buttonId).css ("background-color","#fff0a0");
  36. $("#"+buttonId).click(startPlay)
  37. $("#NewAudioPlayer").parent().remove()
  38. }
  39. }
  40.  
  41. function startPlay() {
  42. if (!hasMp3) {return false}
  43. DestroyPlayer();
  44. $ ("#" + this.id + " + a").after ("<div id='div" + this.id + "'></div>");
  45. $ ("#div"+this.id).append("<audio id='NewAudioPlayer'></audio>");
  46. $("#" + this.id).html("Stop")
  47. $("#" + this.id).css ("background-color","#ffa0f0");
  48. $("#" + this.id).click(stopPlay)
  49. $("#NewAudioPlayer").attr("controls", "controls");
  50. $("#NewAudioPlayer").attr("src", $("#"+this.id).attr("formaction"));
  51. $("#NewAudioPlayer").attr("buttonId", this.id);
  52. $("#NewAudioPlayer").bind('durationchange', function() { WritePlayInfo(this); } );
  53. $("#NewAudioPlayer").bind('ended' , function() { PlayNext(this); } );
  54. $("#NewAudioPlayer").get(0).play();
  55. }
  56.  
  57. function WritePlayInfo(NAP) {
  58. durata = NAP.duration;
  59. // console.debug (NAP.src, durata);
  60. durata_min = parseInt(durata/60);
  61. durata_sec = parseInt(durata-(parseInt(durata/60)*60));
  62. durataf = durata_min + ":" + durata_sec;
  63. buttonId = $("#"+NAP.id).attr("buttonId");
  64. divId = "#div" + buttonId;
  65. // console.debug (buttonId, divId);
  66. urlplayed = $("#" + buttonId + " + a").attr("href");
  67. $ (divId).append("<br/><div class='infoSong'>url: " + decodeURI (urlplayed) + " - durata:" + durataf + "</div>");
  68. }
  69.  
  70. function PlayNext(NAP) {
  71. buttonId = $("#"+NAP.id).attr("buttonId");
  72. nId = parseInt( buttonId.substring (1,buttonId.length) );
  73. if (nId >= NrAudio-1) {
  74. console.debug ("Stop Playing"); return false
  75. }
  76. nId = nId+1;
  77. console.debug ("---- Play next. Song n.", nId+1, " of ", NrAudio);
  78. $("#B" + nId).click();
  79. }
  80.  
  81. function stopPlay() {
  82. DestroyPlayer();
  83. $("#" + this.id).html("Play")
  84. $("#" + this.id).click(startPlay)
  85. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址