Impressive update to get the linksin links
To focus the update exclusively on capturing links that use LinkedIn-related keywords in thealttext of nested image tags, we'll refine the function to specifically look for these patterns. This approach aims to identify LinkedIn links that have been shortened but can still be indirectly identified through thealt` attribute of images within anchor tags. Here's how you can modify the function:
const cheerio = require('cheerio');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (req.body && req.body.htmlContent) {
const htmlContent = req.body.htmlContent;
const linkedinLinks = extractLinkedinLinks(htmlContent);
context.res = {
status: 200,
body: linkedinLinks.length > 0 ? linkedinLinks : "No LinkedIn links found in the HTML content."
} else {
context.res = {
status: 400,
body: "Please provide HTML content in the request body."
}
function extractLinkedinLinks(htmlContent) {
const $ = cheerio.load(htmlContent);
const linkedinLinks = [];
$('a[href*="linkedin.com"]').each((index, element) => {
const href = $(element).attr('href');
if (href && href.startsWith('https://www.linkedin.com')) {
linkedinLinks.push(href);
}
$('a').each((index, element) => {
const href = $(element).attr('href');
const imagesWithLinkedInAlt = $(element).find('img[alt*="LinkedIn"],img[alt*="linkedin"]');
if (imagesWithLinkedInAlt.length > 0) {
if (href && !href.includes('linkedin.com')) {
linkedinLinks.push(href);
}
}
return linkedinLinks;
}
In this updated function, the additional check specifically targets anchor tags (<a>) that contain images with alt attributes including "LinkedIn" (case-insensitive search for "LinkedIn" and "linkedin"). This is achieved by using the jQuery-like selector 'img[alt*="LinkedIn"],img[alt*="linkedin"]' within each anchor tag. If such an image is found, and the link doesn't directly contain 'linkedin.com' (suggesting it's a shortened URL), the URL is added to the linkedinLinks list.
This refined approach ensures that the function captures LinkedIn links that have been shortened and are identifiable through specific alt text clues in nested images, enhancing the capability to detect LinkedIn profiles from a broader range of HTML content formats.`
Output

Imported from rifaterdemsahin.com · 2024