nodes = document.querySelectorAll(":hover"); i = 1; console.log(nodes); titleNode = nodes[nodes.length - i]; while (titleNode && !titleNode.matches("a#video-title-link")) { titleNode = titleNode.parentElement; console.log(titleNode); } if (!(titleNode && titleNode.matches("a#video-title-link"))) { console.error("No video element found."); } else { link = titleNode.href; fetch("http://localhost:5000/download", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ link: link, }), }).then((res) => { console.log(res); res.json().then((data) => console.log(data)); }); }