Skip to content

Instantly share code, notes, and snippets.

@dirkteucher
Created November 9, 2014 21:57
Show Gist options
  • Save dirkteucher/f76a37a5ed090b08a5e5 to your computer and use it in GitHub Desktop.
Save dirkteucher/f76a37a5ed090b08a5e5 to your computer and use it in GitHub Desktop.
if(localFileSizeChanges) do something in browser.
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<title>Watch for a file change</title>
<style type='text/css'>
body {
font-family: sans-serif;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" ></script>
</head>
<body>
<input type='file' id='filename'>
<input type='button' id='btnStart' value='Start'>
<script type='text/javascript'>
(function() {
var input;
var lastMod;
//start checking for file changes. If the file you added changes in size and is saved then this
//script will detect this and in my case click next on a button to page forward on a web site
document.getElementById('btnStart').onclick = function() {
startWatching();
};
function startWatching() {
var file;
if (typeof window.FileReader !== 'function') {
display("The file API isn't supported on this browser yet.");
return;
}
//Input check
input = document.getElementById('filename');
if (!input) {
display("Um, couldn't find the filename element.");
}
//check if browser supports files .. firefox has some problems with this script somewhere, maybe this
else if (!input.files) {
display("This browser doesn't seem to support the `files` property of file inputs.");
}
//if a file has not been added yet
else if (!input.files[0]) {
display("Please select a file before clicking 'Show Size'");
}
else {
file = input.files[0];
lastMod = file.lastModifiedDate;
display("Last modified date: " + lastMod);
display("Change the file");
setInterval(tick, 250);
}
}
function tick() {
var file = input.files && input.files[0];
if (file && lastMod && file.lastModifiedDate.getTime() !== lastMod.getTime()) {
lastMod = file.lastModifiedDate;
display("File changed: " + lastMod);
}
}
function display(msg) {
var p = document.createElement('p');
p.innerHTML = msg;
document.body.appendChild(p);
$(".navNext").click();
console.log("next was clicked");
}
})();
</script>
</body>
</html>
@dirkteucher
Copy link
Author

I used this to page onto the next image on imgur while writing code. Each time you save the file the script hits the next image button. A fun way to pass the time when writing code. Some problems with firefox.

http://www.html5rocks.com/en/tutorials/file/dndfiles/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment