wav link fix

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

当前为 2015-08-03 提交的版本,查看 最新版本

// ==UserScript==
// @name        wav 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();
  // if(invisible controls){
  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);
  /* } else {
  var node = document.getElementById('dynamicAudioControl');
  if(node) node.parentNode.removeChild(node);
  node = event.currentTarget;
  var wrp = document.createElement('div'), ref = node.getAttribute('href'), type = ref.slice(-3);
  switch(type){ case 'mp3': type = 'mpeg'; break; }
  var pos = event.clientX-180; if(pos<0) pos = 0;
  pos = 'left:' + pos + 'px;top:' + (window.pageYOffset+event.clientY-18) + 'px;';
  wrp.setAttribute('style', pos+'position:absolute!important;padding:0px;border:1px solid gray;');
  wrp.innerHTML='<audio controls autoplay style="float:left;"><source src="' + ref + '" type="audio/' + type + '"></audio>';
  wrp.setAttribute('id', 'dynamicAudioControl');
  var btn = document.createElement('input');
  btn.type = 'button';
  btn.value = 'x';
  btn.setAttribute('style', 'display:inline;padding:0px;height:auto;vertical-align:top;')
  btn.onclick = function() { wrp.parentNode.removeChild(wrp); };
  wrp.appendChild(btn);
  document.body.appendChild(wrp);
  } */
  return false;
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址