sound link fix

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

  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.02
  7. // @grant none
  8. // @icon data:image/gif;base64,R0lGODlhEAAQAKECAAAAAAD//////////yH5BAEKAAIALAAAAAAQABAAAAItlI+pi+GcXIAGCEmBxXfpiz2IZoHSQ5LHKaXfegpu2Uyyq9jjS1V0z+vJhIkCADs=
  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. var link = document.links[--i];
  14. if(/\.(mp3|ogg|wav)$/i.test(link.href))
  15. link.addEventListener('click', WavFixOnClickHandler);
  16. } while(i);
  17. function WavFixOnClickHandler(event) {
  18. function CleanUp() { wrp.parentNode.removeChild(wrp); wrp = ctrl = null; };
  19. event.preventDefault();
  20. var node = event.currentTarget, wrp = document.createElement('span'), ref = node.getAttribute('href');
  21. wrp.setAttribute('style', 'position:relative;');
  22. wrp.innerHTML = '<audio controls autoplay><source src="' + ref + '" type="audio/' + ref.slice(-3).replace('mp3','mpeg') + '"></audio>';
  23. var ctrl = wrp.firstChild;
  24. ctrl.style.display = 'none';
  25. ctrl.onended = CleanUp;
  26. ctrl.lastChild.onerror = function() { alert('There was a technical problem.\n\nPlease try an option from the\ncontext menu of this link instead'); CleanUp(); };
  27. ctrl.onloadedmetadata = function(event) {
  28. if(ctrl.duration > 15) {
  29. var btn = document.createElement('BUTTON');
  30. btn.type = 'button';
  31. btn.textContent = 'stop this noise';
  32. btn.style = 'position:absolute;top:-8px;left:8px';
  33. wrp.appendChild(btn);
  34. btn.onclick = function() { ctrl.pause(); CleanUp(); };
  35. }
  36. }
  37. node.parentNode.insertBefore(wrp, node);
  38. return false;
  39. }
  40. // public domain by gnblizz
  41. // contact me with my username + '@web.de'