Wait for inbound images to load

This commit is contained in:
Andrew Ferrazzutti 2021-03-28 04:23:07 -04:00
parent 00e7226efe
commit 5a33500765
1 changed files with 30 additions and 6 deletions

View File

@ -211,9 +211,35 @@ class MautrixController {
// TODO Use "Inner" or not? // TODO Use "Inner" or not?
messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText messageData.text = messageElement.querySelector(".mdRGT07MsgTextInner")?.innerText
} else if (messageElement.classList.contains("mdRGT07Image")) { } else if (messageElement.classList.contains("mdRGT07Image")) {
// TODO Probably need a MutationObserver to wait for image to load. const img = messageElement.querySelector(".mdRGT07MsgImg > img")
// Should also catch "#_chat_message_image_failure" if (img) {
messageData.image_url = messageElement.querySelector(".mdRGT07MsgImg > img")?.src if (img.src.startsWith("blob:")) {
messageData.image_url = img.src
} else {
let resolve
// TODO Should reject on "#_chat_message_image_failure"
let observer = new MutationObserver((changes) => {
for (const change of changes) {
if (change.target.src.startsWith("blob:")) {
observer.disconnect()
observer = null
resolve(change.target.src)
return
}
}
})
observer.observe(img, { attributes: true, attributeFilter: ["src"] })
messageData.image_url = await new Promise((realResolve, reject) => {
resolve = realResolve
setTimeout(() => {
if (observer) {
observer.disconnect()
resolve(img.src)
}
}, 5000)
})
}
}
} }
return messageData return messageData
} }
@ -223,7 +249,6 @@ class MautrixController {
let observer let observer
let msgID = -1 let msgID = -1
let resolve let resolve
let reject
const resolveMessage = () => { const resolveMessage = () => {
observer.disconnect() observer.disconnect()
@ -267,9 +292,8 @@ class MautrixController {
document.querySelector("#_chat_room_msg_list"), document.querySelector("#_chat_room_msg_list"),
{ childList: true }) { childList: true })
return new Promise((realResolve, realReject) => { return new Promise((realResolve, reject) => {
resolve = realResolve resolve = realResolve
reject = realReject
// TODO Handle a timeout better than this // TODO Handle a timeout better than this
setTimeout(() => { setTimeout(() => {
if (observer) { if (observer) {