-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.js
More file actions
73 lines (67 loc) · 2.27 KB
/
viewer.js
File metadata and controls
73 lines (67 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
(function(){
const qs = new URLSearchParams(location.search);
const src = qs.get("src");
const textEl = document.getElementById("text");
const statusEl = document.getElementById("status");
const readBtn = document.getElementById("btn-read");
if (!src) {
textEl.textContent = "Ingen kilde angivet.";
return;
}
async function loadPDF(url){
const pdf = await window.pdfjsLib.getDocument(url).promise;
let out = "";
for (let i=1;i<=pdf.numPages;i++){
const page = await pdf.getPage(i);
const cont = await page.getTextContent();
out += cont.items.map(t => t.str).join(" ") + "\n";
statusEl.textContent = `PDF side ${i}/${pdf.numPages}`;
}
return out;
}
async function loadDOCX(url){
const res = await fetch(url);
const buf = await res.arrayBuffer();
const result = await window.mammoth.convertToPlainText({ arrayBuffer: buf });
return result.value || "";
}
(async function init(){
try{
const lower = src.toLowerCase();
let txt = "";
if (lower.endsWith(".pdf")) {
statusEl.textContent = "Henter PDF…";
txt = await loadPDF(src);
} else if (lower.endsWith(".docx")) {
statusEl.textContent = "Henter Word…";
txt = await loadDOCX(src);
} else {
statusEl.textContent = "Henter…";
const res = await fetch(src);
txt = await res.text(); // fallback (ikke-strengt nødvendigt)
}
textEl.textContent = txt.trim().slice(0, 100000); // beskyt mod enorme filer
statusEl.textContent = "Klar.";
}catch(e){
console.error(e);
textEl.textContent = "Kunne ikke indlæse dokumentet.";
statusEl.textContent = "Fejl.";
}
})();
readBtn.addEventListener("click", () => {
const tmp = document.createElement("div");
tmp.className = "martha-article";
tmp.style.display = "none";
tmp.textContent = textEl.textContent;
document.body.appendChild(tmp);
if (window.martha && window.martha.speakRanges) {
const r = document.createRange();
r.selectNodeContents(tmp);
window.martha.speakRanges([r]);
} else if (window.martha && window.martha.click) {
window.martha.click.call(tmp, new Event("click"));
} else {
alert("Martha er ikke klar.");
}
});
})();