如何从Chrome扩展程序中单击鼠标以获取当前选项卡的元素属性(文本,id,类等). [英] 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?

查看:232
本文介绍了如何从Chrome扩展程序中单击鼠标以获取当前选项卡的元素属性(文本,id,类等).的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在chrome扩展方面还很陌生,到目前为止,我可以设法获取当前的标签标题和鼠标位置,但是,我进行了一些搜索,但找不到方法来获取元素属性,例如当我单击它们时,将其显示为"e.target.innerText",右键单击并获得一个新选项,任何来自鼠标输入的内容,有人知道怎么做吗?

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?

这就是我的popup.js的样子

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;
});

推荐答案

在content.js中,编写以下代码-

In your content.js, write the following code-

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

内容脚本是在网页上下文中运行的文件.通过使用标准的文档对象模型(DOM),他们能够读取浏览器访问的网页的详细信息,对其进行更改并将信息传递给其父扩展.

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.

这篇关于如何从Chrome扩展程序中单击鼠标以获取当前选项卡的元素属性(文本,id,类等).的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆