@@ -43,16 +43,52 @@ function onBlockLoad () {
4343 modal . style . position = "fixed" ;
4444 modal . style . width = "70%" ;
4545 modal . style . left = "15%" ;
46+ modal . style . height = "100dvh" ;
4647 } else {
4748 modal . style . position = old_position ;
4849 modal . style . width = old_width ;
4950 modal . style . left = old_left ;
51+ modal . style . height = "85dvh" ;
5052 }
5153 } ;
5254 }
5355
56+ globalThis . compareText = ( search_phrase , page_label ) => {
57+ var iframe = document . querySelector ( "#pdf-viewer" ) . iframe ;
58+ var innerDoc = ( iframe . contentDocument ) ? iframe . contentDocument : iframe . contentWindow . document ;
59+
60+ var query_selector = (
61+ "#viewer > div[data-page-number='" +
62+ page_label +
63+ "'] > div.textLayer > span"
64+ ) ;
65+ var page_spans = innerDoc . querySelectorAll ( query_selector ) ;
66+ for ( var i = 0 ; i < page_spans . length ; i ++ ) {
67+ var span = page_spans [ i ] ;
68+ if (
69+ span . textContent . length > 4 &&
70+ (
71+ search_phrase . includes ( span . textContent ) ||
72+ span . textContent . includes ( search_phrase )
73+ )
74+ ) {
75+ span . innerHTML = "<span class='highlight selected'>" + span . textContent + "</span>" ;
76+ } else {
77+ // if span is already highlighted, remove it
78+ if ( span . querySelector ( ".highlight" ) ) {
79+ span . innerHTML = span . textContent ;
80+ }
81+ }
82+ }
83+ }
84+
85+ // Sleep function using Promise and setTimeout
86+ function sleep ( ms ) {
87+ return new Promise ( resolve => setTimeout ( resolve , ms ) ) ;
88+ }
89+
5490 // Function to open modal and display PDF
55- globalThis . openModal = ( event ) => {
91+ globalThis . openModal = async ( event ) => {
5692 event . preventDefault ( ) ;
5793 var target = event . currentTarget ;
5894 var src = target . getAttribute ( "data-src" ) ;
@@ -66,8 +102,8 @@ function onBlockLoad () {
66102 if ( current_src != src ) {
67103 pdfViewer . setAttribute ( "src" , src ) ;
68104 }
69- pdfViewer . setAttribute ( "phrase" , phrase ) ;
70- pdfViewer . setAttribute ( "search" , search ) ;
105+ // pdfViewer.setAttribute("phrase", phrase);
106+ // pdfViewer.setAttribute("search", search);
71107 pdfViewer . setAttribute ( "page" , page ) ;
72108
73109 var scrollableDiv = document . getElementById ( "chat-info-panel" ) ;
@@ -80,6 +116,10 @@ function onBlockLoad () {
80116 info_panel . style . display = "none" ;
81117 }
82118 scrollableDiv . scrollTop = 0 ;
119+
120+ /* search for text inside PDF page */
121+ await sleep ( 500 ) ;
122+ compareText ( search , page ) ;
83123 }
84124
85125 globalThis . assignPdfOnclickEvent = ( ) => {
@@ -93,7 +133,6 @@ function onBlockLoad () {
93133 var created_modal = document . getElementById ( "pdf-viewer" ) ;
94134 if ( ! created_modal ) {
95135 createModal ( ) ;
96- console . log ( "Created modal" )
97136 }
98137
99138}
0 commit comments