62 lines
1.8 KiB
TypeScript
62 lines
1.8 KiB
TypeScript
export default class App {
|
|
input: HTMLInputElement;
|
|
outputContainer: HTMLTextAreaElement;
|
|
|
|
constructor() {
|
|
this.input = document.getElementById('in') as HTMLInputElement;
|
|
this.outputContainer = document.getElementById('out') as HTMLTextAreaElement;
|
|
(document.getElementById("run") as HTMLButtonElement).onclick = this.run.bind(this);
|
|
}
|
|
|
|
async run() {
|
|
const promises: Promise<string>[] = [];
|
|
for (const file of this.input.files!) {
|
|
promises.push(this.parseFile(file));
|
|
}
|
|
|
|
const parts = await Promise.all(promises);
|
|
|
|
const output = new Blob([parts.join(' ')], { type: 'text/plain' });
|
|
const a = document.createElement('a');
|
|
a.href = URL.createObjectURL(output);
|
|
a.download = "result.txt";
|
|
a.click();
|
|
}
|
|
|
|
async parseFile(file: File) {
|
|
const xml = await file.text();
|
|
const parser = new DOMParser();
|
|
const doc = parser.parseFromString(xml, "text/xml");
|
|
|
|
const results: string[] = [];
|
|
|
|
for (const node of doc.getElementsByTagNameNS('*', 'tartalom')) {
|
|
let resolve: (value: string) => void, reject: (reason?: any) => void;
|
|
const resultPromise = new Promise<string>((res, rej) => {
|
|
resolve = res, reject = rej;
|
|
});
|
|
|
|
const worker = new Worker('worker.js');
|
|
|
|
worker.onmessage = (e) => {
|
|
resolve(e.data);
|
|
};
|
|
|
|
worker.onerror = (e) => {
|
|
reject(e.error);
|
|
}
|
|
|
|
worker.postMessage(node.textContent);
|
|
|
|
try {
|
|
results.push(await resultPromise);
|
|
} catch (e) {
|
|
console.error(e);
|
|
}
|
|
}
|
|
|
|
return results.join(' ');
|
|
}
|
|
}
|
|
|
|
new App(); |