@@ -141,6 +141,15 @@ async function onLoad() {
141141
142142 if ( ! await hasConsumable ( ) ) {
143143 console . log ( "(Ophirofox) No consumable found." ) ;
144+ if ( path . startsWith ( "/Search/Result" ) ) {
145+ const auto_open_link = await getAutoOpenOption ( ) ;
146+ if ( auto_open_link ) {
147+ const numberOfResul = document . querySelector ( '.resultOperations-count' ) . textContent ;
148+ if ( numberOfResul === '1' ) {
149+ await readWhenOnlyOneResult ( ) ;
150+ }
151+ }
152+ }
144153 return ;
145154 }
146155
@@ -172,4 +181,49 @@ function ophirofoxRealoadOnExpired() {
172181 }
173182}
174183
175- onLoad ( ) ;
184+ async function waitForElement ( selector , callback , attempts = 0 , maxAttempts = 10 ) {
185+ const element = document . querySelector ( selector ) ;
186+ if ( element ) {
187+ callback ( element ) ;
188+ return true ; // Indique que l'élément a été trouvé
189+ } else if ( attempts < maxAttempts ) {
190+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ; // Attendre 0.5 seconde avant de réessayer
191+ return waitForElement ( selector , callback , attempts + 1 , maxAttempts ) ;
192+ } else {
193+ console . error ( 'Element not found after maximum attempts' ) ;
194+ return false ; // Indique que l'élément n'a pas été trouvé
195+ }
196+ }
197+
198+ function readWhenOnlyOneResult ( ) {
199+ const observer = new MutationObserver ( async ( ) => {
200+ const found = await waitForElement ( 'a.docList-links' , ( linkElement ) => {
201+ console . log ( "linkElement" , linkElement ) ;
202+ linkElement . click ( ) ;
203+ } ) ;
204+ if ( found ) {
205+ observer . disconnect ( ) ; // Arrêter l'observation une fois l'élément trouvé et cliqué
206+ }
207+ } ) ;
208+ observer . observe ( document . body , { childList : true , subtree : true } ) ;
209+ }
210+
211+ const DEFAULT_SETTINGS = {
212+ auto_open_link : false
213+ } ;
214+ const OPHIROFOX_SETTINGS_KEY = "ophirofox_settings" ;
215+
216+ async function getAutoOpenOption ( ) {
217+ const key = OPHIROFOX_SETTINGS_KEY ;
218+ return new Promise ( ( accept ) => {
219+ chrome . storage . local . get ( [ key ] , function ( result ) {
220+ if ( result . hasOwnProperty ( key ) ) {
221+ current_settings = JSON . parse ( result [ key ] ) ;
222+ accept ( current_settings . auto_open_link ) ;
223+ }
224+ else accept ( DEFAULT_SETTINGS . auto_open_link ) ;
225+ } ) ;
226+ } ) ;
227+ }
228+
229+ onLoad ( ) ;
0 commit comments