Skip to content

Instantly share code, notes, and snippets.

@kejun
Created January 12, 2011 10:29
Show Gist options
  • Save kejun/775988 to your computer and use it in GitHub Desktop.
Save kejun/775988 to your computer and use it in GitHub Desktop.
Douban音乐推荐直接用xiami播放
// ==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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment