Last active
July 8, 2024 13:58
-
-
Save estevecastells/5f99e7077601ef516f1592bc487c136a to your computer and use it in GitHub Desktop.
Fetch URL to see if a link exists in given URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function checkLink(url, domain) { | |
try { | |
var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); | |
var content = response.getContentText(); | |
// Create a regular expression to match links to the specified domain | |
var regex = new RegExp('href=["\']https?:\/\/(www\.)?' + domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'i'); | |
// Check if the content contains a link matching the regex | |
return regex.test(content); | |
} catch (error) { | |
// If there's an error (e.g., invalid URL), return false | |
return false; | |
} | |
} | |
function extractLinks(url, domain) { | |
try { | |
var response = UrlFetchApp.fetch(url, {muteHttpExceptions: true}); | |
var content = response.getContentText(); | |
// Escape special characters in the domain | |
var escapedDomain = domain.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
// Improved regex to match links more precisely | |
var regex = new RegExp('<a\\s+(?:[^>]*?\\s+)?href=(["\'])(https?:\\/\\/(?:www\\.)?' + escapedDomain + '(?:\\/[^"\']*)?)?\\1[^>]*>.*?<\\/a>', 'gi'); | |
var matches = content.match(regex); | |
if (matches) { | |
// Filter out any matches that don't actually contain the domain | |
matches = matches.filter(function(match) { | |
return match.includes(domain); | |
}); | |
return matches.join('\n'); | |
} else { | |
return ''; | |
} | |
} catch (error) { | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment