sound link fix

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

目前為 2015-08-06 提交的版本,檢視 最新版本

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