Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

javascript - 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?

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;
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...