Greasy Fork 还支持 简体中文。

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.11
  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://greasyfork.org/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").bind('error' , function() { ErrorEvent("error" ); } );
  55. $("#NewAudioPlayer").bind('stalled' , function() { ErrorEvent("stalled"); } );
  56. $("#NewAudioPlayer").get(0).play();
  57. }
  58.  
  59. function ErrorEvent(evento) {
  60. // console.debug ("Error! (Event:" + evento + ")", divId );
  61. if (evento == "error") { evento = evento + " during the loading" }
  62. $("#NewAudioPlayer").parent().html("*** Error! (Event:" + evento + ") ***");
  63. }
  64.  
  65.  
  66. function WritePlayInfo(NAP) {
  67. durata = NAP.duration;
  68. // console.debug (NAP.src, durata);
  69. durata_min = parseInt(durata/60);
  70. durata_sec = parseInt(durata-(parseInt(durata/60)*60));
  71. durataf = durata_min + ":" + durata_sec;
  72. buttonId = $("#"+NAP.id).attr("buttonId");
  73. divId = "#div" + buttonId;
  74. // console.debug (buttonId, divId);
  75. urlplayed = $("#" + buttonId + " + a").attr("href");
  76. $ (divId).append("<br/><div class='infoSong'>url: " + decodeURI (urlplayed) + " - durata:" + durataf + "</div>");
  77. }
  78.  
  79. function PlayNext(NAP) {
  80. buttonId = $("#"+NAP.id).attr("buttonId");
  81. nId = parseInt( buttonId.substring (1,buttonId.length) );
  82. if (nId >= NrAudio-1) {
  83. console.debug ("Stop Playing"); return false
  84. }
  85. nId = nId+1;
  86. console.debug ("---- Play next. Song n.", nId+1, " of ", NrAudio);
  87. $("#B" + nId).click();
  88. }
  89.  
  90. function stopPlay() {
  91. DestroyPlayer();
  92. $("#" + this.id).html("Play")
  93. $("#" + this.id).click(startPlay)
  94. }