deviantART thumb generator

pretty dhumb right now.

by Camilo

HTML

<h2>Give me a dA/fav.me/sta.sh link</h2>
<input id="the-scripts-pussy"/>
<h2>Give it to me good</h2>
<label><input id="add-sizes" type="checkbox" checked="true" />Add width and height to <code>&lt;a&gt;</code></label>
<pre id="code">reults will appear here</pre><img id="copy" title="copy to clipboard" src="https://cdn1.iconfinder.com/data/icons/hawcons/32/699302-icon-32-clipboard-edit-24.png"/>
<div id="sample">sample will be shown here</div>

CSS

body {
    font: 16px Consolas, monospace;
    color:#117DFD;
}

#code {
    width: 80%;
    margin: 16px 0 0 10%;
    border: 2px solid #DDD;
    overflow: hidden;
    display: inline-block;
}

#sample {
    margin: 8px;
    text-align: center;
}

#copy {
    cursor: pointer;
}

JavaScript

$(function() {
    var last = '';
    $('#the-scripts-pussy, #add-sizes').on('keyup change', function() {
        var link=$('#the-scripts-pussy').val();
        var re=/^(https?:\/\/)(fav\.me\/|.*\.deviantart\.com\/|sta\.sh\/).*/i;
        var expect = link + ($('#add-sizes').is(':checked') ? '+' : '-');
        if (expect != last && link.match(re)) {
            last = expect;
            $('#code').text('just waito a momento puriizu~');
            var enc = encodeURIComponent(link);
            var url1 = "http://backend.deviantart.com/oembed?url=";
            var url2 = "&format=jsonp&callback=?";
            $.getJSON(url1 + enc + url2, $.proxy(function(json) {
            	var thumb = json.thumbnail_url_200h;
                var styles = '';
                if ($('#add-sizes').is(':checked')) {
                    styles = ' style="width:' + json.thumbnail_width_200h;
                    styles += 'px;height:' + json.thumbnail_height_200h + 'px"';
                }
                var code = '<a href="' + link + '"' + styles + '>'
                code += '<img src="' + thumb + '"/></a>';
                $('#code').text(code);
                $('#sample').html(code);
            }));
        }
    });
    $('#copy').click(function() {
        selectElementText($('#code')[0]);
    	document.execCommand("copy");
    });
});

//http://stackoverflow.com/a/2838358/124119
function selectElementText(el, win) {
    win = win || window;
    var doc = win.document, sel, range;
    if (win.getSelection && doc.createRange) {
        sel = win.getSelection();
        range = doc.createRange();
        range.selectNodeContents(el);
        sel.removeAllRanges();
        sel.addRange(range);
    } else if (doc.body.createTextRange) {
        range = doc.body.createTextRange();
        range.moveToElementText(el);
        range.select();
    }
}