Chrome extension: is there a ways to access the $0 (the selected console element) from an extension?

坚强是说给别人听的谎言 提交于 2019-12-13 03:59:08

问题


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?


回答1:


$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.



来源:https://stackoverflow.com/questions/19400796/chrome-extension-is-there-a-ways-to-access-the-0-the-selected-console-element

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