如何访问Chrome扩展中弹出的背景页面中的对象

在我正在开发的Chrome扩展中,我想在点击弹出窗口时访问在后台页面中创建和维护的数据结构。不幸的是,我对Javascript和Chrome扩展开发一般都不熟悉,你能告诉我该怎么做吗?这是否涉及弹出窗口和背景页面之间的消息传递? 谢谢。如何访问Chrome扩展中弹出的背景页面中的对象

回答:

你可以写这样的三个文件从popup.html在background.html访问数据结构:


//in popup.html 

<script type="text/javascript" src="mainscript.js"></script>

<!-- JavaScript and HTML must be in separate files for security. -->


//in mainscript.js 

chrome.extension.getBackgroundPage().data = 'your data';


//in background.html 

<script type="text/javascript">

var data;

</script>

和你需要一个像这样的manifest.json(也许使用browser_action而不是page_action):

.... 

,

"background_page": "background.html",

"page_action": {

"default_icon": "your_icon.ico",

"default_title": "Your title",

"default_popup": "popup.html"

},

....

编辑:在镀铬的扩展消息传递看到这些功能

http://code.google.com/chrome/extensions/extension.html#method-sendRequest

http://code.google.com/chrome/extensions/extension.html#event-onRequest

和此有用的描述:

http://code.google.com/chrome/extensions/messaging.html

以上是 如何访问Chrome扩展中弹出的背景页面中的对象 的全部内容, 来源链接: utcz.com/qa/266070.html

回到顶部