refactor: improve search extraction with edge case handling
This commit is contained in:
@@ -488,7 +488,7 @@ function extractFacebookMarketplaceData(
|
||||
let marketplaceData: FacebookMarketplaceSearch | null = null;
|
||||
|
||||
// Find the script containing the require data with marketplace_search
|
||||
for (const script of scripts as unknown as HTMLScriptElement[]) {
|
||||
for (const script of Array.from(scripts) as HTMLScriptElement[]) {
|
||||
const scriptText = script.textContent;
|
||||
if (!scriptText) continue;
|
||||
|
||||
@@ -520,7 +520,7 @@ function extractFacebookMarketplaceData(
|
||||
for (const getData of paths) {
|
||||
try {
|
||||
const result = getData();
|
||||
if (result && isRecord(result) && result.feed_units?.edges) {
|
||||
if (result && isRecord(result) && result.feed_units?.edges?.length > 0) {
|
||||
marketplaceData = result as FacebookMarketplaceSearch;
|
||||
break;
|
||||
}
|
||||
@@ -533,17 +533,19 @@ function extractFacebookMarketplaceData(
|
||||
}
|
||||
|
||||
// Also check for direct marketplace_search in the parsed data
|
||||
if (parsed.marketplace_search && isRecord(parsed.marketplace_search) && parsed.marketplace_search.feed_units?.edges) {
|
||||
marketplaceData = parsed.marketplace_search as FacebookMarketplaceSearch;
|
||||
if (parsed.marketplace_search && isRecord(parsed.marketplace_search)) {
|
||||
const searchData = parsed.marketplace_search as FacebookMarketplaceSearch;
|
||||
if (searchData.feed_units?.edges?.length > 0) {
|
||||
marketplaceData = searchData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
// Ignore parsing errors for other scripts
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!marketplaceData?.feed_units?.edges) {
|
||||
if (!marketplaceData?.feed_units?.edges?.length) {
|
||||
console.warn("No marketplace data found in HTML response");
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user