Skip to content

Instantly share code, notes, and snippets.

@FlandreDaisuki
Last active August 21, 2021 08:38
Show Gist options
  • Save FlandreDaisuki/4dbd94ad90d0b84ed62004e9925c6bd4 to your computer and use it in GitHub Desktop.
Save FlandreDaisuki/4dbd94ad90d0b84ed62004e9925c6bd4 to your computer and use it in GitHub Desktop.
single-page-file-watcher-poc
<pre><code id="output"></code></pre>
<script>
// https://stackoverflow.com/a/59925724
if (/^file:\/\/\//.test(location.href)) {
let path = './';
let orig = fetch;
window.fetch = (resource) => ((/^[^/:]*:/.test(resource)) ?
orig(resource) :
new Promise(function(resolve, reject) {
let request = new XMLHttpRequest();
let fail = (error) => {reject(error)};
['error', 'abort'].forEach((event) => { request.addEventListener(event, fail); });
let pull = (expected) => (new Promise((resolve, reject) => {
if (
request.responseType == expected ||
(expected == 'text' && !request.responseType)
)
resolve(request.response);
else
reject(request.responseType);
}));
request.addEventListener('load', () => (resolve({
arrayBuffer : () => (pull('arraybuffer')),
blob : () => (pull('blob')),
text : () => (pull('text')),
json : () => (pull('json'))
})));
request.open('GET', resource.replace(/^\//, path));
request.send();
})
);
}
</script>
<script>
const fetchJSON = async() => {
const response = await fetch('a.json');
output.textContent = JSON.stringify(JSON.parse(await response.text()), null, 2);
setTimeout(fetchJSON, 500);
};
fetchJSON();
</script>
$ tree .
.
├── a.json
└── index.html
$ google-chrome --allow-file-access-from-files index.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment