Closed reubenfirmin closed 9 months ago
Hello, use addAfterInsertHook
before accessing the DOM element:
image(src = "https://cdn.britannica.com/08/1108-050-416D8AF2/Guyana-map-features-locator.jpg") {
setAttribute("crossorigin", "anonymous")
addAfterInsertHook {
getElement()?.onerror = { _, _, _, _, _ ->
console.log("error loading image, removing crossorigin attribute")
this.removeAttribute("crossorigin")
}
}
}
Ah indeed - that does work. Thanks
This image will render:
Now add this to it, and it will fail to render:
Other images (which have Access-Control-Allow-Origin headers configured correctly) will work with this attribute, e.g. https://miro.medium.com/v2/resize:fit:1000/1*zD10J5eFnTSjy-ZdA2_Krw.jpeg
When the image fails to load, the browser dumps out:
In javascript, (apparently, according to gpt) you can do this to catch the load error:
Similar ability in kvision would be useful. There is no onerror available on Element, and putting a timeout around this to give it a change to be rendered and become an HTMLElement apparently misses the error:
Specifically what I'd want to do is catch the error, drop the crossorigin attribute so that it renders, and mark it with some kind of warning to the user.