chrome.extension.getBackgroundPage()函数示例 [英] chrome.extension.getBackgroundPage() function example

查看:3798
本文介绍了chrome.extension.getBackgroundPage()函数示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个需要在后台运行的小型Chrome扩展程序。但是,我明白,当我使用弹出窗口时,这是不可能的。看完一些后,似乎最好的选择是创建 popup.js ,然后运行 background.js ,使用 chrome.extension.getBackgroundPage()函数。



有人可以向我展示一下它是如何完成的吗?



这里是清单:

 browser_action:{
permissions:[background],
default_popup:popup。 html},
options_page:options.html,
background:{
scripts:[background.js],
persistent :true
}

我在<$ c $中包含了popup.js引用c> popup.html :

 < script src =popup.js><<<< ; /脚本> 

然后在 popup.js

  var bkg = chrome.runtime.getBackgroundPage(); 

现在我需要一种方法来激活 background.js
我需要从 popup.js 中的 background.js 内运行相关函数。 ,
或给出 background.js 运行的一般命令?

解决方案

是的,你需要在弹出窗口中调用背景函数。这是一个简单的例子,它演示了它是如何工作的。

background.js
$ b

 功能backgroundFunction(){
返回hello from the background!
}

popup.js

 (function(){
var otherWindows = chrome.extension.getBackgroundPage();
console.log(otherWindows.backgroundFunction ));
})();

当您检查popup.js时,您会看到来自背景的问候!在你的控制台。 GetBackgroundPage()只是为您的背景页面返回窗口对象,因为您可能知道所有的变量都附加在这个窗口对象上,这样你就可以访问在后台脚本中定义的函数。



有一个示例代码在chrome文档,请参阅空闲简单示例并查看 history.js


I'm working on a small Chrome extension that needs to run in the background. However, I understand that that isn't possible when I'm using a popup. After some reading it seems that the best option is to create popup.js in order run the background.js, using chrome.extension.getBackgroundPage() function.

Can someone please show me an example of how it's done?

here's the manifest:

"browser_action": {
"permissions": ["background"],
"default_popup": "popup.html"},
"options_page": "options.html",
"background": {
    "scripts": ["background.js"],
    "persistent" : true
}

I've included the popup.js reference in popup.html:

<script src="popup.js"></script>

And created a variable in popup.js

var bkg = chrome.runtime.getBackgroundPage();

so now I need a way to activate the background.js Do I need to run the relevant function inside background.js from popup.js, or give a general command for the background.js to run?

解决方案

Yes, you need to call function from background in your popup. Here's a simple example which demonstrates how it works.

background.js

function backgroundFunction () {
    return "hello from the background!"
}

popup.js

(function () {
    var otherWindows = chrome.extension.getBackgroundPage();
    console.log(otherWindows.backgroundFunction()); 
})();

When you inspect your popup.js you'll see "hello from the background!" in your console. GetBackgroundPage() simply returns window object for your background page, as you probably know all variables are attached to this window object so in this way you will get access to function defined in background scripts.

There is a sample code demonstrating this in chrome documentation see Idle Simple Example and look at file history.js

这篇关于chrome.extension.getBackgroundPage()函数示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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