Skip to content
Creeper edited this page Dec 15, 2016 · 2 revisions

Welcome to the id3-parser wiki!

How to use inside browser?

You can use browserify or webpack to make the lib work inside browser.

And then you can do something like below:

var promise = ajax('http://7sbnba.com1.z0.glb.clouddn.com/test-v1-v2.3.mp3')
// or fetch api
var promise = fetch('http://7sbnba.com1.z0.glb.clouddn.com/test-v1-v2.3.mp3')
    .then(res => res.arrayBuffer())


promise.then(res => {
    ID3.parse(new Uint8Array(res.target.response))
        .then(console.log.bind(console))
})

// The output:
// {
//     album: "E=Mc²",
//     artist: "Mariah Carey",
//     comment: "",
//     genre: ""
//     ...
// }


function ajax(options) {
    var deferred = Promise.defer();
    var type = typeof options;
    var request, url;
    if(!options || (type !== 'string' && type !== 'object')) {
        deferred.reject('invalid args');
    } else {
        request = new XMLHttpRequest();
        if(type === 'string') {
            url = options;
        } else {
            url = options.url;
        }
        request.open('GET', url, true);
        request.responseType = options.responseType || 'arraybuffer';
        request.onload = function(res) {
            deferred.resolve(res);
        };
        request.onerror = function(e) {
            deferred.reject(e);
        };
        request.send();
    }
    return deferred.promise;
}
Clone this wiki locally