如何将页面中的数据接收到 Chrome 扩展程序中?
我见过类似的问题,但他们想要的内容的细微差别导致我无法将其转换为我想要的内容。
我希望能够从网页获取背景颜色等信息到扩展程序中,并能够在扩展程序中使用它。我已经向下一页写入了内容,但从它接收数据的情况还没有那么多。
据我了解,get和sendRequest是用于扩展中文件之间的交互,而不是与页面交互。
I've seen similar questions but the slight difference in what they wanted caused me to fail to convert it to what I want.
I want to be able to fetch info such as background color from the web page into the extension and be able to work with it in the extension. I already have writing to the page down, but not so much on receiving data from it.
From what I understood, get and sendRequest was for interaction between files in the extension, not for interaction with the page.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
get
和sendRequest
用于在所谓的内容脚本和后台脚本之间传递信息。内容脚本可以访问允许页面的 DOM,但不能执行任意 AJAX 查找,而后台脚本无法访问允许页面的 DOM。对于您的问题,听起来您可以在内容脚本中解决它。该页面应该告诉您需要添加的
manifest.json
的相关部分,然后您可以告诉 Chrome 加载某些 CSS 覆盖文件以及您可能需要的 JS 文件(如果 URL 与匹配
中的模式。如果您正在执行 AJAX 请求,则需要在manifest.json
中请求权限,运行获取结果的 JS,并使用 消息传递 将信息从后台脚本传递到可以处理它的内容脚本。get
andsendRequest
are used to pass information between so-called content scripts and background scripts. Content scripts have access to the DOM of the allowed pages but can't do arbitrary AJAX lookups, while background scripts can't access the DOM of allowed pages.For your problem, it sounds like you can solve it in a content script. That page should tell you the relevant parts of the
manifest.json
you need to add, and with that you tell Chrome to load certain CSS override files as well as JS files you might need if the URL matches a pattern inmatches
. If you're doing AJAX requests, you'll need to request permissions inmanifest.json
, run JS that gets your results, and use message passing to pass that information from the background script to a content script that can handle it.