Chrome扩展中的重要概念:Content Scripts

If you want your extension to interact with web pages, you need a content script.

一直在关注如何做Chrome的扩展,Chrome如何支持扩展。以前就看过Turorial中的HelloWorld,觉得这么简单,能干些什么啊。今天看了nkGestures的源码,然后又重新看了一下Chrome Extension的开发文档,觉得Content Script的概念比较重要,在此把我的理解记录下。

Chrome的扩展基本是基于Java Script的(当然可以通过其它方法调用C/C++代码),这样就为以后不同平台下的浏览器做好了准备,果然是Google。

Chrome允许在打开窗口,TAB或其它事件的情况下执行扩展中的代码,这部分代码就叫做:Content Scripts。Content Scripts are JavaScript files that run in the context of web pages. By using the standard Document Object Model (DOM), they can read details of the web pages the browser visits, or make changes to them.同时,Content Scripts 可以通过Channel和Extension进行通讯。

Content scripts can communicate with their parent extension using message passing. The content script opens a channel to the extension using the chrome.extension.connect() method and then sends messages back and forth to it. The messages can contain any valid JSON object (null, boolean, number, string, array, or object).

The parent extension can also open a channel to a content script in a given tab by callingchrome.tabs.connect(tabId).

When a channel is opened from a content script to an extension, the onConnect event is fired in all views in the extension. Any view can receive the event. The event contains a Port object, which can be used by the extension view to communicate back to the content scrip

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值