How to modify content under a devtools panel in a Chrome extension?

老子叫甜甜 提交于 2019-12-08 02:13:40

问题


I am working on Google Extension where I am adding new panel to the developer tools and it seems to be working fine for me. But I don't know how to modify the content of the panel through JavaScript.

Could anyone enlighten me?


回答1:


Displaying content in a panel is not that hard. First, I assume that you've created the base extension (consisting of manifest.json, devtools.html and devtools.js) following the documentation. devtools.html contains at least <script src="/devtools.js"></script>.

When chrome.devtools.panel.create is used to create a panel, a callback can be specified. This callback receives an argument of the type ExtensionPanel. This object defines the onShown and onHidden event listeners.
The onShown event listener receives an additional argument: A reference to the panel's window object. Now, you can do anything you wish.

For example, append some text to the panel, when the user opens the devtools panel for the first time:

chrome.devtools.panels.create("devtools Test", "/icon.png", "/panel.html",
  function(extensionPanel) {
    var runOnce = false;
    extensionPanel.onShown.addListener(function(panelWindow) {
        if (runOnce) return;
        runOnce = true;
        // Do something, eg appending the text "Hello!" to the devtools panel
        panelWindow.document.body.appendChild(document.createTextNode('Hello!'));
    });
});


来源:https://stackoverflow.com/questions/11624307/how-to-modify-content-under-a-devtools-panel-in-a-chrome-extension

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