@@ -28,6 +28,7 @@ import { PageAnalysis } from './PageAnalysis';
2828import { Message } from '../../shared/comm/Message' ;
2929import { Command } from '../../shared/comm/Command' ;
3030import { Config } from '../../shared/config' ;
31+ import { ElementAnalysis } from './ElementAnalysis' ;
3132
3233export class Crawler {
3334 private lastPageKey : string ;
@@ -45,7 +46,8 @@ export class Crawler {
4546 private specMutex : Mutex ,
4647 private analysisElementXPathStorage : ObjectStorage < string > ,
4748 private pageAnalysisStorage : PageAnalysisStorage ,
48- private config : Config
49+ private config : Config ,
50+ private lastPageRedirectStorage : ObjectStorage < number >
4951 ) {
5052 this . lastPageKey = 'last-page' ;
5153 }
@@ -76,12 +78,31 @@ export class Crawler {
7678 const pageAnalysisStatus = await this . pageAnalysisStorage . getPageAnalysisStatus ( this . browserContext . getUrl ( ) ) ;
7779 if ( pageAnalysisStatus != PageAnalysisStatus . Pending ) {
7880 window . location . href = lastUnanalyzed . getPageUrl ( ) . href ;
79- // verifica se o href esta salvo como ultima url redirecionado
80- // se nao, salvar novo href como "ultima url redirecionada", com contador 1
81- // se sim, incrementar o contador
82- // se contador >= 5 (exemplo)
83- // salvar pagina com status DONE
84- // salvar elemento como analisado (para ele nao ser mais localizado no lastUnanalyzed)
81+ const amountRedirect = await this . lastPageRedirectStorage . get ( getURLasString ( lastUnanalyzed . getPageUrl ( ) , this . config ) ) ;
82+ if ( amountRedirect != null ) {
83+ const newAmount = amountRedirect + 1 ;
84+ await this . lastPageRedirectStorage . set ( getURLasString ( lastUnanalyzed . getPageUrl ( ) , this . config ) , newAmount ) ;
85+ if ( newAmount >= 5 ) {
86+ const pageAnalysis = new PageAnalysis ( this . browserContext . getUrl ( ) , PageAnalysisStatus . Done ) ;
87+ await this . pageAnalysisStorage . set ( getURLasString ( this . browserContext . getUrl ( ) , this . config ) , pageAnalysis ) ;
88+ const elementAnalysis = new ElementAnalysis (
89+ document . body ,
90+ lastUnanalyzed . getPageUrl ( ) ,
91+ ElementAnalysisStatus . Done ,
92+ this . browserContext . getTabId ( ) ,
93+ this . config
94+ ) ;
95+ const elementSelector = lastUnanalyzed . getElementSelector ( ) ;
96+ if ( elementSelector ) {
97+ elementAnalysis . setPathToElement ( elementSelector ) ;
98+ } else {
99+ throw new Error ( 'ElementSelector was null' ) ;
100+ }
101+ }
102+ } else {
103+ await this . lastPageRedirectStorage . set ( getURLasString ( lastUnanalyzed . getPageUrl ( ) , this . config ) , 1 ) ;
104+ }
105+
85106 return false ;
86107 }
87108 }
0 commit comments