chrome扩展:在内容脚本和后台脚本之间共享对象 [英] chrome extension: sharing an object between content scripts and background script

查看:134
本文介绍了chrome扩展:在内容脚本和后台脚本之间共享对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Chrome扩展程序,该扩展程序在内容脚本中使用jQuery/Zepto.现在,该扩展名旨在在每个网站上运行,这意味着在用户打开的每个选项卡上都将加载jQuery/Zepto的副本.

I am developing a chrome extension that uses jQuery/Zepto in the content script. Now, the extension is meant to run on every website, which means a copy of jQuery/Zepto is loaded on each tab the user opens.

是否可以在各种内容脚本之间共享jQuery/Zepto对象?

Is there a way to share the jQuery/Zepto object between the various content scripts?

我知道内容脚本可以与后台脚本进行通信.我希望能够让后台脚本访问jQuery对象并返回对每个内容脚本的引用.但是我意识到内容和后台脚本之间只能传递JSON消息.

I know content scripts can communicate with the background script. I was hoping to be able to let the background script have access to the jQuery object and return a reference to it, to each of the content scripts. But I realized only JSON messages can be passed between content and background scripts.

有什么方法可以实现我想要的吗?

Is there any way to achieve what I want?

推荐答案

不同选项卡中的内容脚本也无法访问彼此的JavaScript对象.

Content scripts in different tabs do not have access to each other's JavaScript objects either.

Chrome支持通过 chrome.runtime.sendMessage + .onMessage .由于所有消息都是JSON序列化的,因此无法通过这种方式将JavaScript对象泄漏"到其他上下文中.

Chrome supports communication between content scripts and/or the background page via chrome.runtime.sendMessage + .onMessage. Because all messages are JSON-serialized, JavaScript object cannot be "leaked" to other contexts in this way.

因此:不,您不能与其他选项卡(其中的内容脚本)共享jQuery之类的对象.

So: No, you cannot share objects such as jQuery with (content scripts in) other tabs.

这篇关于chrome扩展:在内容脚本和后台脚本之间共享对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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