How may I get the element attributes (text, id, class and so on..) of the current tab, out of a mouse click, from a chrome extension?

▼魔方 西西 提交于 2019-12-13 23:14:23

问题


I'm pretty new on chrome extensions and so far, I could manage to get the current tab title and mouse positions, however, I've made some searches and couldn't find a way to get the element attributes, such as "e.target.innerText" when I click on them, right click and get a new option, anything from a mouse input, does anyone knows how to do so?

This is what my popup.js looks like

window.onclick = e => {
    chrome.extension.getBackgroundPage().chrome.tabs.executeScript(null, {
        code:'chrome.runtime.sendMessage(document.title)'
    });;
} 

chrome.runtime.onMessage.addListener(function (message) {
    document.getElementById('pagetitle').innerHTML = message;
});

回答1:


In your content.js, write the following code-

$(window).click(function(event) {
    console.log("Click event: ", event);
});

Content scripts are files that run in the context of web pages. By using the standard Document Object Model (DOM), they are able to read details of the web pages the browser visits, make changes to them and pass information to their parent extension.




回答2:


Try hitting Ctrl-Shift-C after opening the inspector window




回答3:


you should get the target DOM first, such as document.getElementById('targetId'), and then , the DOM attribute can be shown.



来源:https://stackoverflow.com/questions/52921227/how-may-i-get-the-element-attributes-text-id-class-and-so-on-of-the-curren

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!