且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Chrome 扩展:在内容脚本中获取页面变量

更新时间:2023-11-13 22:28:10

如果确实需要,可以在页面的 DOM 中插入一个 元素; 元素中的代码将被执行,并且该代码将可以访问窗口范围内的 JavaScript 变量.然后,您可以使用 data- 属性和触发自定义事件将它们传达回内容脚本.

If you really need to, you can insert a <script> element into the page's DOM; the code inside your <script> element will be executed and that code will have access to JavaScript variables at the scope of the window. You can then communicate them back to the content script using data- attributes and firing custom events.

听起来很别扭?为什么是的,它是,并且出于 serg 引用的文档中的所有原因故意如此.但如果你真的,真的需要这样做,它是可以做到的.请参阅此处此处了解更多信息.祝你好运!

Sound awkward? Why yes, it is, and intentionally so for all the reasons in the documentation that serg has cited. But if you really, really need to do it, it can be done. See here and here for more info. And good luck!