blobify pdfs, may help on bad browsers

This commit is contained in:
Mylloon 2025-02-07 16:19:45 +01:00
parent 237d76bf4d
commit 9033c5c13d
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -89,6 +89,56 @@ const svgChangeTheme = (mode) => {
}
};
/**
* Replace base64 urls of embeded PDFs into blob
*/
const blobifyPdfs = () => {
const pdfContentType = "application/pdf";
for (const item of document.getElementsByTagName("embed")) {
if (!item.src.startsWith(`data:${pdfContentType};base64,`)) {
// Exclude embed who are not PDFs encoded via base64
continue;
}
/**
* Convert Base64 data to a blob
* @param {String} b64Data Encoded data
* @param {Number} sliceSize Size of the slices
* @returns Blob representing the data
*/
const base64ToBlob = async (b64Data, sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (
let offset = 0;
offset < byteCharacters.length;
offset += sliceSize
) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
return new Blob(byteArrays, { type: pdfContentType });
};
base64ToBlob(item.src.split(",")[1]).then((blob) => {
item.src = URL.createObjectURL(blob);
});
}
};
window.addEventListener("DOMContentLoaded", () => {
// Turn Base64 PDFs into blobs
blobifyPdfs();
});
window.addEventListener("load", () => {
// Fix SVG images
svgChangeTheme(