Skip to content

Instantly share code, notes, and snippets.

@tranch
Last active August 29, 2015 14:09
Show Gist options
  • Select an option

  • Save tranch/f3740b1d79dcf27a6cbf to your computer and use it in GitHub Desktop.

Select an option

Save tranch/f3740b1d79dcf27a6cbf to your computer and use it in GitHub Desktop.
虾米音乐下载 Grease Monkey 插件。
// ==UserScript==
// @name 虾米音乐下载
// @namespace tranch
// @description 替换下载按钮的链接
// @include http://www.xiami.com/album/*
// @include http://www.xiami.com/song/*
// @version 0.06
// @grant none
// ==/UserScript==
(function (d, l) {
var buttons, button, i, attr_val, attr_key='onclick';
if (l.href.contains('/song/')) {
buttons = d.querySelectorAll('li.do_download a');
} else if (l.href.contains('/album/')) {
buttons = d.querySelectorAll('a.song_download');
}
for (i = 0; button = buttons[i]; i++) {
if (attr_val = button.getAttribute(attr_key)) {
if (attr_val.contains('xm_download')) {
button.removeAttribute(attr_key);
button.setAttribute('data-song_id', attr_val.replace(/[^\d]/g, ''));
button.addEventListener('click', function() {
$.getJSON('http://songs.sinaapp.com/apiv3.php?id=' + this.getAttribute('data-song_id') + '&callback=?', function(data) {
l.href = data.location;
});
});
}
}
}
})(document, location);
@lizheming
Copy link

(function (d, l) {
    var buttons, attr_key='onclick';

    if (l.href.contains('/song/')) {
        buttons = 'li.do_download a';
    } else if (l.href.contains('/album/')) {
        buttons = 'a.song_download';
    }

    document.body.on("click", buttons, function(e) {
        var button = this, 
            attr_val = button.getAttribute(attr_key),
            song_id = attr_val.replate(/[^\d]/g, '');
        if( !attr_val.contains("xm_download") ) return;
        button.setAttribute("data-song_id", song_id);
        button.removeAttribute(attr_key);
        $.getJSON("http://songs.sinaapp.com/apiv3.php?callback=?&id="+song_id, function(data) { l.href = data.location });
    });
})(document, location);

(HTMLElement || Element).prototype.on = function(events, selector, fn) {
    var parent = this;
    function run( e ) {
        var target = e.target, 
            parents = [].slice.call( parent.querySelectorAll(selector) );
        do {
            if( parents.indexOf( target ) > -1 ) fn.apply(target, arguments);
        } while( target = target.parentNode );
        return false;
    }
    if( this.addEventListener ) {
        this.addEventListener( events, run, false );
    } else this.attachEvent( "on"+events, run );
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment