From fa5ff3c4e9db16b8a123d2cc0fa2cbc904bbb78f Mon Sep 17 00:00:00 2001 From: Anri Kennel Date: Fri, 22 Sep 2023 12:14:04 +0200 Subject: [PATCH] Actualiser custom.js --- custom.js | 35 +++++++++++++++++++++++++---------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/custom.js b/custom.js index 9121bf9..58bb7b7 100644 --- a/custom.js +++ b/custom.js @@ -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);"; }