diff --git a/uncloseai.js b/uncloseai.js index c134872..8cc6408 100644 --- a/uncloseai.js +++ b/uncloseai.js @@ -32,8 +32,50 @@ let chatHistory = [ ]; // Function to extract text content from the webpage +//function extractWebpageContent() { +// return document.body.innerText; +//} + +// Function to extract text content along with links and metadata from the webpage function extractWebpageContent() { - return document.body.innerText; + let content = ''; + + // Extract title + const title = document.title; + if (title) { + content += `**Page Title**: ${title}\n\n`; + } + + // Extract meta description + const metaDescription = document.querySelector('meta[name="description"]'); + if (metaDescription) { + content += `**Meta Description**: ${metaDescription.content}\n\n`; + } + + // Extract other metadata (if needed) + const metaKeywords = document.querySelector('meta[name="keywords"]'); + if (metaKeywords) { + content += `**Meta Keywords**: ${metaKeywords.content}\n\n`; + } + + // Recursively extract text and links from the body content + function getTextWithLinks(element) { + if (element.nodeType === Node.TEXT_NODE) { + content += element.textContent + ' '; + } else if (element.nodeType === Node.ELEMENT_NODE) { + if (element.tagName.toLowerCase() === 'a') { + // If it's a link, append the text and the href + content += `[${element.textContent}](${element.href}) `; + } else { + // Recursively process child nodes + element.childNodes.forEach(getTextWithLinks); + } + } + } + + getTextWithLinks(document.body); // Start with the body element + + return content.trim(); } // Generator function to send a message to the LLM and yield responses