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