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-24 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Inline Audio Player
  3. // @version 1.0
  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. // @grant none
  9. // @require http://code.jquery.com/jquery-2.1.3.js
  10. // @namespace https://greasyfork.org/users/8668
  11. // ==/UserScript==
  12.  
  13. var audio_links = $("a[href*='.mp3'], a[href*='.wav'], a[href*='.ogg'], a[href*='.m4a'], a[href*='.mp4']");
  14. var hasMp3 = audio_links.length > 0;
  15. // console.log("Inline Mp3 Player start.... N. page links: " + audio_links.length);
  16. if (hasMp3) {
  17. for (var i = 0; i < audio_links.length; i++) {
  18. $(audio_links[i]).before ("<button id='B"+i+"'>Play</Button>");
  19. $("#B"+i).css("fontSize", "11px");
  20. $("#B"+i).css("fontFamily", "Trebuchet MS");
  21. $("#B"+i).css("padding", "2px 5px");
  22. $("#B"+i).css("marginRight", "6px");
  23. $("#B"+i).attr("formaction", audio_links[i].href);
  24. $("#B"+i).click (startPlay);
  25. }
  26. } //if hasMp3
  27.  
  28. function DestroyPlayer() {
  29. if ( $("#NewAudioPlayer").size() > 0) {
  30. var buttonSelId = $("#NewAudioPlayer").attr("buttonSelId");
  31. $(buttonSelId).html("Play")
  32. $(buttonSelId).click(startPlay)
  33. $("#NewAudioPlayer").parent().remove()
  34. }
  35. }
  36.  
  37. function startPlay() {
  38. if (!hasMp3) {return false}
  39. DestroyPlayer();
  40. $ ("#" + this.id + " + a").after ("<div id='div" + this.id + "'></div>");
  41. $ ("#div"+this.id).append("<audio id='NewAudioPlayer'></audio>");
  42. $("#" + this.id).html("Stop")
  43. $("#" + this.id).click(stopPlay)
  44. $("#NewAudioPlayer").attr("controls", "controls");
  45. $("#NewAudioPlayer").attr("src", $("#"+this.id).attr("formaction"));
  46. $("#NewAudioPlayer").attr("buttonSelId", "#" + this.id);
  47. $("#NewAudioPlayer").get(0).play();
  48. }
  49.  
  50. function stopPlay() {
  51. DestroyPlayer();
  52. $("#" + this.id).html("Play")
  53. $("#" + this.id).click(startPlay)
  54. }