Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. @kejun kejun renamed this gist Jan 12, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @kejun kejun created this gist Jan 12, 2011.
    53 changes: 53 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    // ==UserScript==
    // @name douban_xiami
    // @namespace org.kejun
    // @include http://music.douban.com/*
    // ==/UserScript==

    //使用说明:
    // 1. 安装本脚本到Greasemonkey
    // 2. 到http://music.douban.com/recommended
    // 3. 点击专辑封面会自动用虾米播放(如果窗口没弹出,检查是否被浏览器拦截)

    var api = 'http://www.xiami.com/ajax/search-index?key={QUERY}',

    getAlbumInfo = function(name, cb) {
    GM_xmlhttpRequest({
    method: 'GET',
    url: api.replace('{QUERY}', name),
    onload: function(xhr) {
    cb(xhr);
    }
    });
    },

    handleClick = function(e) {
    var el = e.target, cd_name;
    if (el.nodeType > 1) {
    return;
    }
    if (el.tagName.toLowerCase() !== 'img') {
    return;
    }
    cd_name = el.getAttribute('alt');
    if (!cd_name || cd_name.indexOf('-') === -1) {
    return;
    }
    e.preventDefault();
    e.stopPropagation();
    cd_name = encodeURIComponent(cd_name.replace(/\s|-/g, '+').replace(/[+]{2,}/g, '+'));
    getAlbumInfo(cd_name, function(xhr){
    if (xhr.responseText === '') {
    alert('虾米没有他们的歌');
    return;
    }
    var songid = xhr.responseText.match(/\"\/album\/(\d+)\"/);
    if (!songid[1]) {
    alert('虾米没有他们的专辑');
    return;
    }
    window.open('http://www.xiami.com/song/play?ids=/song/playlist/id/' + songid[1] + '/type/1','xm_player','width=760,height=560');
    });
    };

    document.body.addEventListener('click', handleClick, false);