Advertisement
Guest User

Untitled

a guest
Feb 1st, 2024
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. // Settings 
  3.  
  4. const baseUrl = 'https://domain.com/'; // Your website root
  5. const sitemapURL = 'https://domain.com/sitemap.xml'; // Your sitemap URL
  6. const notFoundTitleKeyword = 'Not Found'; // A keyword that is present in your "Not Found" page title. This might be "404", "Uh oh", "We can't find.." etc.
  7. const ignoreWords = ['blog', 'feed', 'guide']; // Common words to ignore. These should be low-value words that are present in many of your URLs.
  8. const minWordLength = 3; // Set the minimum word length in characters. This ignores low-value words like "to", "a", "the", etc. 
  9.  
  10. // Only edit below here if you know what you're doing:
  11.  
  12. document.addEventListener('DOMContentLoaded', function() {
  13.     if (document.title.includes(notFoundTitleKeyword)) {
  14.         async function fetchSitemap(url) {
  15.             try {
  16.                 const response = await fetch(url);
  17.                 const text = await response.text();
  18.                 const parser = new DOMParser();
  19.                 const xmlDoc = parser.parseFromString(text, "text/xml");
  20.                 const urls = xmlDoc.querySelectorAll('loc');
  21.                 return Array.from(urls).map(node => node.textContent);
  22.             } catch (error) {
  23.                 console.error('Error fetching sitemap:', error);
  24.                 return [];
  25.             }
  26.         }
  27.         function processCurrentURL() {
  28.             const path = window.location.pathname;
  29.             let segments = path.split('/').filter(segment => segment.length > 0 && !ignoreWords.includes(segment));
  30.             segments = segments.map(segment => {
  31.                 if (segment.match(/^\d+$/)) return null;
  32.                 return segment.replace(/-/g, ' ');
  33.             }).filter(segment => segment != null && segment.length > minWordLength);
  34.             return segments.join(' ').split(' ').filter(word => word.length > minWordLength);
  35.         }
  36.         function findClosestMatch(currentKeywords, urls) {
  37.           let bestMatch = '';
  38.           let bestScore = 0;
  39.           urls.forEach(url => {
  40.               const urlParts = url.replace('https://colonyroofers.com/', '').split('/').filter(part => part.length);
  41.               let score = 0;
  42.               currentKeywords.forEach(keyword => {
  43.                   if(urlParts.some(part => part.includes(keyword))) score++;
  44.               });
  45.               if(score > bestScore) {
  46.                   bestScore = score;
  47.                   bestMatch = url;
  48.               }
  49.           });
  50.           return bestMatch;
  51.         }
  52.         async function redirectToClosestMatch() {
  53.           const sitemapUrls = await fetchSitemap(sitemapURL);
  54.           if (sitemapUrls.length === 0) {
  55.               console.log('Sitemap is empty or could not be fetched.');
  56.               return;
  57.           }
  58.           const currentKeywords = processCurrentURL();
  59.           const closestMatch = findClosestMatch(currentKeywords, sitemapUrls);
  60.           if(closestMatch) {
  61.               console.log('Redirecting to:', closestMatch);
  62.               window.location.replace(closestMatch);
  63.           } else {
  64.               console.log('No close match found.');
  65.           }
  66.         }
  67.         redirectToClosestMatch();
  68.     }
  69.     else {
  70.         console.log('Incorrect page title:', document.title);
  71.     }
  72. });
  73. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement