Actualiser custom.js

This commit is contained in:
Mylloon 2023-09-22 12:14:04 +02:00
parent d7923fa1cc
commit fa5ff3c4e9
Signed by: Forgejo
GPG key ID: E72245C752A07631

View file

@ -1,26 +1,33 @@
const loopIter = 3;
const timeoutStartDuration = 200;
window.addEventListener("load", () => {
ocamlhljs();
setTimeout(() => checker(3), 200);
/* ocamlhljs(); */
setTimeout(() => checker(loopIter), timeoutStartDuration);
// When clicking on a link, we recheck images
document.addEventListener("click", () => {
setTimeout(() => checker(3), 200);
setTimeout(() => checker(loopIter), timeoutStartDuration);
});
});
/** Check multiple times because the page takes times to load */
const checker = (count) => {
if (count == 0) {
// Stop the loop
return;
}
if (document.getElementsByTagName("img").length === 0) {
setTimeout(() => checker(count - 1), 200);
} else {
svgObsidianFix();
}
// Fix images
svgObsidianFix();
// Increase the waiting time at each count
setTimeout(
() => checker(count - 1),
timeoutStartDuration * Math.abs(count - loopIter - 1)
);
};
/*! `ocaml` grammar compiled for Highlight.js 11.8.0 */
/** `ocaml` grammar compiled for Highlight.js 11.8.0 */
const ocamlhljs=()=>{var e=(()=>{"use strict";return e=>({name:"OCaml",aliases:["ml"],
keywords:{$pattern:"[a-z_]\\w*!?",
keyword:"and as assert asr begin class constraint do done downto else end exception external for fun function functor if in include inherit! inherit initializer land lazy let lor lsl lsr lxor match method!|10 method mod module mutable new object of open! open or private rec sig struct then to try type val! val virtual when while with parser value",
@ -37,10 +44,12 @@ relevance:0},{begin:/->/}]})})();hljs.registerLanguage("ocaml",e)};
const svgObsidianFix = () => {
for (const item of document.getElementsByTagName("img")) {
console.log(item);
if (!item.src.startsWith("data:image/svg+xml;base64,")) {
// Exclude image who aren't SVG and base64 encoded
break;
}
console.log("aaaa");
/** Convert to grayscale */
const colorToGrayscale = (color) => {
@ -54,7 +63,12 @@ const svgObsidianFix = () => {
canvas.height = image.height;
const ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0);
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const imageData = ctx.getImageData(
0,
0,
Math.floor(canvas.width),
Math.floor(canvas.height)
);
const pixelData = imageData.data;
const colors = [];
@ -79,6 +93,7 @@ const svgObsidianFix = () => {
const totalGrayscale = grayscaleValues.reduce((acc, val) => acc + val, 0);
const averageGrayscale = totalGrayscale / grayscaleValues.length;
console.log(averageGrayscale);
if (averageGrayscale < 128) {
item.style = "filter: invert(1);";
}