sound link fix

Prevents the default action for linked .wav/.mp3 files. Inplace playback using HTML5 instead.

目前为 2015-08-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name sound link fix
  3. // @namespace gnblizz
  4. // @description Prevents the default action for linked .wav/.mp3 files. Inplace playback using HTML5 instead.
  5. // @include *
  6. // @version 1
  7. // @grant none
  8. // @icon http://japanese.about.com/library/weekly/graphics/speaker.gif
  9. // ==/UserScript==
  10. // sample page http://japanese.about.com/od/japanesevocabulary/a/expression.htm
  11. var i = document.links.length;
  12. if(i) do {
  13. if(/\.(wav|mp3)$/i.test(document.links[--i].toString()))
  14. document.links[i].addEventListener('click', WavFixOnClickHandler);
  15. } while(i);
  16. function WavFixOnClickHandler(event) {
  17. event.preventDefault();
  18. var node = event.currentTarget, wrp = document.createElement('div'), ref = node.getAttribute('href'), type = ref.slice(-3);
  19. switch(type){ case 'mp3': type = 'mpeg'; break; }
  20. wrp.setAttribute('style', 'position:absolute;visibility:hidden;');
  21. wrp.innerHTML = '<audio controls autoplay><source src="' + ref + '" type="audio/' + type + '"></audio>';
  22. wrp.firstChild.onended = function() { wrp.parentNode.removeChild(wrp); };
  23. document.body.appendChild(wrp);
  24. return false;
  25. }