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", () => { window.addEventListener("load", () => {
ocamlhljs(); /* ocamlhljs(); */
setTimeout(() => checker(3), 200); setTimeout(() => checker(loopIter), timeoutStartDuration);
// When clicking on a link, we recheck images // When clicking on a link, we recheck images
document.addEventListener("click", () => { document.addEventListener("click", () => {
setTimeout(() => checker(3), 200); setTimeout(() => checker(loopIter), timeoutStartDuration);
}); });
}); });
/** Check multiple times because the page takes times to load */ /** Check multiple times because the page takes times to load */
const checker = (count) => { const checker = (count) => {
if (count == 0) { if (count == 0) {
// Stop the loop
return; return;
} }
if (document.getElementsByTagName("img").length === 0) { // Fix images
setTimeout(() => checker(count - 1), 200); svgObsidianFix();
} else {
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"], const ocamlhljs=()=>{var e=(()=>{"use strict";return e=>({name:"OCaml",aliases:["ml"],
keywords:{$pattern:"[a-z_]\\w*!?", 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", 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 = () => { const svgObsidianFix = () => {
for (const item of document.getElementsByTagName("img")) { for (const item of document.getElementsByTagName("img")) {
console.log(item);
if (!item.src.startsWith("data:image/svg+xml;base64,")) { if (!item.src.startsWith("data:image/svg+xml;base64,")) {
// Exclude image who aren't SVG and base64 encoded // Exclude image who aren't SVG and base64 encoded
break; break;
} }
console.log("aaaa");
/** Convert to grayscale */ /** Convert to grayscale */
const colorToGrayscale = (color) => { const colorToGrayscale = (color) => {
@ -54,7 +63,12 @@ const svgObsidianFix = () => {
canvas.height = image.height; canvas.height = image.height;
const ctx = canvas.getContext("2d"); const ctx = canvas.getContext("2d");
ctx.drawImage(image, 0, 0); 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 pixelData = imageData.data;
const colors = []; const colors = [];
@ -79,6 +93,7 @@ const svgObsidianFix = () => {
const totalGrayscale = grayscaleValues.reduce((acc, val) => acc + val, 0); const totalGrayscale = grayscaleValues.reduce((acc, val) => acc + val, 0);
const averageGrayscale = totalGrayscale / grayscaleValues.length; const averageGrayscale = totalGrayscale / grayscaleValues.length;
console.log(averageGrayscale);
if (averageGrayscale < 128) { if (averageGrayscale < 128) {
item.style = "filter: invert(1);"; item.style = "filter: invert(1);";
} }