<!DOCTYPE html>
<html>
	<meta charset="UTF-8">
	<head>
		<title> URL Checker </title>
	</head>

	<script type="text/javascript">
    var lastCheck = Date.now()
	
    function check_link(x) {
        l = x.value
        if (!l.match(/^((https|ftp|http|git?):\/\/)?([w|W]{3}\.)+[a-zA-Z0-9\-\.]{3,}\.[a-zA-Z]{2,}(\.[a-zA-Z]{2,})?$/)) {
            document.getElementById('valid').innerHTML = "Invalid Link";
            document.getElementById('file').innerHTML = "";
        }
        else {
            document.getElementById('valid').innerHTML = "Valid Link format";
            setTimeout(function(){
                fetch(l)
                .then((response) => {
                if (response.ok) {
                    let temp = response.json()
                    document.getElementById('file').innerHTML = temp.fileOrFolder + "exists at this URL"
                    }
                else {
                    document.getElementById('file').innerHTML = "URL does not exist";
                    }
                })
                .catch(error => console.error(error));
            }, Math.max( lastCheck-Date.now()+2000 , 0));
            lastCheck = Date.now();
        }
    }
    </script>
	
	<body>
		<br><h2> URL Checker </h2><br>
        <input id = "lin" type="text" placeholder="Enter valid URL" oninput="check_link(this)">
		<br><div id='valid'></div>
		<br><div id='file'></div>
	</body>
</html>
