(sitemapUrls)
| 11 | } |
| 12 | |
| 13 | async load(sitemapUrls) { |
| 14 | const getSuggestion = async () => { |
| 15 | const currentUrl = window.location.href |
| 16 | // Function to calculate the Levenshtein distance between two strings |
| 17 | function levenshteinDistance(a, b) { |
| 18 | const m = a.length |
| 19 | const n = b.length |
| 20 | const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)) |
| 21 | for (let i = 0; i <= m; i++) { |
| 22 | dp[i][0] = i |
| 23 | } |
| 24 | for (let j = 0; j <= n; j++) { |
| 25 | dp[0][j] = j |
| 26 | } |
| 27 | for (let i = 1; i <= m; i++) { |
| 28 | for (let j = 1; j <= n; j++) { |
| 29 | const cost = a[i - 1] === b[j - 1] ? 0 : 1 |
| 30 | dp[i][j] = Math.min(dp[i - 1][j] + 1, dp[i][j - 1] + 1, dp[i - 1][j - 1] + cost) |
| 31 | } |
| 32 | } |
| 33 | return dp[m][n] |
| 34 | } |
| 35 | |
| 36 | try { |
| 37 | const responses = await Promise.all(sitemapUrls.split(" ").map(async url => await fetch(url))) |
| 38 | const sitemap = await Promise.all(responses.map(async response => await response.text())) |
| 39 | const urls = sitemap.join("\n").split("\n") |
| 40 | |
| 41 | let closestMatch = null |
| 42 | let minDistance = Infinity |
| 43 | |
| 44 | urls.forEach(url => { |
| 45 | const trimmedUrl = url.trim() |
| 46 | if (trimmedUrl) { |
| 47 | const distance = levenshteinDistance(currentUrl, trimmedUrl) |
| 48 | if (distance < minDistance) { |
| 49 | minDistance = distance |
| 50 | closestMatch = trimmedUrl |
| 51 | } |
| 52 | } |
| 53 | }) |
| 54 | |
| 55 | return closestMatch |
| 56 | } catch (error) { |
| 57 | console.error("Failed to fetch the sitemap:", error) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | const closestMatch = await getSuggestion() |
| 62 | const outputDiv = document.getElementById("helpfulNotFound") |
| 63 | if (closestMatch) outputDiv.innerHTML = `Maybe the url you want is<br><a href="${closestMatch}">${closestMatch}</a>?` |
| 64 | else outputDiv.parentElement.textContent = "No similar pages found." |
| 65 | } |
| 66 | } |
no test coverage detected