Skip to content

Commit

Permalink
can use '?load=' query string to load from a URL.
Browse files Browse the repository at this point in the history
  • Loading branch information
ixchow committed Mar 16, 2021
1 parent 49d954e commit 3f37d91
Showing 1 changed file with 45 additions and 18 deletions.
63 changes: 45 additions & 18 deletions visualizer.html
Original file line number Diff line number Diff line change
Expand Up @@ -428,34 +428,52 @@
compile.addEventListener('click', function() { updateVisualizer(false); } );

let reload = document.getElementById("reload");
let currentFile = null;
let currentSource = null; //{file:file} or {url:url} or null
reload.addEventListener('click', function() {
if (currentFile) {
readFile(currentFile);
if (currentSource) {
if (currentSource.file) {
readFile(currentSource.file);
} else if (currentSource.url) {
readURL(currentSource.url);
}
}
});


function setSource(source, text) {
const sameFile = JSON.stringify(currentSource) === JSON.stringify(currentSource);
currentSource = source;
const name = (source.file ? source.file.name : source.url);
document.getElementById('fileName').innerText = name;
console.log("read " + name);
let oldText = text;
//line ending conversion:
text = text.replace(/\r\n/g,"\n");
if (oldText != text) {
console.warn("Converted dos-style line endings to unix-style.")
}
editor.setValue(text, -1);
updateVisualizer(!sameFile);
}

function readURL(url) {
console.log("Attempting to read url: '" + url + "'");
let request = new XMLHttpRequest();
request.addEventListener('load', () => {
setSource({url:url}, request.responseText);
});
request.open('GET', url);
request.send();
}


function readFile(file) {
console.log("Attempting to read file: '" + file.name + "'");

//generate new data:
var reader = new FileReader();
reader.onload = function(){
const sameFile = currentFile && (currentFile.name == file.name);
currentFile = file;
document.getElementById('fileName').innerText = file.name;
console.log("read " + file.name);
let text = reader.result;
let oldText = text;
//line ending conversion:
text = text.replace(/\r\n/g,"\n");
if (oldText != text) {
console.warn("Converted dos-style line endings to unix-style.")
}
editor.setValue(text, -1);

updateVisualizer(!sameFile);
reader.onload = () => {
setSource({file:file}, reader.result);
};
console.log("reading " + file.name);
reader.readAsText(file);
Expand Down Expand Up @@ -561,6 +579,15 @@
evt.preventDefault();
return false;
});


{ //check for '?load=....' in the URL:
const m = document.location.search.match(/^\?load=(.+)$/);
if (m) {
readURL(m[1]);
}
}

</script>

</body>

0 comments on commit 3f37d91

Please sign in to comment.