-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
158 lines (148 loc) · 6.36 KB
/
Copy pathcontent.js
File metadata and controls
158 lines (148 loc) · 6.36 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
console.log("[MarthaExt] content.js loaded");
// ============ Samtykke ============
(async function showConsent() {
const consentKey = "marthaConsentAccepted";
const { [consentKey]: ok } = await chrome.storage.local.get(consentKey);
if (ok) return;
const box = document.createElement("div");
Object.assign(box.style, {
position: "fixed", inset: 0, background: "rgba(0,0,0,.75)",
display: "flex", alignItems: "center", justifyContent: "center",
zIndex: 999999
});
box.innerHTML = `
<div style="background:#fff;color:#000;padding:20px;border-radius:10px;max-width:520px;font-family:system-ui,sans-serif;">
<h2>Beskyttelse af dine data</h2>
<p><b>Martha Læser</b> bruger tekst-til-tale fra
<a href="https://oqaasileriffik.gl" target="_blank">Oqaasileriffik.gl</a>.
Kun den tekst du markerer, sendes midlertidigt til deres server for oplæsning.
Der gemmes ingen personoplysninger, og intet bruges til statistik eller reklame.</p>
<div style="margin-top:16px;text-align:right;">
<button id="martha-accept">Accepter</button>
<button id="martha-decline">Afvis</button>
</div>
</div>`;
document.body.appendChild(box);
document.getElementById("martha-accept").onclick = async () => {
await chrome.storage.local.set({ [consentKey]: true });
box.remove();
};
document.getElementById("martha-decline").onclick = async () => {
await chrome.storage.local.remove(consentKey);
alert("Du skal acceptere samtykket for at bruge udvidelsen.");
box.remove();
};
})();
// ============ Style ============
const style = document.createElement("style");
style.textContent = `
.martha-current{ background: yellow !important; box-shadow:none !important; }
#martha-controls{ z-index:2147483647 !important; }
#martha-float{ position:fixed; right:16px; bottom:16px; z-index:2147483647; }
#martha-float.hidden{ display:none !important; }
#martha-float .panel{
background:#111;color:#fff;border-radius:10px;padding:10px;
font-family:system-ui,sans-serif; box-shadow:0 2px 10px rgba(0,0,0,.25);
cursor:move; user-select:none;
}
#martha-float .panel button{
width:100%; margin:6px 0 0; padding:8px 10px; border:none; border-radius:8px;
background:#2a2a2a; color:#fff; cursor:pointer;
}
`;
document.documentElement.appendChild(style);
// ============ Martha helpers ============
function whenMarthaReady(cb, tries = 0) {
if (window.martha && (window.martha.speakRanges || window.martha.click)) return cb();
if (tries > 100) return console.error("[MarthaExt] Martha API not ready");
setTimeout(() => whenMarthaReady(cb, tries + 1), 50);
}
function readSelection(){
const sel = window.getSelection();
if (!sel || sel.rangeCount === 0 || !sel.toString().trim()) {
alert("Marker tekst på siden først, og prøv igen.");
return;
}
const ranges = [];
for (let i = 0; i < sel.rangeCount; i++) ranges.push(sel.getRangeAt(i).cloneRange());
whenMarthaReady(() => {
try{
if (window.martha.speakRanges) window.martha.speakRanges(ranges);
else if (window.martha.click) {
const tmp = document.createElement("div");
tmp.className = "martha-article"; tmp.style.display = "none";
tmp.appendChild(ranges[0].cloneContents());
document.body.appendChild(tmp);
window.martha.click.call(tmp, new Event("click"));
}
}catch(e){ console.error("[MarthaExt] call error", e); }
});
}
// ============ Floating UI (drag + persist) ============
(async function initFloating() {
const root = document.createElement("div");
root.id = "martha-float";
root.innerHTML = `
<div class="panel" id="martha-drag">
<div style="font-weight:600;margin-bottom:6px;">Martha</div>
<button id="btn-read">Læs markeret tekst</button>
<button id="btn-open-reader" style="display:none;"></button>
</div>`;
document.body.appendChild(root);
// position + visibility
const { marthaFloatPos, marthaFloatingHidden } = await chrome.storage.local.get(["marthaFloatPos","marthaFloatingHidden"]);
if (marthaFloatPos && typeof marthaFloatPos.x === "number" && typeof marthaFloatPos.y === "number") {
root.style.right = "auto"; root.style.bottom = "auto";
root.style.left = marthaFloatPos.x + "px";
root.style.top = marthaFloatPos.y + "px";
}
if (marthaFloatingHidden) root.classList.add("hidden");
// drag logic
(function makeDraggable(handle, box){
let dragging=false, sx=0, sy=0, bx=0, by=0;
handle.addEventListener("mousedown", e=>{
dragging=true; sx=e.clientX; sy=e.clientY;
const rect = box.getBoundingClientRect(); bx=rect.left; by=rect.top;
document.addEventListener("mousemove", onMove);
document.addEventListener("mouseup", onUp, { once:true });
});
function onMove(e){
if(!dragging) return;
const nx = bx + (e.clientX - sx);
const ny = by + (e.clientY - sy);
box.style.left = nx + "px"; box.style.top = ny + "px";
box.style.right = "auto"; box.style.bottom = "auto";
}
async function onUp(){
dragging=false;
document.removeEventListener("mousemove", onMove);
const rect = box.getBoundingClientRect();
await chrome.storage.local.set({ marthaFloatPos: { x: rect.left, y: rect.top } });
}
})(document.getElementById("martha-drag"), root);
// buttons
document.getElementById("btn-read").addEventListener("click", readSelection);
// PDF / Word – vis “Åbn i Martha Reader” hvis nødvendig
const url = location.href.toLowerCase();
const readerBtn = document.getElementById("btn-open-reader");
if (url.endsWith(".pdf") || url.endsWith(".docx")) {
readerBtn.style.display = "block";
readerBtn.textContent = url.endsWith(".pdf") ? "Åbn PDF i Martha Reader" : "Åbn Word i Martha Reader";
readerBtn.onclick = () => {
const u = chrome.runtime.getURL("viewer.html") + "?src=" + encodeURIComponent(location.href);
window.open(u, "_blank");
};
}
// lyt til popup (toggle)
chrome.runtime.onMessage.addListener((msg)=>{
if(!msg) return;
if (msg.type === "toggle_floating") {
root.classList.toggle("hidden");
chrome.storage.local.set({ marthaFloatingHidden: root.classList.contains("hidden") });
}
if (msg.type === "reset_consent") {
chrome.storage.local.remove("marthaConsentAccepted");
alert("Samtykke nulstillet. Genindlæs siden for at se samtykke-popup igen.");
}
});
})();