2023-09-22 12:14:04 +02:00
const loopIter = 3 ;
const timeoutStartDuration = 200 ;
2023-09-22 11:41:36 +02:00
window . addEventListener ( "load" , ( ) => {
2023-09-22 12:14:04 +02:00
/* ocamlhljs(); */
setTimeout ( ( ) => checker ( loopIter ) , timeoutStartDuration ) ;
2023-09-22 11:55:44 +02:00
// When clicking on a link, we recheck images
document . addEventListener ( "click" , ( ) => {
2023-09-22 12:14:04 +02:00
setTimeout ( ( ) => checker ( loopIter ) , timeoutStartDuration ) ;
2023-09-22 11:55:44 +02:00
} ) ;
2023-09-22 11:41:36 +02:00
} ) ;
2023-09-22 11:55:44 +02:00
/** Check multiple times because the page takes times to load */
2023-09-22 11:41:36 +02:00
const checker = ( count ) => {
if ( count == 0 ) {
2023-09-22 12:14:04 +02:00
// Stop the loop
2023-09-22 11:41:36 +02:00
return ;
}
2023-09-22 12:14:04 +02:00
// Fix images
svgObsidianFix ( ) ;
// Increase the waiting time at each count
setTimeout (
( ) => checker ( count - 1 ) ,
timeoutStartDuration * Math . abs ( count - loopIter - 1 )
) ;
2023-09-22 11:41:36 +02:00
} ;
2023-09-22 12:14:04 +02:00
/** `ocaml` grammar compiled for Highlight.js 11.8.0 */
2023-09-22 11:41:36 +02:00
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" ,
built _in : "array bool bytes char exn|5 float int int32 int64 list lazy_t|5 nativeint|5 string unit in_channel out_channel ref" ,
literal : "true false" } , illegal : /\/\/|>>/ , contains : [ { className : "literal" ,
begin : "\\[(\\|\\|)?\\]|\\(\\)" , relevance : 0 } , e . COMMENT ( "\\(\\*" , "\\*\\)" , {
contains : [ "self" ] } ) , { className : "symbol" , begin : "'[A-Za-z_](?!')[\\w']*" } , {
className : "type" , begin : "`[A-Z][\\w']*" } , { className : "type" ,
begin : "\\b[A-Z][\\w']*" , relevance : 0 } , { begin : "[a-z_]\\w*'[\\w']*" , relevance : 0
} , e . inherit ( e . APOS _STRING _MODE , { className : "string" , relevance : 0
} ) , e . inherit ( e . QUOTE _STRING _MODE , { illegal : null } ) , { className : "number" ,
begin : "\\b(0[xX][a-fA-F0-9_]+[Lln]?|0[oO][0-7_]+[Lln]?|0[bB][01_]+[Lln]?|[0-9][0-9_]*([Lln]|(\\.[0-9_]*)?([eE][-+]?[0-9_]+)?)?)" ,
relevance : 0 } , { begin : /->/ } ] } ) } ) ( ) ; hljs . registerLanguage ( "ocaml" , e ) } ;
const svgObsidianFix = ( ) => {
for ( const item of document . getElementsByTagName ( "img" ) ) {
2023-09-22 12:14:04 +02:00
console . log ( item ) ;
2023-09-22 11:41:36 +02:00
if ( ! item . src . startsWith ( "data:image/svg+xml;base64," ) ) {
// Exclude image who aren't SVG and base64 encoded
break ;
}
2023-09-22 12:14:04 +02:00
console . log ( "aaaa" ) ;
2023-09-22 11:41:36 +02:00
/** Convert to grayscale */
const colorToGrayscale = ( color ) => {
return 0.3 * color . r + 0.59 * color . g + 0.11 * color . b ;
} ;
/** Extract color using canvas2d */
const extractColors = ( image ) => {
const canvas = document . createElement ( "canvas" ) ;
canvas . width = image . width ;
canvas . height = image . height ;
const ctx = canvas . getContext ( "2d" ) ;
ctx . drawImage ( image , 0 , 0 ) ;
2023-09-22 12:14:04 +02:00
const imageData = ctx . getImageData (
0 ,
0 ,
Math . floor ( canvas . width ) ,
Math . floor ( canvas . height )
) ;
2023-09-22 11:41:36 +02:00
const pixelData = imageData . data ;
const colors = [ ] ;
for ( let i = 0 ; i < pixelData . length ; i += 4 ) {
if ( pixelData [ i + 3 ] > 0 ) {
colors . push ( {
r : pixelData [ i ] ,
g : pixelData [ i + 1 ] ,
b : pixelData [ i + 2 ] ,
} ) ;
}
}
return colors ;
} ;
// Extract colors
const colors = extractColors ( item ) ;
// Calculate the average grayscale value
const grayscaleValues = colors . map ( colorToGrayscale ) ;
const totalGrayscale = grayscaleValues . reduce ( ( acc , val ) => acc + val , 0 ) ;
const averageGrayscale = totalGrayscale / grayscaleValues . length ;
2023-09-22 12:14:04 +02:00
console . log ( averageGrayscale ) ;
2023-09-22 11:41:36 +02:00
if ( averageGrayscale < 128 ) {
item . style = "filter: invert(1);" ;
}
}
} ;