can you modify the code to indicate the word that is close to the hovered word in…
- User
can you modify the code to indicate the word that is close to the hovered word in meaning from the four words below it:
// content.js
// Cache to store fetched meanings and prevent redundant API calls const meaningsCache = {};
// Variable to keep track of the currently hovered word let currentWord = null;
// Variable to keep the tooltip element let tooltip = null;
// Listen for mouseover events document.addEventListener('mouseover', handleMouseOver);
// Listen for mouseout events to remove tooltip document.addEventListener('mouseout', handleMouseOut);
// Function to handle mouseover events function handleMouseOver(event) { const word = getWordUnderCursor(event);
// If there's a word and it's different from the current word if (word && word !== currentWord) { currentWord = word;
// Check if meanings are already cached if (meaningsCache[word]) { showTooltip(event.pageX, event.pageY, meaningsCache[word]); } else { // Fetch meanings and cache them fetchMeanings(word) .then(meanings => { if (meanings.length > 0) { meaningsCache[word] = meanings.join('; '); showTooltip(event.pageX, event.pageY, meaningsCache[word]); } }) .catch(error => { console.error('Error fetching meanings:', error); }); }} }
// Function to handle mouseout events function handleMouseOut(event) { const relatedTarget = event.relatedTarget;
// If moving to a child element, do not remove tooltip if (tooltip && (!relatedTarget || !tooltip.contains(relatedTarget))) { removeExistingTooltip(); currentWord = null; } }
// Function to get the word under the cursor function getWordUnderCursor(event) { const range = document.caretRangeFromPoint ? document.caretRangeFromPoint(event.clientX, event.clientY) : document.caretPositionFromPoint(event.clientX, event.clientY)?.offsetNode;
if (!range) return null;
const node = range.startContainer;
if (node.nodeType !== Node.TEXT_NODE) return null;
const text = node.textContent; const offset = range.startOffset;
// Use regex to find word boundaries const left = text.slice(0, offset).search(/\S+$/); const right = text.slice(offset).search(/\s/);
const start = left === -1 ? 0 : left; const end = right === -1 ? text.length : offset + right;
const word = text.slice(start, end).trim();
// Validate that it's a single word (no spaces) if (word && !/\s/.test(word)) { return word; }
return null; }
// Function to fetch meanings from the Datamuse API function fetchMeanings(word) { return fetch(
https://api.datamuse.com/words?ml=${encodeURIComponent(word)}&max=5) .then(response => response.json()) .then(data => data.map(obj => obj.word)); }// Function to display a tooltip near the hovered word function showTooltip(x, y, text) { removeExistingTooltip(); // Ensure no duplicate tooltips
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.