@@ -16,7 +16,7 @@ import { GraphStorage } from '../storage/GraphStorage';
1616import { ElementInteractionGraph } from '../graph/ElementInteractionGraph' ;
1717import { ElementInteraction } from '../../content-script/crawler/ElementInteraction' ;
1818import { Graph } from '../../content-script/graph/Graph' ;
19- import { getURLWithoutQueries , sleep } from '../../content-script/util' ;
19+ import { getURLasString , sleep } from '../../content-script/util' ;
2020import { ElementAnalysisStatus } from '../../content-script/crawler/ElementAnalysisStatus' ;
2121import { ElementAnalysisStorage as ElementAnalysisStorageBackground } from '../../background-script/storage/ElementAnalysisStorage' ;
2222import { PageAnalysisStorage as PageAnalysisStorageBackground } from '../../background-script/storage/PageAnalysisStorage' ;
@@ -30,6 +30,7 @@ import { IndexedDBDatabases } from '../../shared/storage/IndexedDBDatabases';
3030import { deleteDB } from 'idb' ;
3131import { PageAnalysis } from '../../content-script/crawler/PageAnalysis' ;
3232import { PageAnalysisStatus } from '../../content-script/crawler/PageAnalysisStatus' ;
33+ import { Config } from '../../shared/config' ;
3334
3435export class ExtensionFacade {
3536 private openedTabs : Array < Tab > ;
@@ -45,12 +46,14 @@ export class ExtensionFacade {
4546 private tabAjaxCalls : Map < string , string [ ] > ;
4647 private specMutex : Mutex ;
4748 private initialHost : string | null ;
49+ private config : Config ;
4850
4951 constructor (
5052 extension : Extension ,
5153 communicationChannel : CommunicationChannel ,
5254 inMemoryDatabase : InMemoryDatabase ,
53- openedTabsLimit : number
55+ openedTabsLimit : number ,
56+ config : Config ,
5457 ) {
5558 this . openedTabs = [ ] ;
5659 this . extension = extension ;
@@ -65,35 +68,14 @@ export class ExtensionFacade {
6568 this . tabAjaxCalls = new Map < string , string [ ] > ( ) ;
6669 this . specMutex = new Mutex ( 'spec-mutex' ) ;
6770 this . initialHost = null ;
71+ this . config = config ;
6872 }
6973
7074 public async setup ( ) : Promise < void > {
7175 let _this = this ;
7276 await this . deleteIDBDatabases ( ) ;
73- this . extension . setBrowserActionListener (
74- ExtensionBrowserAction . ExtensionIconClicked ,
75- async function ( tab : Tab ) {
76- if ( ! _this . extensionIsEnabled ) {
77- if ( ! tab . getURL ( ) ) {
78- throw new Error ( "Tab has no URL" ) ;
79- }
80- //@ts -ignore
81- _this . initialHost = tab . getURL ( ) . host ;
82- _this . extensionIsEnabled = true ;
83- _this . openedTabs . push ( tab ) ;
84- _this . tabFinished . set ( tab . getId ( ) , false ) ;
85- _this . openedTabsCounter ++ ;
86- // while(_this.tabStillHasAjaxToComplete(tab)){
87- // await sleep(5);
88- // }
89- _this . sendOrderToCrawlTab ( tab , true ) ;
90- } else {
91- _this . extension . reload ( ) ;
92- }
93- }
94- ) ;
95-
96- this . listenToAjaxCalls ( ) ;
77+
78+ //this.listenToAjaxCalls();
9779
9880 this . communicationChannel . setMessageListener ( async function (
9981 message : Message ,
@@ -107,8 +89,41 @@ export class ExtensionFacade {
10789 ) {
10890 if ( responseCallback ) responseCallback ( new Message ( [ ] , sender . getId ( ) ) ) ;
10991 }
110-
111- if ( message . includesAction ( Command . SetValueInMemoryDatabase ) ) {
92+ console . log ( message ) ;
93+ if ( message . includesAction ( Command . Start ) ) {
94+ if ( ! _this . extensionIsEnabled ) {
95+ chrome . tabs . query ( { active : true , currentWindow : true } , function ( tabs ) {
96+ if ( ! tabs [ 0 ] . id || ! tabs [ 0 ] . url ) {
97+ return false ;
98+ }
99+ var activeTab = new Tab ( tabs [ 0 ] . id . toString ( ) , new URL ( tabs [ 0 ] . url ) ) ;
100+ if ( ! activeTab . getURL ( ) ) {
101+ throw new Error ( "Tab has no URL" ) ;
102+ }
103+ //@ts -ignore
104+ _this . initialHost = activeTab . getURL ( ) . host ;
105+ _this . setExtensionRunningStatus ( true ) ;
106+ _this . openedTabs . push ( activeTab ) ;
107+ _this . tabFinished . set ( activeTab . getId ( ) , false ) ;
108+ _this . openedTabsCounter ++ ;
109+ // while(_this.tabStillHasAjaxToComplete(tab)){
110+ // await sleep(5);
111+ // }
112+ _this . sendOrderToCrawlTab ( activeTab , true ) ;
113+ } ) ;
114+
115+ } else {
116+ _this . extension . reload ( ) ;
117+ }
118+ } else if ( message . includesAction ( Command . GetRunningStatus ) ) {
119+ const data = message . getExtra ( ) ;
120+ if ( data ) {
121+ if ( responseCallback ) {
122+ responseCallback ( new Message ( [ ] , _this . extensionIsEnabled ) ) ;
123+ }
124+ }
125+ }
126+ else if ( message . includesAction ( Command . SetValueInMemoryDatabase ) ) {
112127 const data = message . getExtra ( ) ;
113128 if ( data ) {
114129 const key = data . key ;
@@ -265,7 +280,7 @@ export class ExtensionFacade {
265280 const concordiaFiles = new FeatureFileGenerator ( ) ;
266281 concordiaFiles . generate ( spec ) ;
267282
268- _this . extensionIsEnabled = false ;
283+ _this . setExtensionRunningStatus ( false ) ;
269284 }
270285 } else if ( message . includesAction ( Command . ProcessUnload ) ) {
271286 _this . tabLocked . set ( sender . getId ( ) , true ) ;
@@ -342,9 +357,9 @@ export class ExtensionFacade {
342357 Variant
343358 ) ;
344359 const elementInteractionStorage = new ElementInteractionStorage ( featureStorage , variantStorage ) ;
345- const elementAnalysisStorage = new ElementAnalysisStorage ( communicationChannel ) ;
360+ const elementAnalysisStorage = new ElementAnalysisStorage ( communicationChannel , this . config ) ;
346361 const graphStorage = new GraphStorage ( this . inMemoryDatabase ) ;
347- return new ElementInteractionGraph ( 'tab-' + id , elementInteractionStorage , elementAnalysisStorage , graphStorage ) ;
362+ return new ElementInteractionGraph ( 'tab-' + id , elementInteractionStorage , elementAnalysisStorage , graphStorage , this . config ) ;
348363 }
349364
350365 private async setInteractionElementAsAnalyzed ( interaction : ElementInteraction < HTMLElement > , sender : Tab ) : Promise < void > {
@@ -353,7 +368,8 @@ export class ExtensionFacade {
353368 interaction . getElementSelector ( ) ,
354369 interaction . getPageUrl ( ) ,
355370 ElementAnalysisStatus . Done ,
356- sender . getId ( )
371+ sender . getId ( ) ,
372+ this . config
357373 ) ;
358374 const elementAnalysisStorage = new ElementAnalysisStorageBackground ( this . inMemoryDatabase ) ;
359375 await elementAnalysisStorage . set ( elementAnalysis . getId ( ) , elementAnalysis ) ;
@@ -364,7 +380,8 @@ export class ExtensionFacade {
364380 elementPath ,
365381 url ,
366382 ElementAnalysisStatus . Done ,
367- sender . getId ( )
383+ sender . getId ( ) ,
384+ this . config
368385 ) ;
369386 const elementAnalysisStorage = new ElementAnalysisStorageBackground ( this . inMemoryDatabase ) ;
370387 await elementAnalysisStorage . set ( elementAnalysis . getId ( ) , elementAnalysis ) ;
@@ -376,8 +393,8 @@ export class ExtensionFacade {
376393 PageAnalysisStatus . Done ,
377394 ) ;
378395
379- const pageAnalysisStorage = new PageAnalysisStorageBackground ( this . inMemoryDatabase ) ;
380- await pageAnalysisStorage . set ( getURLWithoutQueries ( pageAnalysis . getUrl ( ) ) , pageAnalysis ) ;
396+ const pageAnalysisStorage = new PageAnalysisStorageBackground ( this . inMemoryDatabase , this . config ) ;
397+ await pageAnalysisStorage . set ( getURLasString ( pageAnalysis . getUrl ( ) , this . config ) , pageAnalysis ) ;
381398 }
382399
383400 public openNewTab ( url : URL ) : void {
@@ -434,7 +451,8 @@ export class ExtensionFacade {
434451 element ,
435452 url ,
436453 ElementAnalysisStatus . Done ,
437- sender . getId ( )
454+ sender . getId ( ) ,
455+ this . config
438456 ) ;
439457 const elementAnalysisStorage = new ElementAnalysisStorageBackground ( this . inMemoryDatabase ) ;
440458 await elementAnalysisStorage . set ( analysis . getId ( ) , analysis ) ;
@@ -554,4 +572,10 @@ export class ExtensionFacade {
554572 }
555573 }
556574
575+ private async setExtensionRunningStatus ( status : boolean ) {
576+ this . extensionIsEnabled = status ;
577+ const runningStatusStorage = new IndexedDBObjectStorage < boolean > ( IndexedDBDatabases . RunningStatus , IndexedDBDatabases . RunningStatus ) ;
578+ runningStatusStorage . set ( 'status' , status ) ;
579+ }
580+
557581}
0 commit comments