altchecker/dist/alt-text-checker.min.js

1 line
6.1 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

!function(){"use strict";const e="atc",t={success:{bg:"lime",outline:"#000",text:"#000"},error:{bg:"#EB0000",outline:"#fff",text:"#fff"},warning:{bg:"orange",outline:"#000",text:"#000"},excluded:{bg:"#e0e0e0",outline:"#666",text:"#333"},info:{bg:"yellow",outline:"#000",text:"#000"}},n={success:"OK",error:"FEHLER",warning:"WARNUNG",excluded:"AUSGESCHLOSSEN",info:"HINWEIS"},o={error:!0,warning:!0},l={"font-family":"sans-serif","font-size":"small",padding:"5px 8px",outline:"2px dotted #000","outline-offset":"-3px",border:"0",cursor:"default","z-index":"2147483647"};function r(){return"alt-text-checker:excluded:"+location.href}function i(){try{const e=localStorage.getItem(r());return e?JSON.parse(e):[]}catch(e){return[]}}function a(e){if(null===e)return;const t=i(),n=t.indexOf(e);-1===n?t.push(e):t.splice(n,1),function(e){try{localStorage.setItem(r(),JSON.stringify(e))}catch(e){}}(t)}function c(e,t,n,o,l){if(t=t||document,o=o||new Set,l=l||new Set,o.has(t))return 0;o.add(t);let r=0;return Array.from(t.querySelectorAll(e)).forEach(e=>{l.has(e)||(l.add(e),n(e),r+=1)}),Array.from(t.querySelectorAll("*")).forEach(t=>{t.shadowRoot&&(r+=c(e,t.shadowRoot,n,o,l))}),r}function s(e){return!(!e.parentNode||"A"!==e.parentNode.tagName&&!s(e.parentNode))}function u(o,r,i,a){const c=t[r],s=!!(a=a||{}).global,u=document.createElement(s?"label":"button");u.className=`${e} ${e}-${r}`.trim();const d=Object.assign({},l,{"background-color":c.bg,"outline-color":c.outline,color:c.text});if("absolute"===a.position){const e=o.getBoundingClientRect();d.position="absolute",d.left=`${e.left+window.scrollX}px`,d.top=`${e.top+window.scrollY}px`}var f;a.extraStyle&&Object.assign(d,a.extraStyle),u.setAttribute("style",(f=d,Object.keys(f).map(e=>`${e}:${f[e]}`).join(";")));const g=a.icon||"";if(s)u.textContent=g?`${g} ${i}`:i;else{u.appendChild(document.createTextNode(g));const t=document.createElement("span");t.className=`${e}-badge`,t.textContent=n[r]||"",t.style.marginLeft="4px",t.style.fontWeight="700",u.appendChild(t);const o=document.createElement("span");if(o.className=`${e}-details`,o.appendChild(function(e){const t=document.createDocumentFragment();return e.forEach(e=>{if("string"==typeof e)t.appendChild(document.createTextNode(e));else if(void 0!==e.code){const n=document.createElement("code");n.textContent=e.code,t.appendChild(n)}else if(void 0!==e.strike){const n=document.createElement("s");n.textContent=e.strike,t.appendChild(n)}}),t}(i)),o.style.color="inherit",o.style.marginLeft="4px",u.appendChild(o),!a.alwaysOpen){o.style.display="none";const t=()=>{0===document.querySelectorAll(`.${e}-toggle:checked`).length&&(o.style.display="inline")},n=()=>{0===document.querySelectorAll(`.${e}-toggle:checked`).length&&(o.style.display="none")};u.addEventListener("focus",t),u.addEventListener("mouseover",t),u.addEventListener("blur",n),u.addEventListener("mouseout",n)}}if(a.onClick&&u.addEventListener("click",e=>{e.preventDefault(),e.stopPropagation(),a.onClick()}),s&&a.withToggle){u.appendChild(document.createTextNode(" — alle Details anzeigen"));const t=document.createElement("input");t.type="checkbox",t.className=`${e}-toggle`,t.style.margin="0 0 0 4px",t.style.verticalAlign="baseline",t.addEventListener("change",function(){document.querySelectorAll(`.${e}-details`).forEach(e=>{e.style.display=this.checked?"inline":"none"})}),u.appendChild(t)}return s&&(document.querySelectorAll(`.${e}-global`).forEach(e=>e.remove()),u.className+=` ${e}-global`),"absolute"===a.position?document.body.appendChild(u):(a.insertInto||o.parentNode).appendChild(u),u}function d(e,t){const n=t.getAttribute(e);if(!n)return null;const o=n.split(" ").map(e=>e.trim()).filter(Boolean),l=[];return o.forEach(n=>{const o=document.getElementById(n);if(!o)return;u(o,"warning",[`Referenziert von [${e}="`,{code:t.id||"…"},'"]'],{icon:"⚠️",position:"absolute"});const r=o.textContent.trim();r&&l.push(r)}),l.length?l.join(" "):null}!function t(){c(`.${e}`,document,e=>e.remove());const n=i(),l=new Map,r={ok:0,error:0,warning:0,info:0,excluded:0,total:0};c("a[alt], button[alt], label[alt]",document,e=>{u(e,"error",[`<${e.tagName.toLowerCase()}> mit ungültigem `,{code:`alt="${e.getAttribute("alt")}"`}],{icon:"✖",position:"absolute"})}),c('img, [role="img"]',document,e=>{r.total+=1;const i=function(e,t){const n=function(e){if("IMG"===e.tagName)return e.currentSrc||e.src||null;const t=e.closest?e.closest("a[href]"):null;if(t&&t.href)return`role-img:${t.href}`;const n=e.getAttribute("aria-label");return n?`role-img:${n}`:e.className?`role-img:class:${e.className}`:null}(e);if(null===n)return null;const o=t.get(n)||0;return t.set(n,o+1),`${n}#${o}`}(e,l);if(function(e,t){return null!==e&&t.includes(e)}(i,n))return r.excluded+=1,void u(e,"excluded",["Ausgeschlossen (manuell als dekorativ markiert)"],{icon:"⦸",position:"absolute",onClick:()=>{a(i),t()}});const c=e.hasAttribute("alt"),f=c?e.getAttribute("alt"):null,g=e.hasAttribute("aria-label"),m=d("aria-describedby",e),p=d("aria-labelledby",e),h=e.hasAttribute("title"),b=e.hasAttribute("longdesc"),y="true"===e.getAttribute("aria-hidden"),x=g||null!==m||null!==p,$=s(e)?"Bild in Link: ":"",A=[];let w,E;if(h&&A.push(" ",{code:`title="${e.getAttribute("title")}"`}),b&&A.push(" ",{code:`longdesc="${e.getAttribute("longdesc")}"`}),c){const e=0===f.trim().length;w=[$,{code:`alt="${f}"`},...A],E=e?"warning":"success"}else if(y&&!x)w=[$,{code:'aria-hidden="true"'},...A],E="info";else if(x){w=[h?"":$,{strike:"alt"}," fehlt, aber ARIA-Name vorhanden: ",{code:g?e.getAttribute("aria-label"):p||m},...A],E="warning"}else w=[$,{strike:"alt"}," fehlt",...A],E="error";r["success"===E?"ok":E]+=1;u(e,E,w,{icon:"success"===E?"✔":"error"===E?"✖":"info"===E?"":"⚠️",position:"absolute",onClick:o[E]?()=>{a(i),t()}:void 0})});const f=r.error>0?"error":r.warning>0?"warning":"success",g=r.total?`Bilder: ${r.total} · ✔ ${r.ok} OK · ✖ ${r.error} Fehler · ⚠️ ${r.warning} Warnung · ${r.info} Hinweis · ⦸ ${r.excluded} ausgeschlossen`:"Keine Bilder auf dieser Seite gefunden.";u(document.body,r.total?f:"info",g,{global:!0,withToggle:!!r.total,extraStyle:{position:"fixed",top:"0",left:"0"}})}()}();