I am developing a chrome extension and a certain feature allows the user to click on a button to select an element on the current page (just like the hover feature in Dev Tools). A certain problem I am encountering is when some elements (children) within a large div are not being singled out. Is there a way to get the last child of where the mouse is currently pointing?
Note: lastChild does not work as it will get me the last child of the outermost parent div.
Here is the flow:
popup.js
let selectElem = document.getElementById('select_element_button');
selectElem.addEventListener("click", function(){
chrome.runtime.sendMessage("hello");
});
background.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(message + "from bg");
if(message == "hello"){
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id,"toContent");
});
}
});
content.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
if(message == "toContent"){
document.addEventListener("mouseover", (event) => {
event.target.style.backgroundColor = "rgba(121, 204, 255, 0.4)";
});
document.addEventListener("mouseout", (out) => {
out.target.style.backgroundColor = "";
});
}
});