accessify-widget 0.3.71 → 0.3.74
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/dist/accessify.min.js +1 -1
- package/dist/accessify.min.js.map +1 -1
- package/dist/accessify.mjs +1 -1
- package/dist/alt-text-CoS2ISqs.js +745 -0
- package/dist/alt-text-CoS2ISqs.js.map +1 -0
- package/dist/{index-DLo9u9kS.js → index-DCvvWewP.js} +10 -743
- package/dist/index-DCvvWewP.js.map +1 -0
- package/dist/{keyboard-nav-C9qVZ3e8.js → keyboard-nav-BMIfQKyz.js} +2 -2
- package/dist/{keyboard-nav-C9qVZ3e8.js.map → keyboard-nav-BMIfQKyz.js.map} +1 -1
- package/dist/{page-structure-DL3Xdzlm.js → page-structure-BimA9vQz.js} +2 -2
- package/dist/{page-structure-DL3Xdzlm.js.map → page-structure-BimA9vQz.js.map} +1 -1
- package/dist/{tts-CjszLRnb.js → tts-zrXtEd07.js} +29 -6
- package/dist/tts-zrXtEd07.js.map +1 -0
- package/dist/widget.js +1 -1
- package/dist/widget.js.map +1 -1
- package/package.json +10 -10
- package/dist/index-DLo9u9kS.js.map +0 -1
- package/dist/tts-CjszLRnb.js.map +0 -1
|
@@ -0,0 +1,745 @@
|
|
|
1
|
+
import { g as getCurrentWidgetLang } from "./index-DCvvWewP.js";
|
|
2
|
+
const IDB_NAME = "accessify-alt-text-cache";
|
|
3
|
+
const IDB_STORE = "alt-texts";
|
|
4
|
+
const IDB_VERSION = 2;
|
|
5
|
+
function hashSrc(src) {
|
|
6
|
+
let hash = 5381;
|
|
7
|
+
const n = src.toLowerCase().trim();
|
|
8
|
+
for (let i = 0; i < n.length; i++) {
|
|
9
|
+
hash = (hash << 5) + hash + n.charCodeAt(i);
|
|
10
|
+
hash |= 0;
|
|
11
|
+
}
|
|
12
|
+
return Math.abs(hash).toString(36);
|
|
13
|
+
}
|
|
14
|
+
function normalizeImageUrl(url) {
|
|
15
|
+
try {
|
|
16
|
+
const u = new URL(url);
|
|
17
|
+
u.searchParams.delete("width");
|
|
18
|
+
u.searchParams.delete("height");
|
|
19
|
+
u.searchParams.delete("scale-down-to");
|
|
20
|
+
return u.href;
|
|
21
|
+
} catch {
|
|
22
|
+
return url;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function getImageSrc(img) {
|
|
26
|
+
return img.currentSrc || img.src;
|
|
27
|
+
}
|
|
28
|
+
function openCache() {
|
|
29
|
+
return new Promise((resolve) => {
|
|
30
|
+
try {
|
|
31
|
+
const req = indexedDB.open(IDB_NAME, IDB_VERSION);
|
|
32
|
+
req.onupgradeneeded = () => {
|
|
33
|
+
const db = req.result;
|
|
34
|
+
if (!db.objectStoreNames.contains(IDB_STORE)) db.createObjectStore(IDB_STORE, { keyPath: "key" });
|
|
35
|
+
if (!db.objectStoreNames.contains("alt-text-reports")) db.createObjectStore("alt-text-reports", { keyPath: "key" });
|
|
36
|
+
};
|
|
37
|
+
req.onsuccess = () => resolve(req.result);
|
|
38
|
+
req.onerror = () => resolve(null);
|
|
39
|
+
} catch {
|
|
40
|
+
resolve(null);
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
async function getAllCachedAltTexts() {
|
|
45
|
+
const db = await openCache();
|
|
46
|
+
const map = /* @__PURE__ */ new Map();
|
|
47
|
+
if (!db) return map;
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
try {
|
|
50
|
+
const tx = db.transaction(IDB_STORE, "readonly");
|
|
51
|
+
const req = tx.objectStore(IDB_STORE).getAll();
|
|
52
|
+
req.onsuccess = () => {
|
|
53
|
+
for (const r of req.result) {
|
|
54
|
+
map.set(r.src, r.altText);
|
|
55
|
+
}
|
|
56
|
+
resolve(map);
|
|
57
|
+
};
|
|
58
|
+
req.onerror = () => resolve(map);
|
|
59
|
+
} catch {
|
|
60
|
+
resolve(map);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async function getCachedAltText(src) {
|
|
65
|
+
const db = await openCache();
|
|
66
|
+
if (!db) return null;
|
|
67
|
+
return new Promise((resolve) => {
|
|
68
|
+
try {
|
|
69
|
+
const tx = db.transaction(IDB_STORE, "readonly");
|
|
70
|
+
const store = tx.objectStore(IDB_STORE);
|
|
71
|
+
const req1 = store.get(hashSrc(src));
|
|
72
|
+
req1.onsuccess = () => {
|
|
73
|
+
if (req1.result?.altText) {
|
|
74
|
+
resolve(req1.result.altText);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
const norm = normalizeImageUrl(src);
|
|
78
|
+
if (norm !== src) {
|
|
79
|
+
const req2 = store.get(hashSrc(norm));
|
|
80
|
+
req2.onsuccess = () => resolve(req2.result?.altText || null);
|
|
81
|
+
req2.onerror = () => resolve(null);
|
|
82
|
+
} else {
|
|
83
|
+
resolve(null);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
req1.onerror = () => resolve(null);
|
|
87
|
+
} catch {
|
|
88
|
+
resolve(null);
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
async function setCachedAltText(src, altText, langCode) {
|
|
93
|
+
const db = await openCache();
|
|
94
|
+
if (!db) return;
|
|
95
|
+
return new Promise((resolve) => {
|
|
96
|
+
try {
|
|
97
|
+
const tx = db.transaction(IDB_STORE, "readwrite");
|
|
98
|
+
tx.objectStore(IDB_STORE).put({ key: hashSrc(src), src, altText, lang: langCode, createdAt: Date.now() });
|
|
99
|
+
tx.oncomplete = () => resolve();
|
|
100
|
+
tx.onerror = () => resolve();
|
|
101
|
+
} catch {
|
|
102
|
+
resolve();
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const DEFAULT_API_BASE = "https://accessify-api.accessify.workers.dev";
|
|
107
|
+
let currentAltSiteMode = "manual";
|
|
108
|
+
async function fetchServerAltTexts(siteKey, proxyUrl, lang) {
|
|
109
|
+
const map = /* @__PURE__ */ new Map();
|
|
110
|
+
try {
|
|
111
|
+
const base = proxyUrl || DEFAULT_API_BASE;
|
|
112
|
+
const pageUrl = window.location.origin + window.location.pathname;
|
|
113
|
+
const res = await fetch(`${base}/v1/manifest?siteKey=${encodeURIComponent(siteKey)}&url=${encodeURIComponent(pageUrl)}&feature=alt_text&variant=${encodeURIComponent(lang)}`, {
|
|
114
|
+
headers: { "Accept": "application/json" },
|
|
115
|
+
cache: "no-cache"
|
|
116
|
+
});
|
|
117
|
+
if (!res.ok) return map;
|
|
118
|
+
const data = await res.json();
|
|
119
|
+
currentAltSiteMode = data.siteMode === "auto" ? "auto" : "manual";
|
|
120
|
+
if (data.blocks) {
|
|
121
|
+
for (const block of data.blocks) {
|
|
122
|
+
if (block.selector && block.result) map.set(block.selector, block.result);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
} catch {
|
|
126
|
+
}
|
|
127
|
+
return map;
|
|
128
|
+
}
|
|
129
|
+
function persistAltTextToServer(siteKey, proxyUrl, imageUrl, altText, lang) {
|
|
130
|
+
try {
|
|
131
|
+
const base = proxyUrl || DEFAULT_API_BASE;
|
|
132
|
+
const pageUrl = window.location.origin + window.location.pathname;
|
|
133
|
+
fetch(`${base}/v1/cache/persist-alt-text`, {
|
|
134
|
+
method: "POST",
|
|
135
|
+
headers: { "Content-Type": "application/json" },
|
|
136
|
+
body: JSON.stringify({ siteKey, pageUrl, imageUrl, altText, lang })
|
|
137
|
+
}).catch(() => {
|
|
138
|
+
});
|
|
139
|
+
} catch {
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let autoApplied = false;
|
|
143
|
+
async function autoApplyCachedAltTexts(widgetConfig) {
|
|
144
|
+
if (autoApplied) return 0;
|
|
145
|
+
autoApplied = true;
|
|
146
|
+
const cache = /* @__PURE__ */ new Map();
|
|
147
|
+
const siteKey = widgetConfig?.siteKey;
|
|
148
|
+
const proxyUrl = widgetConfig?.proxyUrl || "";
|
|
149
|
+
const pageLang = widgetConfig?.lang?.split("-")[0] || document.documentElement.lang?.split("-")[0] || "en";
|
|
150
|
+
if (siteKey) {
|
|
151
|
+
const serverCache = await fetchServerAltTexts(siteKey, proxyUrl, pageLang);
|
|
152
|
+
for (const [url, alt] of serverCache) {
|
|
153
|
+
cache.set(url, alt);
|
|
154
|
+
const norm = normalizeImageUrl(url);
|
|
155
|
+
if (norm !== url) cache.set(norm, alt);
|
|
156
|
+
setCachedAltText(url, alt, pageLang).catch(() => {
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
const localCache = await getAllCachedAltTexts();
|
|
161
|
+
for (const [url, alt] of localCache) {
|
|
162
|
+
if (!cache.has(url)) cache.set(url, alt);
|
|
163
|
+
}
|
|
164
|
+
if (cache.size === 0) return 0;
|
|
165
|
+
function applyToImg(img) {
|
|
166
|
+
if (img.closest("#accessify-root") || img.closest("accessify-widget")) return false;
|
|
167
|
+
const src = getImageSrc(img);
|
|
168
|
+
const norm = normalizeImageUrl(src);
|
|
169
|
+
const imgSrcNorm = normalizeImageUrl(img.src);
|
|
170
|
+
const cached = cache.get(src) || cache.get(norm) || cache.get(img.src) || cache.get(imgSrcNorm);
|
|
171
|
+
if (!cached) return false;
|
|
172
|
+
const alt = img.getAttribute("alt");
|
|
173
|
+
if (alt === null || alt.trim() === "") {
|
|
174
|
+
img.setAttribute("alt", cached);
|
|
175
|
+
img.setAttribute("data-accessify-alt", "auto");
|
|
176
|
+
}
|
|
177
|
+
img.setAttribute("title", cached);
|
|
178
|
+
return true;
|
|
179
|
+
}
|
|
180
|
+
let applied = 0;
|
|
181
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
182
|
+
if (applyToImg(img)) applied++;
|
|
183
|
+
});
|
|
184
|
+
const observer = new MutationObserver((mutations) => {
|
|
185
|
+
for (const m of mutations) {
|
|
186
|
+
for (const node of m.addedNodes) {
|
|
187
|
+
const imgs = node instanceof HTMLImageElement ? [node] : node instanceof HTMLElement ? Array.from(node.querySelectorAll("img")) : [];
|
|
188
|
+
for (const img of imgs) applyToImg(img);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
observer.observe(document.body, { childList: true, subtree: true });
|
|
193
|
+
setTimeout(() => observer.disconnect(), 3e4);
|
|
194
|
+
return applied;
|
|
195
|
+
}
|
|
196
|
+
function createAltTextModule(aiService, initialLang = "de", serverConfig) {
|
|
197
|
+
const siteKey = serverConfig?.siteKey || "";
|
|
198
|
+
const proxyUrl = serverConfig?.proxyUrl || "";
|
|
199
|
+
let enabled = false;
|
|
200
|
+
let domObserver = null;
|
|
201
|
+
let autoGenerating = false;
|
|
202
|
+
let missingAltImages = [];
|
|
203
|
+
const processedImages = /* @__PURE__ */ new Map();
|
|
204
|
+
function lang() {
|
|
205
|
+
return getCurrentWidgetLang() || initialLang;
|
|
206
|
+
}
|
|
207
|
+
function isDE() {
|
|
208
|
+
return lang().startsWith("de");
|
|
209
|
+
}
|
|
210
|
+
function isGenericAlt(alt) {
|
|
211
|
+
if (!alt.trim()) return true;
|
|
212
|
+
if (/^(image|img|photo|bild|foto|untitled|placeholder)/i.test(alt)) return true;
|
|
213
|
+
if (/^IMG_\d+/i.test(alt)) return true;
|
|
214
|
+
if (/\.(jpg|jpeg|png|gif|webp|svg|avif)$/i.test(alt)) return true;
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
function isDecorativeImage(img) {
|
|
218
|
+
const role = img.getAttribute("role");
|
|
219
|
+
if (role === "presentation" || role === "none") return true;
|
|
220
|
+
if (img.complete && img.naturalWidth > 0 && (img.naturalWidth < 50 || img.naturalHeight < 50)) return true;
|
|
221
|
+
return false;
|
|
222
|
+
}
|
|
223
|
+
function isValidImage(img) {
|
|
224
|
+
if (img.closest("#accessify-root") || img.closest("accessify-widget")) return false;
|
|
225
|
+
if (img.complete && img.naturalWidth > 0 && (img.naturalWidth < 50 || img.naturalHeight < 50)) return false;
|
|
226
|
+
const rect = img.getBoundingClientRect();
|
|
227
|
+
if (rect.width < 40 || rect.height < 40) return false;
|
|
228
|
+
const style = getComputedStyle(img);
|
|
229
|
+
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") return false;
|
|
230
|
+
if (img.getAttribute("role") === "presentation" || img.getAttribute("aria-hidden") === "true") return false;
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
function needsAltText(img) {
|
|
234
|
+
if (!isValidImage(img)) return false;
|
|
235
|
+
const alt = img.getAttribute("alt");
|
|
236
|
+
if (alt === null) return true;
|
|
237
|
+
if (isGenericAlt(alt)) return true;
|
|
238
|
+
if (alt === "" && !isDecorativeImage(img)) return true;
|
|
239
|
+
return false;
|
|
240
|
+
}
|
|
241
|
+
function scanForMissingAlt() {
|
|
242
|
+
const missing = [];
|
|
243
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
244
|
+
if (needsAltText(img)) missing.push(img);
|
|
245
|
+
});
|
|
246
|
+
document.querySelectorAll("picture").forEach((picture) => {
|
|
247
|
+
if (picture.closest("#accessify-root") || picture.closest("accessify-widget")) return;
|
|
248
|
+
const img = picture.querySelector("img");
|
|
249
|
+
if (img && !missing.includes(img) && needsAltText(img)) {
|
|
250
|
+
missing.push(img);
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
return missing;
|
|
254
|
+
}
|
|
255
|
+
function gatherImageContext(img) {
|
|
256
|
+
const parts = [];
|
|
257
|
+
try {
|
|
258
|
+
const url = new URL(getImageSrc(img), window.location.href);
|
|
259
|
+
const filename = url.pathname.split("/").pop();
|
|
260
|
+
if (filename) parts.push(`Filename: ${filename}`);
|
|
261
|
+
} catch {
|
|
262
|
+
}
|
|
263
|
+
const figure = img.closest("figure");
|
|
264
|
+
if (figure) {
|
|
265
|
+
const caption = figure.querySelector("figcaption");
|
|
266
|
+
if (caption?.textContent?.trim()) parts.push(`Caption: ${caption.textContent.trim()}`);
|
|
267
|
+
}
|
|
268
|
+
const container = img.parentElement?.parentElement || img.parentElement;
|
|
269
|
+
if (container) {
|
|
270
|
+
const heading = container.querySelector("h1, h2, h3, h4, h5, h6");
|
|
271
|
+
if (heading?.textContent?.trim()) parts.push(`Nearby heading: ${heading.textContent.trim()}`);
|
|
272
|
+
}
|
|
273
|
+
return parts.join(". ");
|
|
274
|
+
}
|
|
275
|
+
function applyAltText(img, altText) {
|
|
276
|
+
img.setAttribute("alt", altText);
|
|
277
|
+
img.setAttribute("title", altText);
|
|
278
|
+
processedImages.set(img, { generatedAlt: altText });
|
|
279
|
+
if (enabled) addInfoBadge(img, altText);
|
|
280
|
+
}
|
|
281
|
+
function applyTitlesToAllImages() {
|
|
282
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
283
|
+
if (!isValidImage(img)) return;
|
|
284
|
+
if (img.getAttribute("title")) return;
|
|
285
|
+
const alt = img.getAttribute("alt");
|
|
286
|
+
if (alt && alt.trim()) {
|
|
287
|
+
img.setAttribute("title", alt);
|
|
288
|
+
img.setAttribute("data-accessify-title", "auto");
|
|
289
|
+
}
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
const BADGE_ATTR = "data-accessify-badge";
|
|
293
|
+
const WRAPPER_ATTR = "data-accessify-badge-wrap";
|
|
294
|
+
const OVERLAY_ID = "accessify-badge-overlay";
|
|
295
|
+
const BADGE_STYLE_ID = "accessify-badge-styles";
|
|
296
|
+
let badgePositionRAF = null;
|
|
297
|
+
let trackedBadges = [];
|
|
298
|
+
let outsideClickHandler = null;
|
|
299
|
+
function getOrCreateOverlay() {
|
|
300
|
+
let overlay = document.getElementById(OVERLAY_ID);
|
|
301
|
+
if (!overlay) {
|
|
302
|
+
overlay = document.createElement("div");
|
|
303
|
+
overlay.id = OVERLAY_ID;
|
|
304
|
+
Object.assign(overlay.style, {
|
|
305
|
+
position: "absolute",
|
|
306
|
+
top: "0",
|
|
307
|
+
left: "0",
|
|
308
|
+
width: "0",
|
|
309
|
+
height: "0",
|
|
310
|
+
overflow: "visible",
|
|
311
|
+
pointerEvents: "none",
|
|
312
|
+
zIndex: "10000"
|
|
313
|
+
});
|
|
314
|
+
document.body.appendChild(overlay);
|
|
315
|
+
}
|
|
316
|
+
if (!document.getElementById(BADGE_STYLE_ID)) {
|
|
317
|
+
const style = document.createElement("style");
|
|
318
|
+
style.id = BADGE_STYLE_ID;
|
|
319
|
+
style.textContent = `
|
|
320
|
+
[${BADGE_ATTR}="badge"]:hover + [${BADGE_ATTR}="tooltip"] { display: block !important; }
|
|
321
|
+
[${BADGE_ATTR}="badge"]:hover { background: rgba(0,0,0,0.85) !important; }
|
|
322
|
+
@media (pointer: coarse) {
|
|
323
|
+
[${BADGE_ATTR}="badge"] { width: 36px !important; height: 36px !important; line-height: 36px !important; font-size: 16px !important; }
|
|
324
|
+
}
|
|
325
|
+
`;
|
|
326
|
+
document.head.appendChild(style);
|
|
327
|
+
}
|
|
328
|
+
return overlay;
|
|
329
|
+
}
|
|
330
|
+
function positionBadge(target, badge, tooltip) {
|
|
331
|
+
const rect = target.getBoundingClientRect();
|
|
332
|
+
if (rect.width < 40 || rect.height < 40 || rect.bottom < 0 || rect.top > window.innerHeight + 200 || rect.right < 0 || rect.left > window.innerWidth + 200) {
|
|
333
|
+
badge.style.display = "none";
|
|
334
|
+
tooltip.style.display = "none";
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
const scrollX = window.scrollX;
|
|
338
|
+
const scrollY = window.scrollY;
|
|
339
|
+
const top = rect.top + scrollY + 6;
|
|
340
|
+
const left = rect.left + scrollX + 6;
|
|
341
|
+
badge.style.display = "";
|
|
342
|
+
badge.style.top = `${top}px`;
|
|
343
|
+
badge.style.left = `${left}px`;
|
|
344
|
+
tooltip.style.top = `${top + 26}px`;
|
|
345
|
+
tooltip.style.left = `${left}px`;
|
|
346
|
+
}
|
|
347
|
+
function updateAllBadgePositions() {
|
|
348
|
+
for (const { target, badge, tooltip } of trackedBadges) {
|
|
349
|
+
if (!document.body.contains(target)) continue;
|
|
350
|
+
positionBadge(target, badge, tooltip);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
function startBadgeTracking() {
|
|
354
|
+
if (badgePositionRAF !== null) return;
|
|
355
|
+
const tick = () => {
|
|
356
|
+
updateAllBadgePositions();
|
|
357
|
+
badgePositionRAF = requestAnimationFrame(tick);
|
|
358
|
+
};
|
|
359
|
+
badgePositionRAF = requestAnimationFrame(tick);
|
|
360
|
+
if (!outsideClickHandler) {
|
|
361
|
+
outsideClickHandler = (e) => {
|
|
362
|
+
const target = e.target;
|
|
363
|
+
if (target.getAttribute(BADGE_ATTR) === "badge") return;
|
|
364
|
+
for (const entry of trackedBadges) {
|
|
365
|
+
entry.tooltip.style.display = "none";
|
|
366
|
+
entry.badge.style.background = "rgba(0,0,0,0.6)";
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
document.addEventListener("click", outsideClickHandler, { passive: true });
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
function stopBadgeTracking() {
|
|
373
|
+
if (badgePositionRAF !== null) {
|
|
374
|
+
cancelAnimationFrame(badgePositionRAF);
|
|
375
|
+
badgePositionRAF = null;
|
|
376
|
+
}
|
|
377
|
+
if (outsideClickHandler) {
|
|
378
|
+
document.removeEventListener("click", outsideClickHandler);
|
|
379
|
+
outsideClickHandler = null;
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
function addInfoBadge(target, altText) {
|
|
383
|
+
if (target.getAttribute(BADGE_ATTR)) return;
|
|
384
|
+
target.setAttribute(BADGE_ATTR, "1");
|
|
385
|
+
const overlay = getOrCreateOverlay();
|
|
386
|
+
const badge = document.createElement("span");
|
|
387
|
+
Object.assign(badge.style, {
|
|
388
|
+
position: "absolute",
|
|
389
|
+
width: "22px",
|
|
390
|
+
height: "22px",
|
|
391
|
+
borderRadius: "50%",
|
|
392
|
+
background: "rgba(0,0,0,0.6)",
|
|
393
|
+
color: "#fff",
|
|
394
|
+
fontFamily: "sans-serif",
|
|
395
|
+
fontWeight: "bold",
|
|
396
|
+
fontSize: "13px",
|
|
397
|
+
lineHeight: "22px",
|
|
398
|
+
textAlign: "center",
|
|
399
|
+
cursor: "default",
|
|
400
|
+
pointerEvents: "auto",
|
|
401
|
+
boxShadow: "0 1px 4px rgba(0,0,0,0.4)",
|
|
402
|
+
transition: "background 0.15s"
|
|
403
|
+
});
|
|
404
|
+
badge.textContent = "i";
|
|
405
|
+
badge.setAttribute("aria-hidden", "true");
|
|
406
|
+
badge.setAttribute("translate", "no");
|
|
407
|
+
badge.classList.add("notranslate");
|
|
408
|
+
badge.setAttribute(BADGE_ATTR, "badge");
|
|
409
|
+
const tooltip = document.createElement("span");
|
|
410
|
+
Object.assign(tooltip.style, {
|
|
411
|
+
display: "none",
|
|
412
|
+
position: "absolute",
|
|
413
|
+
minWidth: "150px",
|
|
414
|
+
maxWidth: "280px",
|
|
415
|
+
padding: "6px 10px",
|
|
416
|
+
background: "rgba(0,0,0,0.88)",
|
|
417
|
+
color: "#fff",
|
|
418
|
+
fontFamily: "sans-serif",
|
|
419
|
+
fontSize: "12px",
|
|
420
|
+
lineHeight: "1.4",
|
|
421
|
+
borderRadius: "6px",
|
|
422
|
+
pointerEvents: "none",
|
|
423
|
+
wordWrap: "break-word",
|
|
424
|
+
boxShadow: "0 2px 8px rgba(0,0,0,0.3)",
|
|
425
|
+
zIndex: "1"
|
|
426
|
+
});
|
|
427
|
+
tooltip.textContent = altText;
|
|
428
|
+
tooltip.setAttribute("translate", "no");
|
|
429
|
+
tooltip.classList.add("notranslate");
|
|
430
|
+
tooltip.setAttribute(BADGE_ATTR, "tooltip");
|
|
431
|
+
badge.addEventListener("click", () => {
|
|
432
|
+
const isOpen = tooltip.style.display === "block";
|
|
433
|
+
for (const entry of trackedBadges) {
|
|
434
|
+
entry.tooltip.style.display = "none";
|
|
435
|
+
entry.badge.style.background = "rgba(0,0,0,0.6)";
|
|
436
|
+
}
|
|
437
|
+
if (!isOpen) {
|
|
438
|
+
badge.style.background = "rgba(0,0,0,0.85)";
|
|
439
|
+
tooltip.style.display = "block";
|
|
440
|
+
}
|
|
441
|
+
});
|
|
442
|
+
overlay.appendChild(badge);
|
|
443
|
+
overlay.appendChild(tooltip);
|
|
444
|
+
positionBadge(target, badge, tooltip);
|
|
445
|
+
trackedBadges.push({ target, badge, tooltip });
|
|
446
|
+
}
|
|
447
|
+
function removeAllBadges() {
|
|
448
|
+
stopBadgeTracking();
|
|
449
|
+
trackedBadges = [];
|
|
450
|
+
document.getElementById(OVERLAY_ID)?.remove();
|
|
451
|
+
document.getElementById(BADGE_STYLE_ID)?.remove();
|
|
452
|
+
document.querySelectorAll(`[${BADGE_ATTR}]`).forEach((el) => el.removeAttribute(BADGE_ATTR));
|
|
453
|
+
document.querySelectorAll(`[${WRAPPER_ATTR}]`).forEach((wrapper) => {
|
|
454
|
+
const img = wrapper.querySelector("img");
|
|
455
|
+
if (img && wrapper.parentElement) {
|
|
456
|
+
wrapper.parentElement.insertBefore(img, wrapper);
|
|
457
|
+
wrapper.remove();
|
|
458
|
+
}
|
|
459
|
+
});
|
|
460
|
+
}
|
|
461
|
+
function addBadgesToAllImages() {
|
|
462
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
463
|
+
if (!isValidImage(img)) return;
|
|
464
|
+
const alt = img.getAttribute("alt") || img.getAttribute("title");
|
|
465
|
+
if (alt && alt.trim()) {
|
|
466
|
+
addInfoBadge(img, alt.trim());
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
document.querySelectorAll("[data-accessify-bg-alt]").forEach((el) => {
|
|
470
|
+
const alt = el.getAttribute("aria-label");
|
|
471
|
+
if (alt && alt.trim()) {
|
|
472
|
+
addInfoBadge(el, alt.trim());
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
if (trackedBadges.length > 0) startBadgeTracking();
|
|
476
|
+
}
|
|
477
|
+
function removeTitles() {
|
|
478
|
+
document.querySelectorAll('img[data-accessify-title="auto"]').forEach((img) => {
|
|
479
|
+
img.removeAttribute("title");
|
|
480
|
+
img.removeAttribute("data-accessify-title");
|
|
481
|
+
});
|
|
482
|
+
document.querySelectorAll("[data-accessify-bg-alt]").forEach((el) => {
|
|
483
|
+
el.removeAttribute("role");
|
|
484
|
+
el.removeAttribute("aria-label");
|
|
485
|
+
el.removeAttribute("title");
|
|
486
|
+
el.removeAttribute("data-accessify-bg-alt");
|
|
487
|
+
});
|
|
488
|
+
removeAllBadges();
|
|
489
|
+
}
|
|
490
|
+
function scanForBackgroundImages() {
|
|
491
|
+
const found = [];
|
|
492
|
+
const allElements = document.querySelectorAll("*");
|
|
493
|
+
for (const el of allElements) {
|
|
494
|
+
if (el.closest("#accessify-root") || el.closest("accessify-widget")) continue;
|
|
495
|
+
if (el.getAttribute("data-accessify-bg-alt")) continue;
|
|
496
|
+
if (el.offsetWidth < 200 || el.offsetHeight < 150) continue;
|
|
497
|
+
const style = getComputedStyle(el);
|
|
498
|
+
const bg = style.backgroundImage;
|
|
499
|
+
if (!bg || bg === "none") continue;
|
|
500
|
+
const urlMatch = bg.match(/url\(["']?([^"')]+)["']?\)/);
|
|
501
|
+
if (!urlMatch) continue;
|
|
502
|
+
if (urlMatch[1].startsWith("data:image/svg")) continue;
|
|
503
|
+
if (el.getAttribute("role") === "img" && el.getAttribute("aria-label")) continue;
|
|
504
|
+
found.push(el);
|
|
505
|
+
}
|
|
506
|
+
return found;
|
|
507
|
+
}
|
|
508
|
+
function getBackgroundImageUrl(el) {
|
|
509
|
+
const bg = getComputedStyle(el).backgroundImage;
|
|
510
|
+
const match = bg?.match(/url\(["']?([^"')]+)["']?\)/);
|
|
511
|
+
return match ? match[1] : null;
|
|
512
|
+
}
|
|
513
|
+
function applyBgAlt(el, altText) {
|
|
514
|
+
el.setAttribute("role", "img");
|
|
515
|
+
el.setAttribute("aria-label", altText);
|
|
516
|
+
el.setAttribute("title", altText);
|
|
517
|
+
el.setAttribute("data-accessify-bg-alt", "auto");
|
|
518
|
+
if (enabled) addInfoBadge(el, altText);
|
|
519
|
+
}
|
|
520
|
+
async function generateBgAlts() {
|
|
521
|
+
if (!aiService || !enabled) return;
|
|
522
|
+
if (currentAltSiteMode !== "auto") {
|
|
523
|
+
if (window.__ACCESSIFY_DEBUG) {
|
|
524
|
+
console.info("[Accessify] alt-text: siteMode=manual — skipping live generation for background images");
|
|
525
|
+
}
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const bgElements = scanForBackgroundImages();
|
|
529
|
+
for (const el of bgElements) {
|
|
530
|
+
if (!enabled) break;
|
|
531
|
+
const src = getBackgroundImageUrl(el);
|
|
532
|
+
if (!src) continue;
|
|
533
|
+
const cached = await getCachedAltText(src);
|
|
534
|
+
if (cached) {
|
|
535
|
+
applyBgAlt(el, cached);
|
|
536
|
+
continue;
|
|
537
|
+
}
|
|
538
|
+
try {
|
|
539
|
+
const ctx = gatherBgContext(el);
|
|
540
|
+
const alt = await aiService.generateAltText(src, ctx, lang());
|
|
541
|
+
if (alt && enabled) {
|
|
542
|
+
applyBgAlt(el, alt);
|
|
543
|
+
setCachedAltText(src, alt, lang()).catch(() => {
|
|
544
|
+
});
|
|
545
|
+
if (siteKey) persistAltTextToServer(siteKey, proxyUrl, src, alt, lang());
|
|
546
|
+
}
|
|
547
|
+
} catch (err) {
|
|
548
|
+
console.warn("[Accessify] Bg alt-text generation failed:", src, err);
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
function gatherBgContext(el) {
|
|
553
|
+
const parts = [];
|
|
554
|
+
parts.push(`Element: <${el.tagName.toLowerCase()}>`);
|
|
555
|
+
if (el.className) parts.push(`Class: ${el.className}`);
|
|
556
|
+
const text = el.textContent?.trim().slice(0, 100);
|
|
557
|
+
if (text) parts.push(`Overlay text: ${text}`);
|
|
558
|
+
const heading = el.querySelector("h1, h2, h3");
|
|
559
|
+
if (heading?.textContent?.trim()) parts.push(`Heading: ${heading.textContent.trim()}`);
|
|
560
|
+
return parts.join(". ");
|
|
561
|
+
}
|
|
562
|
+
async function generateAll() {
|
|
563
|
+
if (autoGenerating || !enabled || !aiService) return;
|
|
564
|
+
autoGenerating = true;
|
|
565
|
+
const CONCURRENCY = 3;
|
|
566
|
+
const uncached = [];
|
|
567
|
+
for (const img of missingAltImages) {
|
|
568
|
+
if (!enabled) {
|
|
569
|
+
autoGenerating = false;
|
|
570
|
+
return;
|
|
571
|
+
}
|
|
572
|
+
if (processedImages.has(img)) continue;
|
|
573
|
+
const src = getImageSrc(img);
|
|
574
|
+
const cached = await getCachedAltText(src);
|
|
575
|
+
if (cached) {
|
|
576
|
+
applyAltText(img, cached);
|
|
577
|
+
} else {
|
|
578
|
+
uncached.push(img);
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (!enabled) {
|
|
582
|
+
autoGenerating = false;
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
if (!uncached.length) {
|
|
586
|
+
autoGenerating = false;
|
|
587
|
+
return;
|
|
588
|
+
}
|
|
589
|
+
if (currentAltSiteMode !== "auto") {
|
|
590
|
+
if (window.__ACCESSIFY_DEBUG) {
|
|
591
|
+
console.info(
|
|
592
|
+
`[Accessify] alt-text: siteMode=manual — ${uncached.length} image(s) left untouched. Trigger a crawl from the dashboard or enable auto mode.`
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
autoGenerating = false;
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
for (let i = 0; i < uncached.length; i += CONCURRENCY) {
|
|
599
|
+
if (!enabled) {
|
|
600
|
+
autoGenerating = false;
|
|
601
|
+
return;
|
|
602
|
+
}
|
|
603
|
+
const batch = uncached.slice(i, i + CONCURRENCY);
|
|
604
|
+
await Promise.all(batch.map(async (img) => {
|
|
605
|
+
if (!enabled) return;
|
|
606
|
+
try {
|
|
607
|
+
const src = getImageSrc(img);
|
|
608
|
+
const ctx = gatherImageContext(img);
|
|
609
|
+
const alt = await aiService.generateAltText(src, ctx, lang());
|
|
610
|
+
if (alt && enabled) {
|
|
611
|
+
applyAltText(img, alt);
|
|
612
|
+
setCachedAltText(src, alt, lang()).catch(() => {
|
|
613
|
+
});
|
|
614
|
+
if (siteKey) persistAltTextToServer(siteKey, proxyUrl, src, alt, lang());
|
|
615
|
+
}
|
|
616
|
+
} catch (err) {
|
|
617
|
+
console.warn("[Accessify] Alt-text generation failed:", getImageSrc(img), err);
|
|
618
|
+
}
|
|
619
|
+
}));
|
|
620
|
+
}
|
|
621
|
+
autoGenerating = false;
|
|
622
|
+
}
|
|
623
|
+
async function generateSingle(img) {
|
|
624
|
+
if (!aiService || !enabled) return;
|
|
625
|
+
const src = getImageSrc(img);
|
|
626
|
+
const cached = await getCachedAltText(src);
|
|
627
|
+
if (cached) {
|
|
628
|
+
applyAltText(img, cached);
|
|
629
|
+
return;
|
|
630
|
+
}
|
|
631
|
+
if (currentAltSiteMode !== "auto") return;
|
|
632
|
+
try {
|
|
633
|
+
const ctx = gatherImageContext(img);
|
|
634
|
+
const alt = await aiService.generateAltText(src, ctx, lang());
|
|
635
|
+
if (alt && enabled) {
|
|
636
|
+
applyAltText(img, alt);
|
|
637
|
+
setCachedAltText(src, alt, lang()).catch(() => {
|
|
638
|
+
});
|
|
639
|
+
if (siteKey) persistAltTextToServer(siteKey, proxyUrl, src, alt, lang());
|
|
640
|
+
}
|
|
641
|
+
} catch (err) {
|
|
642
|
+
console.warn("[Accessify] Alt-text generation failed:", src, err);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
function tryRegisterImage(img) {
|
|
646
|
+
if (!enabled) return;
|
|
647
|
+
if (!isValidImage(img)) return;
|
|
648
|
+
if (missingAltImages.includes(img)) return;
|
|
649
|
+
function addIfValid() {
|
|
650
|
+
if (!enabled || missingAltImages.includes(img)) return;
|
|
651
|
+
if (img.naturalWidth < 20 || img.naturalHeight < 20) return;
|
|
652
|
+
if (needsAltText(img)) {
|
|
653
|
+
missingAltImages.push(img);
|
|
654
|
+
generateSingle(img);
|
|
655
|
+
} else {
|
|
656
|
+
const alt = img.getAttribute("alt");
|
|
657
|
+
if (alt && alt.trim()) {
|
|
658
|
+
if (!img.getAttribute("title")) {
|
|
659
|
+
img.setAttribute("title", alt);
|
|
660
|
+
img.setAttribute("data-accessify-title", "auto");
|
|
661
|
+
}
|
|
662
|
+
addInfoBadge(img, alt.trim());
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
if (img.complete && img.naturalWidth > 0) addIfValid();
|
|
667
|
+
else img.addEventListener("load", addIfValid, { once: true });
|
|
668
|
+
}
|
|
669
|
+
function activate() {
|
|
670
|
+
if (enabled) return;
|
|
671
|
+
enabled = true;
|
|
672
|
+
document.querySelectorAll('img[data-accessify-alt="auto"]').forEach((img) => {
|
|
673
|
+
if (img.closest("#accessify-root")) return;
|
|
674
|
+
if (!processedImages.has(img)) {
|
|
675
|
+
processedImages.set(img, { generatedAlt: img.getAttribute("alt") || "" });
|
|
676
|
+
missingAltImages.push(img);
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
const missing = scanForMissingAlt();
|
|
680
|
+
for (const img of missing) {
|
|
681
|
+
if (!missingAltImages.includes(img)) {
|
|
682
|
+
missingAltImages.push(img);
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
generateAll();
|
|
686
|
+
generateBgAlts();
|
|
687
|
+
applyTitlesToAllImages();
|
|
688
|
+
addBadgesToAllImages();
|
|
689
|
+
document.querySelectorAll("img").forEach((img) => {
|
|
690
|
+
if (!img.complete) tryRegisterImage(img);
|
|
691
|
+
});
|
|
692
|
+
for (const delay of [2e3, 5e3, 1e4]) {
|
|
693
|
+
setTimeout(() => {
|
|
694
|
+
if (enabled) {
|
|
695
|
+
document.querySelectorAll("img").forEach(tryRegisterImage);
|
|
696
|
+
applyTitlesToAllImages();
|
|
697
|
+
addBadgesToAllImages();
|
|
698
|
+
}
|
|
699
|
+
}, delay);
|
|
700
|
+
}
|
|
701
|
+
domObserver = new MutationObserver((mutations) => {
|
|
702
|
+
for (const m of mutations) {
|
|
703
|
+
for (const node of m.addedNodes) {
|
|
704
|
+
if (node instanceof HTMLImageElement) tryRegisterImage(node);
|
|
705
|
+
else if (node instanceof HTMLElement) {
|
|
706
|
+
node.querySelectorAll("img").forEach(tryRegisterImage);
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
domObserver.observe(document.body, { childList: true, subtree: true });
|
|
712
|
+
}
|
|
713
|
+
function deactivate() {
|
|
714
|
+
enabled = false;
|
|
715
|
+
autoGenerating = false;
|
|
716
|
+
domObserver?.disconnect();
|
|
717
|
+
domObserver = null;
|
|
718
|
+
removeTitles();
|
|
719
|
+
missingAltImages = [];
|
|
720
|
+
}
|
|
721
|
+
autoApplyCachedAltTexts({ siteKey, proxyUrl, lang: initialLang }).catch(() => {
|
|
722
|
+
});
|
|
723
|
+
return {
|
|
724
|
+
id: "alt-text",
|
|
725
|
+
name: () => isDE() ? "Bildbeschreibung" : "Image Description",
|
|
726
|
+
description: isDE() ? "Bildbeschreibungen per Hover anzeigen und fehlende automatisch erzeugen" : "Show image descriptions on hover and auto-generate missing ones",
|
|
727
|
+
icon: "alt-text",
|
|
728
|
+
category: "ai",
|
|
729
|
+
activate,
|
|
730
|
+
deactivate,
|
|
731
|
+
getState: () => ({
|
|
732
|
+
id: "alt-text",
|
|
733
|
+
enabled,
|
|
734
|
+
value: {
|
|
735
|
+
missingCount: missingAltImages.filter((img) => !processedImages.has(img)).length,
|
|
736
|
+
processedCount: processedImages.size
|
|
737
|
+
}
|
|
738
|
+
})
|
|
739
|
+
};
|
|
740
|
+
}
|
|
741
|
+
export {
|
|
742
|
+
autoApplyCachedAltTexts,
|
|
743
|
+
createAltTextModule as default
|
|
744
|
+
};
|
|
745
|
+
//# sourceMappingURL=alt-text-CoS2ISqs.js.map
|