@@ -18,12 +18,12 @@ export async function extractSources(content: HTMLElement, format: any, data: {
1818 res = "" ;
1919 i = 1 ;
2020
21- for ( const { open, close, selector, extractionType, paginationSelector, content : msgContent } of data . selectors ) {
21+ for ( const { open, close, selector, extractionType, paginationSelector, content : msgContent , scope : scopeType } of data . selectors ) {
2222 open && await safeExecute ( await selectAndClick ( open , content ) ) ;
2323
2424 switch ( extractionType ) {
2525 case 'list' :
26- res = await safeExecute ( await extractFromList ( format , content , selector ?? msgContent ) ) as unknown as string ;
26+ res = await safeExecute ( await extractFromList ( format , content , selector ?? msgContent , scopeType ) ) as unknown as string ;
2727 break ;
2828 case 'tile-list' :
2929 res = await safeExecute ( await extractFromTileList ( format , content , selector ) ) as unknown as string ;
@@ -103,24 +103,27 @@ function extractFromLinks(links: any[] | NodeListOf<any>, format: (arg0: any) =>
103103 * @param selectorOrContent
104104 * @returns {Promise<string> }
105105 */
106- async function extractFromList ( format : any , content : HTMLElement , selectorOrContent : { selector : any ; scope : any ; } ) : Promise < string > {
106+ async function extractFromList ( format : any , content : HTMLElement , selectorOrContent : { selector : any ; scope : any ; } , scopeType : string ) : Promise < string > {
107107 let res = '' ;
108108 let i = 1 ;
109109
110110 const selector = typeof selectorOrContent === "object"
111111 ? selectorOrContent . selector
112112 : selectorOrContent ;
113113
114- const scope = typeof selectorOrContent === "object"
115- ? selectorOrContent . scope
116- : "document" ;
114+ const scope = scopeType === "content"
115+ ? "content"
116+ : typeof selectorOrContent === "object"
117+ ? selectorOrContent . scope
118+ : "document" ;
117119
118120 const qs = scope === "document"
119121 ? document . querySelectorAll ( selector )
120122 : content . querySelectorAll ( selector ) ;
121123
122124 // console.log(content, scope, qs)
123125 for ( const tile of qs ) {
126+ // console.log("THE TILE : ", tile)
124127 res += await formatSources ( i , format , tile ) ;
125128 i ++ ;
126129 }
@@ -162,11 +165,13 @@ async function extractFromTileList(format: any, content: HTMLElement, selector:
162165 * @returns {Promise<string> }
163166 */
164167export async function formatSources ( i : string | number , format : ( arg0 : any ) => string , tile : Element ) : Promise < string > {
165- const elt : HTMLElement = tile . querySelector ( "div.default " ) as HTMLElement //Perplexity
168+ const elt : HTMLElement = tile . querySelector ( "a " ) as HTMLElement //Perplexity
166169 || tile ;
167170
171+ const title : HTMLElement = tile . querySelector ( '.font-display' ) as HTMLElement
172+
168173 const text = "(" + i + ") "
169- + format ( elt . innerText
174+ + format ( title . innerText
170175 . replaceAll ( "\n" , " " )
171176 . replaceAll ( '"' , '' )
172177 . replace ( / ^ \d + / , "" ) // Removes numbers at the beginning
@@ -206,53 +211,16 @@ export async function formatSources(i: string | number, format: (arg0: any) => s
206211
207212 // Export content
208213 let res = "- " ;
214+ // console.log(tile)
209215 // @ts -ignore TODO
210- if ( tile && tile . href )
216+ // if (tile && tile.href)
211217 // @ts -ignore TODO
212- res += formatLink ( tile . href , text ) + "\n" ;
213- else {
214- const url : HTMLElement = await safeExecute ( extractYoutubeLink ( tile as HTMLElement ) ) as unknown as HTMLElement ;
215- res += url
216- ? formatLink ( url , text ) + "\n"
217- : text + "\n" ;
218- }
218+ res += ( elt . href ? formatLink ( elt . href , text ) : text ) + "\n" ;
219+ // else {
220+ // const url: HTMLElement = await safeExecute(extractYoutubeLink(tile as HTMLElement)) as unknown as HTMLElement;
221+ // res += url
222+ // ? formatLink(url, text) + "\n"
223+ // : text + "\n";
224+ // }
219225 return res ;
220226}
221-
222-
223- // async function extractSourcesOld(msgContent, searchResults, res, format) {
224- // const buttonsInCard = msgContent[2].querySelectorAll("button");
225- // for (const btn of buttonsInCard) {
226- // if (btn.textContent.toLowerCase() === "view all search results") {
227- // // Open modal
228- // btn.click();
229- // await sleep(0); // Needed to wait for the modal to open (even if it's 0!)
230- //
231- // // Export sources and all search results, put correct index in front of each link
232- // let i = 1;
233- // let allResults = "**All search results:**";
234- //
235- // const dialogLinks = Array.from(document.querySelectorAll("[role='dialog'] a"));
236- // const p2Array = Array.from(searchResults);
237- // dialogLinks.forEach((link) => {
238- // // If the link is in the sources, add it to the sources with the correct index
239- // if (p2Array.find((elt) => elt.getAttribute("href") === link.getAttribute("href"))) {
240- // res += "\n- " + format(link.outerHTML).replace("[", `[(${i}) `);
241- // }
242- //
243- // // Add the link to the all search results with the correct index
244- // allResults += "\n- " + format(link.outerHTML).replace("[", `[(${i}) `);
245- // i++;
246- // });
247- //
248- // // Append all search results after the sources
249- // res += "\n\n" + allResults;
250- //
251- // // Close modal
252- // document.querySelectorAll("[role='dialog'] [type='button']").forEach((btn) => {
253- // if (btn.textContent.toLowerCase() === "close") btn.click();
254- // });
255- // }
256- // }
257- // return res;
258- // }
0 commit comments