在内容脚本中可以使用`chrome。*`扩展API吗?

编程入门 行业动态 更新时间:2024-10-27 16:26:38
本文介绍了在内容脚本中可以使用`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 / a / 11617742

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

更多推荐

在内容脚本中可以使用`chrome。*`扩展API吗?

本文发布于:2023-07-16 22:56:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可以使用   脚本   内容   API   chrome

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!