在内容脚本中可以使用`chrome。*`扩展API吗? [英] Can `chrome.*` extension API's be used inside content scripts?

查看:186
本文介绍了在内容脚本中可以使用`chrome。*`扩展API吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用 chrome.windows.onCreated 但我得到了未捕获的TypeError:无法读取未定义的属性'onCreated'。我注意到在示例中,所有对 chrome。* API的调用似乎都源自后台脚本?我不能在内容脚本中使用它们吗?

I am trying to use chrome.windows.onCreated but I got Uncaught TypeError: Cannot read property 'onCreated' of undefined. I noticed in the samples, all calls to chrome.* API's seem to originate from background scripts? Can I not use them in content scripts?

我想在浏览器中触发点击。然后在弹出窗口中获取一些信息(刮擦)。为此,我想我需要知道何时打开一个窗口。所以我需要 windows.onCreated ?我可以在内容脚本中使用它吗?或者我将如何结合背景和内容脚本?

I want to trigger clicks in my browser. Then get some information (scrape) in popup window. For that I think I will need to know when a window is opened. So I need windows.onCreated? Can I use that in content scripts? Or how will I combine background and content scripts?

推荐答案

来自内容脚本 b
$ b

From the documentation for Content scripts:


但是,内容脚本有一些限制。


  • 使用 chrome。* API (除了部分 chrome.extension

  • 使用由其扩展页面定义的变量或函数
  • 使用由网页或其他内容脚本定义的变量或函数

  • Use chrome.* APIs (except for parts of chrome.extension)
  • Use variables or functions defined by their extension's pages
  • Use variables or functions defined by web pages or by other content scripts

可以避免间接这些限制,主要通过发送邮件。该文档提供了几个涉及消息传递的示例。在之前的Chrome 20中,消息API方法被称为 onRequest sendRequest 。自20版以来,它们被称为 onMessage sendMessage

These limitations can indirectly be avoided, mainly by sending messages within the extension. The documentation offers several examples which involves message passing. Prior Chrome 20, the message API methods was called onRequest and sendRequest. Since version 20, they're called onMessage and sendMessage.

下面的答案提到了如何将消息从内容脚本传递到弹出窗口的步骤:
https:// stackoverflow .com / a / 11617742

Here's an answer which mentions the steps how to pass a message from a content script to a popup: https://stackoverflow.com/a/11617742

这篇关于在内容脚本中可以使用`chrome。*`扩展API吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆