Chrome扩展程序:是否可以通过扩展程序访问$ 0(选定的控制台元素)? [英] Chrome extension: is there a ways to access the $0 (the selected console element) from an extension?

查看:139
本文介绍了Chrome扩展程序:是否可以通过扩展程序访问$ 0(选定的控制台元素)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为正在构建的扩展程序访问$ 0(这是chrome开发人员工具中最后选择的元素).

有什么办法吗?

更多信息: 该扩展名是页面操作扩展名.它具有多个功能,我希望有一个选项可以查询当前选定的对象($ 0).

我可以将其保留为页面扩展扩展名还是必须使用开发人员工具插件的自由度吗?

解决方案

$0仅在开发人员工具仍处于激活状态时才有意义.获得$0结果的唯一方法是通过 devtools页面. >

devtools页面可以使用消息传递API 与其余扩展程序进行通信.在正常情况下,开发工具的状态(打开/关闭)是已知的,并且在(页面操作)弹出窗口的生命周期内是固定的.除非打开弹出窗口,否则无法切换devtool.除非弹出窗口是正在接受检查 .
因此,devtools页面应为接收者 chrome.runtime.onMessage chrome.runtime.onConnect ,并且弹出窗口应为发件人 chrome.riuntime.sendMessage

I am trying to access the $0 (which is the last selected element in the chrome developers tools) for an extension I'm building.

Any way to do so?

More information: The extension is a page action extension. It has several features and I want to have an option to inquire about the current selected object ($0).

Can I keep it as a page extension extension or dof I have to go developer tool plugin way?

解决方案

$0 is only meaningful when the developer tools are still activated. The only way to get the result of $0 is through a devtools page.

A devtools page can communicate with the rest of your extension using the message passing APIs. Under normal circumstances, the state of the dev tools (open/closed) is known and fixed for the lifetime of the (page action) popup the devtools cannot be toggled while the popup is open unless the popup is being inspected.
So, the devtools page should be a receiver chrome.runtime.onMessage or chrome.runtime.onConnect, and the popup should be a sender chrome.riuntime.sendMessage or chrome.runtime.connect.

Within the dev tools page, you can easily interact with the last inspected element using chrome.devtools.inspectedWindow.eval:

// E.g. Test if the currently inspected element is the main <body> element
chrome.devtools.inspectedWindow.eval('$0 === document.body', function(result) {
    alert('$0 is ' + (result ? '' : 'not ') + '<body>');
});

The result of the last expression is passed back to the callback. This value must be serializable, so you cannot "return" the DOM element itself. Consequently, you cannot get direct access to the DOM element, any interaction with the inspected element has to be done through the devtools API.

这篇关于Chrome扩展程序:是否可以通过扩展程序访问$ 0(选定的控制台元素)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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