let url;
let loop;
let finished = false;

$("#document").ready(onReady);


function onReady() {
    const element = document.querySelector('form');
    displayUrlBox();
    element.addEventListener('submit', function(event) {
        event.preventDefault();
        if($("#urlBox").val().length > 0) {
            start();
        }
        return false;
    });
}

function displayUrlBox() {
    $("#urlBox").css("display", "inline-block");
    $("#progressBar").css("display", "none");
}

function displayLoadingBar() {
    $("#urlBox").css("display", "none");
    $("#progressBar").css("display", "inline-block");
}

function start() {
    finished = false;
    event.preventDefault();
    url = $("#urlBox").val();
    $("#status").text("Getting layer details...");
    $.ajax({
        url: "/scrape",
        data : {
            url : url
        },
        success : function(response) {
            displayLoadingBar();
            refreshStatus();
            loop = setInterval(refreshStatus, 500);
            $("#status").text(response);
        },
        error : function(error) {
            // alert("Please enter a url");
            // console.log(error);
        }
    });
    return false;
}

function refreshStatus() {
    $.getJSON("/status", {url : url }, function(response) {
        finished = response.finished;
        if(response.failed === true) {
            $("#status").text("Failed :(");
        } else if( finished === true) {
            clearInterval(loop);
            let status = $("#status");
            status.text("Finished!");
            $("#download").prop("disabled", false);
            let percentDone = 100 * response.done / response.total;
            $("#progressBar").find("> div").css("width", percentDone + "%");
            download();
            displayUrlBox();
            status.text("Another?");
        } else if(response.total !== 0 && response.total === response.done) {
            $("#status").text("Zipping up files...");
        } else if(response.total !== 0) {
            $("#download").prop("disabled", true);
            let progressBar =$("#progressBar").find("> div");
            let percentDone = 100 * response.done / response.total;
            progressBar.css("width", percentDone + "%");
            let status = $("#status");
            status.text( response.layer + ": " + Math.round(percentDone) + "%" );
        }
    })
}

function download() {
    window.location.replace("/output?url=" + url);
}
@@ -33,16 +33,48 @@
 = false;
+%0Alet alreadyRanDownload = false;
 %0A%0A$(%22#do
@@ -1475,32 +1475,56 @@
 inished === true
+ && !alreadyRanDownload 
 ) %7B%0A            
@@ -2479,24 +2479,93 @@
 ownload() %7B%0A
+    if(!alreadyRanDownload) %7B%0A        alreadyRanDownload = true;%0A    
     window.l
@@ -2603,9 +2603,15 @@
 + url);%0A
+    %7D%0A
 %7D

add check to potentially fix multiple downloads