检测和编辑MP3链接

时间:2011-12-16 16:37:12

标签: javascript audio

我需要使用JavaScript检测页面中的MP3链接,并将文本替换为:

<embed type="application/x-shockwave-flash"
src="http://www.google.com/reader/ui/3247397568-audio-player.swf?
audioUrl=http://LINK" width="400" height="27" 
allowscriptaccess="never" quality="best" bgcolor="#ffffff" 
wmode="window" flashvars="playerMode=embedded" />

有MP3的URL值吗?

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

[].map.call( document.links, function(v){

    if( v.href.indexOf( ".mp3" ) === v.href.length - 4 ) {
        return '<embed type="application/x-shockwave-flash" ' +
            'src="http://www.google.com/reader/ui/3247397568-audio-player.swf?' +
            'audioUrl='+encodeURIComponent(v.href)+' width="400" height="27" ' +
            'allowscriptaccess="never" quality="best" bgcolor="#ffffff" ' +
            'wmode="window" flashvars="playerMode=embedded" />';
    }

    return "";

}).join("");

http://jsfiddle.net/e5DSU/2/

页面上的以下链接:

<a href="/asd/asd/asd.mp3"></a>
<a href="/asd/asd/asd.mp3"></a>
<a href="/asd/asd/asd.mp3"></a>

得到:

<embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http%3A%2F%2Ffiddle.jshell.net%2Fasd%2Fasd%2Fasd.mp3 width="400" height="27" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" />
<embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http%3A%2F%2Ffiddle.jshell.net%2Fasd%2Fasd%2Fasd.mp3 width="400" height="27" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" />
<embed type="application/x-shockwave-flash" src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=http%3A%2F%2Ffiddle.jshell.net%2Fasd%2Fasd%2Fasd.mp3 width="400" height="27" allowscriptaccess="never" quality="best" bgcolor="#ffffff" wmode="window" flashvars="playerMode=embedded" />

答案 1 :(得分:0)

http://jsfiddle.net/ZVzF6/

$('a').each(function(){
    var src=$(this).attr('href');
    var a = src.split('.');
    var ext = a[a.length-1];
    if (ext=='mp3'){

        var html = '<embed type="application/x-shockwave-flash"                                                                        
                    src="http://www.google.com/reader/ui/3247397568-audio-player.swf?audioUrl=' + encodeURIComponent(src) + '" 
                    width="400"
                    height="27" allowscriptaccess="never" quality="best" 
                    bgcolor="#ffffff" wmode="window" 
                    flashvars="playerMode=embedded" />';

        $(html).insertAfter($(this));
        $(this).remove();    
    }
});