<commit_before>(function(xhr) {
    var open = XMLHttpRequest.prototype.open;
    xhr.prototype.open = function() {
        open.apply(this, arguments);
        this.setRequestHeader('X-Requested-With','XMLHttpRequest');
    };
})(XMLHttpRequest);

var history = [];
var historyEnabled = true;
function loadHistory() {
    historyEnabled = readCookie('hist-opt-out') === null;
    history = JSON.parse(window.localStorage.getItem('history'));
    if (!history) {
        history = [];
    }
}
function saveHistory() {
    window.localStorage.setItem('history', JSON.stringify(history));
}
function addItemToHistory(hash) {
    if (!historyEnabled)
        return;
    for (var i = 0; i < history.length; i++) {
        if (history[i] === hash)
            return;
    }
    history.push(hash);
    saveHistory();
}
function clearHistory() {
    if (!window.localStorage)
        return;
    history = [];
    window.localStorage.setItem('history', JSON.stringify(history));
}
function removeItemFromHistory(hash) {
    for (var i = 0; i < history.length; i++) {
        if (history[i] == hash) {
            history.remove(i);
            break;
        }
    }
    saveHistory();
}
function loadDetailedHistory(items, callback) {
    if (items.length == 0) callback([]);
    var hashes = items.join(',');
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/api/info?list=' + hashes);
    xhr.onload = function() {
        if (xhr.status != 200) {
            var itemsElement = document.getElementById('items');
            itemsElement.innerHTML = 'There was an error showing your history, sorry.';
            return;
        }
        var result = JSON.parse(this.response);
        if (callback) callback(result);
    };
    xhr.send();
}

var uploads = 0;
function browse() {
    var file = document.getElementById('browse');
    file.click();
}

function handleFiles(files) {
    var area = document.getElementById('files');
    area.className = '';
    var blurb = document.getElementById('blurb');
    blurb.className = 'hidden';
    var timeout = 500;
    for (var i = 0; i < files.length; i++) {
        uploads++;
        if (i == 0)
            handleFile(files[i]);
        else {
            void function(i) {
                setTimeout(function() {
                    handleFile(files[i]);
                }, timeout);
            }(i);
            timeout += 500;
        }
    }
}

function handleFile(file) {
    var reader = new FileReader();
    reader.onloadend = function(e) {
        var data = e.target.result;
        var hash = btoa(rstr_md5(data)).substr(0, 12).replace('+', '-').replace('/', '_');
        var preview = createPreview(file);
        if (!preview.supported) {
            var error = document.createElement('span');
            error.className = 'error';
            error.textContent = 'This filetype is not supported.';
            preview.fileStatus.appendChild(error);
        } else {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/api/' + hash + '/exists');
            xhr.onload = function() {
                if (this.status == 200) {
                    var p = document.createElement('h3');
                    p.textContent = 'Upload complete!';
                    var p2 = document.createElement('p');
                    p2.className = 'small';
                    p.textContent = 'Tap here to view';
                    var a = document.createElement('a');
                    a.setAttribute('target', '_blank');
                    a.href = '/' + hash;
                    a.className = 'full-size';
                    preview.fileStatus.appendChild(a);
                    preview.fileStatus.appendChild(p);
                    preview.fileStatus.appendChild(p2);
                    uploads--;
                    addItemToHistory(hash);
                } else {
                    var p = document.createElement('h3');
                    p.textContent = 'Uploading...';
                    preview.fileStatus.appendChild(p);
                    uploadFile(file, hash, preview.fileStatus, preview.progress);
                }
            };
            xhr.send();
        }
    };
    reader.readAsBinaryString(file);
}

function uploadFile(file, hash, statusUI, progressUI) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/api/upload/file');
    xhr.upload.onprogress = function(e) {
        if (e.lengthComputable) {
            progressUI.style.width = (e.loaded / e.total) * 100 + '%';
        }
    };
    xhr.onload = function() {
        var error = null;
        var responseJSON = JSON.parse(this.responseText);

        if (this.status == 415) {
            error = 'This media format is not supported.';
        } else if (this.status == 409) {
            finish(statusUI, responseJSON['hash']);
        } else if (this.status == 420) {
            error = 'You have consumed your hourly quota. Try again later.';
        } else if (this.status == 200) {
            statusUI.innerHTML = '';
            var p = document.createElement('h3');
            p.textContent = 'Processing...';
            statusUI.appendChild(p);
            hash = responseJSON['hash'];
            progressUI.className += ' progress-green';
            progressUI.style.width = '100%';
            setTimeout(function() {
                checkStatus(hash, statusUI, progressUI);
            }, 1000);
        }
        if (error) {
            progressUI.parentElement.removeChild(progressUI);
            var errorText = document.createElement('p');
            errorText.className = 'error';
            errorText.textContent = error;
            statusUI.appendChild(errorText);
            progressUI.style.width = 0;
        }
    };
    var formData = new FormData();
    formData.append('file', file);
    xhr.send(formData);
}

function checkStatus(hash, statusUI, progressUI) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/api/' + hash + '/status');
    xhr.onload = function() {
        responseJSON = JSON.parse(this.responseText);
        if (responseJSON['status'] == 'ready') {
            progressUI.parentElement.removeChild(progressUI);
            finish(statusUI, hash);
        } else if (responseJSON['status'] == 'timeout' || responseJSON['status'] == 'error') {
            progressUI.parentElement.removeChild(progressUI);
            var error = document.createElement('p');
            error.className = 'error';
            statusUI.innerHTML = '';
            if (responseJSON['status'] == 'timeout') {
                error.textContent = 'This file took too long to process.';
            } else {
                error.textContent = 'There was an error processing this file.';
            }
            statusUI.appendChild(error);
            uploads--;
        } else {
            setTimeout(function() {
                checkStatus(hash, statusUI, progressUI);
            }, 1000);
        }
    };
    xhr.send();
}

function finish(statusUI, hash) {
    var p = document.createElement('h3');
    p.textContent = 'Upload complete! ';
    var a2 = document.createElement('a');
    a2.setAttribute('target', '_blank');
    a2.href = '/' + hash;
    a2.className = 'full-size';
    statusUI.innerHTML = '';<commit_msg>Add 'done' as possible status on mobile JS<commit_after>(function(xhr) {
    var open = XMLHttpRequest.prototype.open;
    xhr.prototype.open = function() {
        open.apply(this, arguments);
        this.setRequestHeader('X-Requested-With','XMLHttpRequest');
    };
})(XMLHttpRequest);

var history = [];
var historyEnabled = true;
function loadHistory() {
    historyEnabled = readCookie('hist-opt-out') === null;
    history = JSON.parse(window.localStorage.getItem('history'));
    if (!history) {
        history = [];
    }
}
function saveHistory() {
    window.localStorage.setItem('history', JSON.stringify(history));
}
function addItemToHistory(hash) {
    if (!historyEnabled)
        return;
    for (var i = 0; i < history.length; i++) {
        if (history[i] === hash)
            return;
    }
    history.push(hash);
    saveHistory();
}
function clearHistory() {
    if (!window.localStorage)
        return;
    history = [];
    window.localStorage.setItem('history', JSON.stringify(history));
}
function removeItemFromHistory(hash) {
    for (var i = 0; i < history.length; i++) {
        if (history[i] == hash) {
            history.remove(i);
            break;
        }
    }
    saveHistory();
}
function loadDetailedHistory(items, callback) {
    if (items.length == 0) callback([]);
    var hashes = items.join(',');
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/api/info?list=' + hashes);
    xhr.onload = function() {
        if (xhr.status != 200) {
            var itemsElement = document.getElementById('items');
            itemsElement.innerHTML = 'There was an error showing your history, sorry.';
            return;
        }
        var result = JSON.parse(this.response);
        if (callback) callback(result);
    };
    xhr.send();
}

var uploads = 0;
function browse() {
    var file = document.getElementById('browse');
    file.click();
}

function handleFiles(files) {
    var area = document.getElementById('files');
    area.className = '';
    var blurb = document.getElementById('blurb');
    blurb.className = 'hidden';
    var timeout = 500;
    for (var i = 0; i < files.length; i++) {
        uploads++;
        if (i == 0)
            handleFile(files[i]);
        else {
            void function(i) {
                setTimeout(function() {
                    handleFile(files[i]);
                }, timeout);
            }(i);
            timeout += 500;
        }
    }
}

function handleFile(file) {
    var reader = new FileReader();
    reader.onloadend = function(e) {
        var data = e.target.result;
        var hash = btoa(rstr_md5(data)).substr(0, 12).replace('+', '-').replace('/', '_');
        var preview = createPreview(file);
        if (!preview.supported) {
            var error = document.createElement('span');
            error.className = 'error';
            error.textContent = 'This filetype is not supported.';
            preview.fileStatus.appendChild(error);
        } else {
            var xhr = new XMLHttpRequest();
            xhr.open('GET', '/api/' + hash + '/exists');
            xhr.onload = function() {
                if (this.status == 200) {
                    var p = document.createElement('h3');
                    p.textContent = 'Upload complete!';
                    var p2 = document.createElement('p');
                    p2.className = 'small';
                    p.textContent = 'Tap here to view';
                    var a = document.createElement('a');
                    a.setAttribute('target', '_blank');
                    a.href = '/' + hash;
                    a.className = 'full-size';
                    preview.fileStatus.appendChild(a);
                    preview.fileStatus.appendChild(p);
                    preview.fileStatus.appendChild(p2);
                    uploads--;
                    addItemToHistory(hash);
                } else {
                    var p = document.createElement('h3');
                    p.textContent = 'Uploading...';
                    preview.fileStatus.appendChild(p);
                    uploadFile(file, hash, preview.fileStatus, preview.progress);
                }
            };
            xhr.send();
        }
    };
    reader.readAsBinaryString(file);
}

function uploadFile(file, hash, statusUI, progressUI) {
    var xhr = new XMLHttpRequest();
    xhr.open('POST', '/api/upload/file');
    xhr.upload.onprogress = function(e) {
        if (e.lengthComputable) {
            progressUI.style.width = (e.loaded / e.total) * 100 + '%';
        }
    };
    xhr.onload = function() {
        var error = null;
        var responseJSON = JSON.parse(this.responseText);

        if (this.status == 415) {
            error = 'This media format is not supported.';
        } else if (this.status == 409) {
            finish(statusUI, responseJSON['hash']);
        } else if (this.status == 420) {
            error = 'You have consumed your hourly quota. Try again later.';
        } else if (this.status == 200) {
            statusUI.innerHTML = '';
            var p = document.createElement('h3');
            p.textContent = 'Processing...';
            statusUI.appendChild(p);
            hash = responseJSON['hash'];
            progressUI.className += ' progress-green';
            progressUI.style.width = '100%';
            setTimeout(function() {
                checkStatus(hash, statusUI, progressUI);
            }, 1000);
        }
        if (error) {
            progressUI.parentElement.removeChild(progressUI);
            var errorText = document.createElement('p');
            errorText.className = 'error';
            errorText.textContent = error;
            statusUI.appendChild(errorText);
            progressUI.style.width = 0;
        }
    };
    var formData = new FormData();
    formData.append('file', file);
    xhr.send(formData);
}

function checkStatus(hash, statusUI, progressUI) {
    var xhr = new XMLHttpRequest();
    xhr.open('GET', '/api/' + hash + '/status');
    xhr.onload = function() {
        responseJSON = JSON.parse(this.responseText);
        if (responseJSON['status'] == 'ready' || responseJSON['status'] == 'done') {
            progressUI.parentElement.removeChild(progressUI);
            finish(statusUI, hash);
        } else if (responseJSON['status'] == 'timeout' || responseJSON['status'] == 'error') {
            progressUI.parentElement.removeChild(progressUI);
            var error = document.createElement('p');
            error.className = 'error';
            statusUI.innerHTML = '';
            if (responseJSON['status'] == 'timeout') {
                error.textContent = 'This file took too long to process.';
            } else {
                error.textContent = 'There was an error processing this file.';
            }
            statusUI.appendChild(error);
            uploads--;
        } else {
            setTimeout(function() {
                checkStatus(hash, statusUI, progressUI);
            }, 1000);
        }
    };
    xhr.send();
}

function finish(statusUI, hash) {
    var p = document.createElement('h3');
    p.textContent = 'Upload complete! ';
    var a2 = document.createElement('a');
    a2.setAttribute('target', '_blank');
    a2.href = '/' + hash;
    a2.className = 'full-size';
    statusUI.innerHTML = '';
{'fraction_non_alphanumeric': 0.14092994590152708, 'fraction_numerical': 0.008217489556940355, 'mean_word_length': 1.921968787515006, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7352656', 'n_tokens_mistral': 4102, 'n_tokens_neox': 3945, 'n_words': 865}