Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript atob returning 'String contains an invalid character'

Tags:

I have an AJAX call getting info out of the Github API. It is returned in base64 encoding but when i try to decode it I get the aforementioned error.

Has anyone run into this before and know what is going on? The info the server is returning runs fine through web based decoders. Maybe it get s mangled a bit in a variable? IDK :/

(function () {
    'use strict';

    var objectsList = [];
    var isParsing = 0;
    var insertArea = $('body');

    function makeAJAXCall(hash, cb) {
        $.ajaxSetup({
            Accept: 'Application/vnd.github.raw+json',
            dataType: 'jsonp'
        });

        $.ajax({
            url: hash,
            success: function (json) {
                if (cb) {
                    cb(json);
                }
            },
            error: function (error) {
                console.error(error);
                throw error;
            }
        });
    }

    function parseBlob(hash, cb) {
        makeAJAXCall(hash, function (returnedJSON) {  // no loop as only one entry
            if (cb) {
                cb(returnedJSON.data.content);
            }
        });
    }

    function addSVGToPage(SVGToAdd) {
        var entry, decodedEntry;
        makeAJAXCall(SVGToAdd, function (returnedJSON) {
            console.info(window.atob(returnedJSON.data.content));
        });
    }

    function parseTree(hash) {
        var i, entry;
        var tree = 'https://api.github.com/repos/myusername/SVG-Shapes/git/trees/' + hash;

        makeAJAXCall(tree, function (returnedJSON) {
            for (i = 0;  i < returnedJSON.data.tree.length; i += 1) {
                entry = returnedJSON.data.tree[i];

                if (entry.type === 'blob') {
                    if (entry.path.slice(-4) === '.svg') {     // we only want the svg images not the ignore file and README etc
                        addSVGToPage(entry.url);
                    }
                } else if (entry.type === 'tree') {
                    parseTree(entry.sha);
                }
            }
        });
    }

    $(document).ready(function () {
        parseTree('master');
    });
}());

Edit: below is a sample of what is returned from a console.info(returnedJSON.data.content);

PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCFET0NU WVBFIHN2ZyBQVUJMSUMgIi0vL1czQy8vRFREIFNWRyAxLjEvL0VOIiAiaHR0 cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRk Ij4KPH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2h0PSIyNCIgeD0iMCIgeT0iMCIgc3R5bGU9InN0cm9rZS13aWR0aDoy cHg7c3Ryb2tlOmdyZXk7Ii8+ICA8IS0tIEJhY2tpbmcgcmVjdC9mcmFtZSAt LT4KCgk8ZyBjbGlwLXBhdGg9InVybCgjbG9hZGNsaXApIj4KCQk8cmVjdCB4 PSItMTgiIHkgPSIyIiB3aWR0aD0iNTE2IiBoZWlnaHQ9IjIwIiBzdHlsZT0i c3Ryb2tlLXdpZHRoOjBweDtzdHJva2U6Z3JleTsiIGZpbGw9InVybCgjc3Ry aXBlZGJhY2spIj4KCQkJPGFuaW1hdGVUcmFuc2Zvcm0KCQkgICAgICAgIGF0 dHJpYnV0ZVR5cGU9IlhNTCIKCQkgICAgICAgIGF0dHJpYnV0ZU5hbWU9InRy YW5zZm9ybSIKCQkgICAgICAgIHR5cGU9InRyYW5zbGF0ZSIKCQkJCWZyb209 IjIwIiB0bz0iMCIKCQkJCWJlZ2luPSIwcyIgZHVyPSIwLjZzIgoJCQkJcmVw ZWF0Q291bnQ9ImluZGVmaW5pdGUiCgkJCQlhZGRpdGl2ZT0ic3VtIi8+CgkJ PC9yZWN0PgoJPC9nPgo8L3N2Zz4K
like image 577
Doug Miller Avatar asked Mar 20 '12 12:03

Doug Miller


2 Answers

According to MDN docs, you might need to escape and then decodeURIComponent to handle unicode:

function utf8_to_b64( str ) {
    return window.btoa(unescape(encodeURIComponent( str )));
}

function b64_to_utf8( str ) {
    return decodeURIComponent(escape(window.atob( str )));
}

// Usage:
utf8_to_b64('✓ à la mode'); // "4pyTIMOgIGxhIG1vZGU="
b64_to_utf8('4pyTIMOgIGxhIG1vZGU='); // "✓ à la mode"
like image 162
Linus Thiel Avatar answered Oct 24 '22 18:10

Linus Thiel


atob breaks due to the newlines in the response. Remove them to get your code to work:

function addSVGToPage(SVGToAdd) {
    var entry, decodedEntry;         // <-- What is this doing here? It's unused.
    makeAJAXCall(SVGToAdd, function (returnedJSON) {
        console.info(window.atob(returnedJSON.data.content.replace(/\s/g, '')));
        //                                                ^^^^^^^^^^^^^^^^^^^
    });
}
like image 25
Rob W Avatar answered Oct 24 '22 19:10

Rob W