Skip to content

Commit d6d4a7a

Browse files
committed
tries to fix infinite loop during analysis
1 parent 1e4f191 commit d6d4a7a

File tree

6 files changed

+45
-10
lines changed

6 files changed

+45
-10
lines changed

src/content-script/content.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,12 @@ getTabId(communicationChannel).then(async (tabId) => {
142142

143143
const specMutex: Mutex = new Mutex('spec-mutex');
144144

145+
const lastPageRedirectStorage = new BackgroundIndexedDBObjectStorage<number>(
146+
IndexedDBDatabases.LastPageRedirect,
147+
IndexedDBDatabases.LastPageRedirect,
148+
communicationChannel
149+
);
150+
145151
const crawler: Crawler = new Crawler(
146152
browserContext,
147153
pageStorage,
@@ -155,7 +161,8 @@ getTabId(communicationChannel).then(async (tabId) => {
155161
specMutex,
156162
analysisElementXPathStorage,
157163
pageAnalysisStorage,
158-
config
164+
config,
165+
lastPageRedirectStorage
159166
);
160167

161168
communicationChannel.setMessageListener(async function (message: Message) {

src/content-script/crawler/Crawler.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { PageAnalysis } from './PageAnalysis';
2828
import { Message } from '../../shared/comm/Message';
2929
import { Command } from '../../shared/comm/Command';
3030
import { Config } from '../../shared/config';
31+
import { ElementAnalysis } from './ElementAnalysis';
3132

3233
export 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
}

src/content-script/crawler/ElementAnalysis.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,8 @@ export class ElementAnalysis {
7070
public getTabId(): string {
7171
return this.tabId;
7272
}
73+
74+
public setPathToElement(pathToElement: string): void {
75+
this.pathToElement = pathToElement;
76+
}
7377
}

src/content-script/storage/BackgroundIndexedDBObjectStorage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export class BackgroundIndexedDBObjectStorage<Type> implements ObjectStorage<Typ
1010
classe no construtor também
1111
*/
1212
constructor(
13-
private dbName: string,
14-
private storeName: string,
13+
protected dbName: string,
14+
protected storeName: string,
1515
private communicationChannel: CommunicationChannel,
1616
private typeConstructor?: ClassConstructor<unknown>,
1717
private getCommand: Command = Command.GetValueFromBackgroundIndexedDB

src/content-script/storage/ElementAnalysisStorage.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { CommunicationChannel } from '../../shared/comm/CommunicationChannel';
66
import { BackgroundIndexedDBObjectStorage } from './BackgroundIndexedDBObjectStorage';
77
import { IndexedDBDatabases } from '../../shared/storage/IndexedDBDatabases';
88
import { Config } from '../../shared/config';
9+
import { Command } from '../../shared/comm/Command';
10+
import { Message } from '../../shared/comm/Message';
911

1012
// TODO Trocar o nome da classe
1113
export class ElementAnalysisStorage extends BackgroundIndexedDBObjectStorage<ElementAnalysis> {

src/shared/storage/IndexedDBDatabases.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ export enum IndexedDBDatabases {
55
ElementAnalysis = 'element-analysis',
66
PageAnalysis = 'page-analysis',
77
RunningStatus = 'running-status',
8+
LastPageRedirect = 'last-page-redirect',
89
}

0 commit comments

Comments
 (0)