#Javascript#

对象赋值 obj.name 和 obj["name"] 的区别

两种方式语义是一样的,不过后一种属性的名称被看作字符串,可以更好的避免和 JS 里预留关键词冲突。

obj.for = "Simon"; // 语法错误,因为 for 是一个预留关键字
obj["for"] = "Simon"; // 工作正常

不过在定义属性时也最好不要用预留关键词!

获取虾米音乐下载链接

今天在博客园看到的,通过反编译播放器找到解码音乐资源的地址的算法。

算法这里就不多解释了,原文有分析,这里说一下获取“加密”地址的过程:

1.进入单首歌曲介绍页,比如 http://www.xiami.com/song/1768914936 ,地址后面的数字就是歌曲的 id
<!--more-->

2.根据 id 找到包含歌曲相关信息的 xml 文件地址,例如 http://www.xiami.com/song/playlist/id/1768914936/object_name/default/object_id/0

3.载入 xml ,获取 location 节点的内容,就是“加密”的地址

第二步里把最后的 0 换成其他数字会得到不同的加密地址,但是解码后资源真实地址还是一样的。

原文是用 java 实现的,这里改成了 javascript,使用起来更方便~

function loadXMLDoc(dname) 
{
try //Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  }
catch(e)
  {
  try //Firefox, Mozilla, Opera, etc.
    {
    xmlDoc=document.implementation.createDocument("","",null);
    }
  catch(e) {alert(e.message)}
  }
try 
  {
  xmlDoc.async=false;
  xmlDoc.load(dname);
  return(xmlDoc);
  }
catch(e) {alert(e.message)}
return(null);
}
//获取歌曲 id
var patt1=new RegExp("[0-9].*");
var songId=patt1.exec(location.href);
//载入 xml
var xmlDoc=loadXMLDoc("http://www.xiami.com/song/playlist/id/"+songId+"/object_name/default/object_id/0");
//解码地址
function getLocation(xlocation){
        var _local10;
        var _local2 = parseInt(xlocation.substring(0, 1));
        var _local3 = xlocation.substring(1, xlocation.length);
        var _local4 = Math.floor(_local3.length / _local2);
        var _local5 = _local3.length % _local2;
        var _local6 = new Array();
    _local6.length = _local2;
        var _local7 = 0;
        while (_local7 < _local5) {
            if (_local6[_local7] == null) {
                _local6[_local7] = "";
            }
            _local6[_local7] = _local3.substring((( _local4 + 1) * _local7),
                    (( _local4 + 1) * _local7) + ( _local4 + 1));
            _local7++;
        }
        _local7 = _local5;
        while (_local7 < _local2) {
            _local6[_local7] = _local3.substring((( _local4 * (_local7 - _local5)) + (( _local4 + 1) * _local5)),(( _local4 * (_local7 - _local5)) + ((_local4 + 1) * _local5))+ _local4);
            _local7++;
        }
        var _local8 = "";
        _local7 = 0;
        while (_local7 < ( _local6[0]).length) {
            _local10 = 0;
            while (_local10 = _local6[_local10].length) {
                    break;
                }
                _local8 = (_local8 + _local6[_local10].charAt(_local7));
                _local10++;
            }
            _local7++;
        }
        _local8 = decodeURIComponent(_local8);
        var _local9 = "";
        _local7 = 0;
        while (_local7 < _local8.length) {
            if (_local8.charAt(_local7) == '^'){
                _local9 = (_local9 + "0");
            } else {
                _local9 = (_local9 + _local8.charAt(_local7));
            };
            _local7++;
        }
        _local9 = _local9.replace("+", " ");
        alert( _local9);
    }
getLocation(xmlDoc.getElementsByTagName("location")[0].textContent);

实际上无论是否登录在虾米音乐网站上下载的都是低码率的音乐,如果要高码率的话只能用虾歌哦。