fix: fix wrong dimensions of user pfp in screenshot export

closes #53
This commit is contained in:
Pionxzh
2023-03-09 21:36:27 +08:00
parent 6ddbe78743
commit ad679cd107
+21
View File
@@ -28,6 +28,18 @@ export async function exportToPng(fileNameFormat: string) {
// hide bottom bar
thread.children[thread.children.length - 1].classList.add('hidden')
const avatarEls = Array.from(document.querySelectorAll('img[alt]:not([aria-hidden])'))
// disabled the avatar srcset
// fix https://github.com/pionxzh/chatgpt-exporter/issues/53
// seems related to https://github.com/niklasvh/html2canvas/issues/2218
avatarEls.forEach((el) => {
const srcset = el.getAttribute('srcset')
if (srcset) {
el.setAttribute('data-srcset', srcset)
el.removeAttribute('srcset')
}
})
await sleep(100)
const canvas = await html2canvas(thread, {
@@ -47,6 +59,15 @@ export async function exportToPng(fileNameFormat: string) {
}
})
// restore the avatar srcset
avatarEls.forEach((el) => {
const srcset = el.getAttribute('data-srcset')
if (srcset) {
el.setAttribute('srcset', srcset)
el.removeAttribute('data-srcset')
}
})
const dataUrl = canvas.toDataURL('image/png', 1)
.replace(/^data:image\/[^;]/, 'data:application/octet-stream')
const fileName = getFileNameWithFormat(fileNameFormat, 'png')