fix: make domain matching resilient to redirects and www / non-www mismatches
This commit is contained in:
+21
-3
@@ -41,6 +41,9 @@ export async function extractNavLinks(targetUrl: string): Promise<NavLink[]> {
|
||||
await page.goto(targetUrl, { waitUntil: "domcontentloaded", timeout: 30000 });
|
||||
await page.waitForTimeout(1200); // dropdown'ların render edilmesi için
|
||||
|
||||
// Yönlendirme sonrası nihai URL'i al (örn: ayris.tech -> www.ayris.tech)
|
||||
const finalUrl = new URL(page.url());
|
||||
|
||||
// Navbar içindeki tüm <a> etiketlerini çek
|
||||
const rawLinks = await page.evaluate(() => {
|
||||
const selectors = [
|
||||
@@ -73,10 +76,18 @@ export async function extractNavLinks(targetUrl: string): Promise<NavLink[]> {
|
||||
if (SKIP_SCHEMES.some((s) => href.startsWith(s))) continue;
|
||||
|
||||
let parsed: URL;
|
||||
try { parsed = new URL(href, targetUrl); } catch { continue; }
|
||||
try {
|
||||
parsed = new URL(href, finalUrl.toString());
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Sadece aynı domain
|
||||
if (parsed.hostname !== base.hostname) continue;
|
||||
// Sadece aynı domain veya yönlendirilen domain (örn: www'li / www'siz eşleşmesi)
|
||||
const isDomainMatch =
|
||||
isSameDomain(parsed.hostname, base.hostname) ||
|
||||
isSameDomain(parsed.hostname, finalUrl.hostname);
|
||||
|
||||
if (!isDomainMatch) continue;
|
||||
|
||||
const path = parsed.pathname.toLowerCase();
|
||||
|
||||
@@ -99,6 +110,13 @@ export async function extractNavLinks(targetUrl: string): Promise<NavLink[]> {
|
||||
}
|
||||
}
|
||||
|
||||
function isSameDomain(hostA: string, hostB: string): boolean {
|
||||
const getBase = (h: string) => h.replace(/^www\./i, "").toLowerCase();
|
||||
const baseA = getBase(hostA);
|
||||
const baseB = getBase(hostB);
|
||||
return baseA === baseB || baseA.endsWith("." + baseB) || baseB.endsWith("." + baseA);
|
||||
}
|
||||
|
||||
function pathToLabel(pathname: string): string {
|
||||
const last = pathname.split("/").filter(Boolean).pop() || "anasayfa";
|
||||
return last.replace(/[-_]/g, " ").replace(/\b\w/g, (c) => c.toUpperCase());
|
||||
|
||||
Reference in New Issue
Block a user