@@ -5,19 +5,19 @@ const Constants = require('../../utils/constants');
55class OrganicResult {
66 /** @type {string } */
77 title ;
8-
8+
99 /** @type {string } */
1010 description ;
11-
11+
1212 /** @type {string } */
1313 url ;
1414
1515 /** @type {boolean } */
1616 is_sponsored ;
17-
17+
1818 /** @type {{ high_res: string; low_res: string; } } */
1919 favicons ;
20-
20+
2121 constructor ( data ) {
2222 this . title = data . title ;
2323 this . description = data . description ;
@@ -46,23 +46,23 @@ class OrganicResults {
4646
4747 const titles = $ ( Constants . SELECTORS . TITLE )
4848 . map ( ( _i , el ) => {
49- const is_ad =
50- el . parent . attribs . style == '-webkit-line-clamp:2' ||
49+ const is_ad =
50+ Reflect . has ( el . parent ?. parent ?. parent ?. attribs || { } , 'data-rw' ) ||
5151 ( ! is_mobile && el . parent . attribs . class . startsWith ( 'vdQmEd' ) ) ;
52-
52+
5353 // Ignore ad titles if parse_ads is false
5454 if ( ! parse_ads && is_ad )
5555 return null ;
5656
57- return is_mobile ?
57+ return is_mobile ?
5858 $ ( el ) . text ( ) . trim ( ) : $ ( el ) . find ( 'h3' ) . text ( ) . trim ( ) || $ ( el ) . find ( 'a > div > span' ) . first ( ) . text ( ) . trim ( ) ;
5959 } ) . get ( ) ;
6060
6161 const descriptions = $ ( Constants . SELECTORS . DESCRIPTION )
6262 . map ( ( _i , el ) => {
63- const is_ad = el . parent . attribs . class == 'w1C3Le' ||
63+ const is_ad = Reflect . has ( el . parent ?. parent ?. parent ?. attribs || { } , 'data-text-ad' ) ||
6464 ( ! is_mobile && ! Object . keys ( el . parent . attribs ) . length ) ;
65-
65+
6666 // Ignore ad descriptions if parse_ads is false
6767 if ( ! parse_ads && is_ad ) {
6868 return null ;
@@ -75,31 +75,25 @@ class OrganicResults {
7575
7676 const urls = $ ( is_mobile ? Constants . SELECTORS . URL : `${ Constants . SELECTORS . TITLE } > a` )
7777 . map ( ( _i , el ) => {
78- const is_ad = el . parent ?. parent ?. attribs ?. class ?. startsWith ( 'vdQmEd' ) ;
79-
80- /**
81- * Since the selector for URLs is the same as the one for titles on desktop,
82- * we need to check if the element is an ad. If we're parsing the mobile page,
83- * then ads are simply stripped out of the results.
84- */
85- if ( ! is_mobile && ! parse_ads && is_ad ) {
78+ const is_ad = ! ! $ ( el ) . attr ( 'data-rw' ) || el . parent ?. parent ?. attribs ?. class ?. startsWith ( 'vdQmEd' ) ;
79+
80+ if ( ! parse_ads && is_ad )
8681 return null ;
87- }
8882
8983 return $ ( el ) . attr ( 'href' ) ;
9084 } ) . get ( ) ;
91-
85+
9286 // Refine results
9387 if ( titles . length < urls . length && titles . length < descriptions . length ) {
9488 urls . shift ( ) ;
9589 }
96-
90+
9791 if ( urls . length > titles . length ) {
9892 urls . shift ( ) ;
9993 }
100-
94+
10195 const is_innacurate_data = descriptions . length < urls . slice ( 1 ) . length ;
102-
96+
10397 urls . forEach ( ( item , index ) => {
10498 // Why YouTube? Because video results usually don't have a description.
10599 if ( item . includes ( 'm.youtube.com' ) && is_innacurate_data ) {
@@ -108,23 +102,23 @@ class OrganicResults {
108102 index -- ;
109103 }
110104 } ) ;
111-
105+
112106 const results = [ ] ;
113-
107+
114108 for ( let i = 0 ; i < titles . length ; i ++ ) {
115109 const title = titles [ i ] ;
116110 const description = descriptions [ i ] ;
117-
111+
118112 let url = urls [ i ] ;
119113
120114 // Some results have a different URL format (AMP and ad results).
121115 if ( url ?. startsWith ( '/aclk' ) || url ?. startsWith ( '/amp/s' ) ) {
122116 url = `${ Constants . URLS . W_GOOGLE } ${ url . substring ( 1 ) } ` ;
123117 }
124-
118+
125119 const high_res_favicon = `${ Constants . URLS . FAVICONKIT } /${ new URL ( url || Constants . URLS . W_GOOGLE ) . hostname } /192` ;
126120 const low_res_favicon = `${ Constants . URLS . W_GOOGLE } s2/favicons?sz=64&domain_url=${ new URL ( url || Constants . URLS . W_GOOGLE ) . hostname } ` ;
127-
121+
128122 if ( titles [ i ] && descriptions [ i ] && urls [ i ] ) {
129123 results . push ( new OrganicResult ( {
130124 title,
@@ -138,7 +132,7 @@ class OrganicResults {
138132 } ) ) ;
139133 }
140134 }
141-
135+
142136 return results ;
143137 }
144138}
0 commit comments