简体   繁体   English

如何为 chrome 扩展开发添加 react-devtools?

[英]How to add react-devtools for chrome extension development?

I am inserting a react component into a web page - Everything seems to work well but im not able to get the react dev tools working.我正在将一个反应组件插入 web 页面 - 一切似乎都运行良好,但我无法让反应开发工具工作。

After reading online it seems like this could be caused by:在网上阅读后,似乎这可能是由以下原因引起的:

  1. React devtools injects a script and it doesn't have the required permissions React devtools 注入了一个脚本,但它没有所需的权限
  2. Extensions can not communicate with eachover扩展无法与每个人通信

Things i have tried:我尝试过的事情:

  • Checking that the REACT_DEVTOOLS_GLOBAL_HOOK is present on the window object, Its present on the window when targeted through the browser but not when i try to grab it from within my extension检查 window object 上是否存在 REACT_DEVTOOLS_GLOBAL_HOOK,当我尝试从我的扩展程序中获取它时,它存在于 window 上

  • I have tried give any required permissions by doing:我尝试通过执行以下操作来授予任何必需的权限:

"content_security_policy": "connect-src ws://localhost:8097", // to my manifest
// and
"script-src http://localhost:8097"
//and 
"content_security_policy": "script-src 'self' http://localhost:8097; object-src 'self'",

Which would give me an error that the value is invalid - So i went to a manifest v2.这会给我一个值无效的错误 - 所以我去了一个清单 v2。 That did fix that error and then i was able to run那确实修复了该错误,然后我能够运行

yarn add react-devtools
yarn react-devtools

Which caused the devtools to pop up (not through the chrome extension but from my console) - However this was empty and there were no components being shown on it.这导致 devtools 弹出(不是通过 chrome 扩展,而是从我的控制台) - 但是这是空的,上面没有显示任何组件。

Im suspicious of how these components are actually being picked up because right now i am adding the components to the body of the page using querySelector, Could this be causing an issue?我怀疑这些组件实际上是如何被拾取的,因为现在我正在使用 querySelector 将组件添加到页面主体,这会导致问题吗?

Heres what my code looks like:这是我的代码的样子:


import 'react-devtools'

import React from 'react';
import { render } from 'react-dom';
import Index from './components/index'

render(<App />, window.document.querySelector('body'));

In manifest v3 it's afaik not possible to use a remote script (even though the docs suggest otherwise).在清单 v3 中,不可能使用远程脚本(即使文档另有建议)。

What you can do instead is include the script from the standalone devtools in your extension files.您可以做的是将独立开发工具中的脚本包含在扩展文件中。

So instead of including: <script src="http://localhost:8097"></script> as the docs describe, open that url in your browser, save the JS as file in your chrome extension folder, then include that file.因此,不要像文档描述的那样包括: <script src="http://localhost:8097"></script> ,而是在浏览器中打开 url,将 JS 作为文件保存在 chrome 扩展文件夹中,然后包含该文件. For example <script src="/react-devtools.js"></script> .例如<script src="/react-devtools.js"></script>

Once you have done that the devtools will show in the standalone app.完成后,devtools 将显示在独立应用程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM