Chrome扩展程序:如何让HTML元素被鼠标悬停在上面

Chrome Extension: How to get HTML element being hovered on by the mouse

本文关键字:元素 鼠标 悬停 在上面 HTML 程序 扩展 Chrome      更新时间:2023-09-26

我正在制作一个 chrome 扩展,需要获取当前鼠标悬停在上面的元素的 ID。从本质上讲,如果鼠标悬停在按钮上,并且我使用键盘快捷键,它应该能够显示带有按钮 ID 的警报。

这是我期望做的:

manifest.json

    "commands": {
    "Display ID": {
        "suggested_key": {
            "default": "Ctrl+Shift+1",
            "mac": "Command+Shift+1"
        },
        "description": "Display ID"
    },

背景.js

chrome.commands.onCommand.addListener(function(command) {
    // Get the active tab when the command was fired
    chrome.tabs.query({active:true, currentWindow: true}, function(arrayOfTabs) {
        currentTabId = arrayOfTabs[0].id; 
    });
    // Display the ID of the HTML element where the mouse is currently hovering
    alert("This is the element's id:" + elementId); // How to do this part???
});

请告诉我这是否可能!!

我找到了问题的答案。我将当前悬停的元素存储到变量中,然后在执行键盘快捷键命令时调用它。实质上,我放置了一个新的鼠标悬停事件侦听器,用于跟踪当前悬停的元素。