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

1 line
6.0 KiB
JavaScript

!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:"INFO"},o={"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 l(){return"alt-text-checker:excluded:"+location.href}function r(){try{const e=localStorage.getItem(l());return e?JSON.parse(e):[]}catch(e){return[]}}function i(e){if(null===e)return;const t=r(),n=t.indexOf(e);-1===n?t.push(e):t.splice(n,1),function(e){try{localStorage.setItem(l(),JSON.stringify(e))}catch(e){}}(t)}function a(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+=a(e,t.shadowRoot,n,o,l))}),r}function c(e){return!(!e.parentNode||"A"!==e.parentNode.tagName&&!c(e.parentNode))}function s(l,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({},o,{"background-color":c.bg,"outline-color":c.outline,color:c.text});if("absolute"===a.position){const e=l.getBoundingClientRect();d.position="absolute",d.left=`${e.left+window.scrollX}px`,d.top=`${e.top+window.scrollY}px`}var g;a.extraStyle&&Object.assign(d,a.extraStyle),u.setAttribute("style",(g=d,Object.keys(g).map(e=>`${e}:${g[e]}`).join(";")));const f=a.icon||"";if(s)u.textContent=f?`${f} ${i}`:i;else{u.appendChild(document.createTextNode(f));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||l.parentNode).appendChild(u),u}function u(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;s(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(){a(`.${e}`,document,e=>e.remove());const n=r(),o=new Map,l={ok:0,error:0,warning:0,excluded:0,total:0};a("a[alt], button[alt], label[alt]",document,e=>{s(e,"error",[`<${e.tagName.toLowerCase()}> mit ungültigem `,{code:`alt="${e.getAttribute("alt")}"`}],{icon:"✖",position:"absolute"})}),a('img, [role="img"]',document,e=>{l.total+=1;const r=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,o);if(function(e,t){return null!==e&&t.includes(e)}(r,n))return l.excluded+=1,void s(e,"excluded",["Ausgeschlossen (manuell als dekorativ markiert)"],{icon:"⦸",position:"absolute",onClick:()=>{i(r),t()}});const a=e.hasAttribute("alt"),d=a?e.getAttribute("alt"):null,g=e.hasAttribute("aria-label"),f=u("aria-describedby",e),m=u("aria-labelledby",e),p=e.hasAttribute("title"),h=e.hasAttribute("longdesc"),b="true"===e.getAttribute("aria-hidden"),y=g||null!==f||null!==m,x=c(e)?"Bild in Link: ":"",$=[];let A,w;if(p&&$.push(" ",{code:`title="${e.getAttribute("title")}"`}),h&&$.push(" ",{code:`longdesc="${e.getAttribute("longdesc")}"`}),a){const e=0===d.trim().length;A=[x,{code:`alt="${d}"`},...$],w=e?"warning":"success"}else if(b&&!y)A=[x,{code:'aria-hidden="true"'},...$],w="warning";else if(y){A=[p?"":x,{strike:"alt"}," fehlt, aber ARIA-Name vorhanden: ",{code:g?e.getAttribute("aria-label"):m||f},...$],w="warning"}else A=[x,{strike:"alt"}," fehlt",...$],w="error";l["success"===w?"ok":w]+=1,s(e,w,A,{icon:"success"===w?"✔":"error"===w?"✖":"⚠️",position:"absolute",onClick:()=>{i(r),t()}})});const d=l.error>0?"error":l.warning>0?"warning":"success",g=l.total?`Bilder: ${l.total} · ✔ ${l.ok} OK · ✖ ${l.error} Fehler · ⚠️ ${l.warning} Warnung · ⦸ ${l.excluded} ausgeschlossen`:"Keine Bilder auf dieser Seite gefunden.";s(document.body,l.total?d:"info",g,{global:!0,withToggle:!!l.total,extraStyle:{position:"fixed",top:"0",left:"0"}})}()}();