support loading logs through postMessage in logviewer

This commit is contained in:
Bruno Windels 2022-05-10 11:02:15 +02:00
parent a2a17dbf7a
commit 2a729f8969
2 changed files with 21 additions and 1 deletions

View file

@ -218,5 +218,20 @@
<main></main>
<aside></aside>
<script type="module" src="main.js"></script>
<script type="module">
import {loadBlob} from "./main.js";
window.addEventListener("message", event => {
console.log("message", event);
if (event.data.type === "open") {
document.getElementById("filename").innerText = "Loading logs from other tab...";
loadBlob(event.data.logs).then(() => {
document.getElementById("filename").innerText = "Logs from other tab at " + new Date();
});
} else if (event.data.type === "ping") {
document.getElementById("filename").innerText = "Waiting for logs from other tab...";
event.source.postMessage({type: "pong"});
}
});
</script>
</body>
</html>

View file

@ -164,7 +164,11 @@ function getRootItemHeader(prevItem, item) {
async function loadFile() {
const file = await openFile();
document.getElementById("filename").innerText = file.name;
const json = await readFileAsText(file);
await loadBlob(file);
}
export async function loadBlob(blob) {
const json = await readFileAsText(blob);
const logs = JSON.parse(json);
logs.items.sort((a, b) => itemStart(a) - itemStart(b));
rootItem = {c: logs.items};
@ -181,6 +185,7 @@ async function loadFile() {
return fragment;
}, document.createDocumentFragment());
main.replaceChildren(fragment);
main.scrollTop = main.scrollHeight;
}
// TODO: make this use processRecursively