improve image fallback
parent
527486f866
commit
f9ca8437ea
|
@ -106,16 +106,25 @@ function onAbbreviation() {
|
||||||
}
|
}
|
||||||
|
|
||||||
function onImageFallback() {
|
function onImageFallback() {
|
||||||
|
document.addEventListener('load', onLoad, { capture: true })
|
||||||
document.addEventListener('error', onError, { capture: true })
|
document.addEventListener('error', onError, { capture: true })
|
||||||
|
|
||||||
|
const attribute = 'data-origin'
|
||||||
|
|
||||||
|
function onLoad(event: Event) {
|
||||||
|
if (!isHTMLImageElement(event.target)) return
|
||||||
|
event.target.removeAttribute(attribute)
|
||||||
|
}
|
||||||
|
|
||||||
function onError(event: Event) {
|
function onError(event: Event) {
|
||||||
if (event.target === null) return
|
if (!isHTMLImageElement(event.target)) return
|
||||||
const target = event.target as HTMLImageElement
|
const origin = event.target.getAttribute(attribute)
|
||||||
if (target.tagName !== 'IMG') return
|
if (origin) event.target.src = origin
|
||||||
const attribute = 'data-origin'
|
event.target.removeAttribute(attribute)
|
||||||
const origin = target.getAttribute(attribute)
|
}
|
||||||
if (origin) target.src = origin
|
|
||||||
target.removeAttribute(attribute)
|
function isHTMLImageElement(target: EventTarget | null): target is HTMLImageElement {
|
||||||
target.removeEventListener('error', onError)
|
if (target === null) return false
|
||||||
|
return (target as HTMLElement).tagName === 'IMG'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue