javascript chrome.extension.getBackgroundPage() 函数示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21146457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
chrome.extension.getBackgroundPage() function example
提问by Nim
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.
我正在开发一个需要在后台运行的小型 Chrome 扩展程序。但是,我知道当我使用弹出窗口时这是不可能的。经过一些阅读,似乎最好的选择是创建popup.js
以运行background.js
, usingchrome.extension.getBackgroundPage()
函数。
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
:
我已将 popup.js 引用包含在popup.html
:
<script src="popup.js"></script>
And created a variable in popup.js
并在其中创建了一个变量 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?
所以现在我需要一种方法来激活background.js
我是否需要在background.js
from 中运行相关函数popup.js
,或者给出一个background.js
运行的通用命令?
回答by Pawel Miech
Yes, you need to call function from background in your popup. Here's a simple example which demonstrates how it works.
是的,您需要在弹出窗口中从后台调用函数。这是一个简单的示例,演示了它是如何工作的。
background.js
背景.js
function backgroundFunction () {
return "hello from the background!"
}
popup.js
弹出窗口.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.
当您检查 popup.js 时,您会看到“您好,来自后台!” 在您的控制台中。GetBackgroundPage()只是返回背景页面的窗口对象,因为您可能知道所有变量都附加到此窗口对象,因此通过这种方式您可以访问在后台脚本中定义的函数。
There is a sample code demonstrating this in chrome documentationsee Idle Simple Exampleand look at file history.js
chrome 文档中有一个示例代码演示了这一点,请参阅空闲简单示例并查看文件history.js
回答by lexasss
The background page is loaded then you extension is loaded into Chrome.
背景页面被加载,然后你的扩展程序被加载到 Chrome 中。
Consider the popup page just as a normal web-page: here, you need to use chrome.runtimeto make requests to the background page.
将弹出页面视为普通网页:在这里,您需要使用chrome.runtime向后台页面发出请求。
Usually, I do it using an implicit (temporal) or explicit (permanent) channel. With temporal channel:
通常,我使用隐式(时间)或显式(永久)通道来执行此操作。使用时间通道:
popup.js
弹出窗口.js
chrome.runtime.onMessage.addListener(function (answer) { /* your code */ }
chrome.runtime.sendMessage({cmd: "shutdown"});
background.js
背景.js
chrome.runtime.onMessage.addListener(function (request) {
if (request.cmd === "shutdown") {
shutdown();
}
}
With permanent channel:
有永久频道:
popup.js
弹出窗口.js
var port = chrome.runtime.connect({name: "myChannel"});
port.onMessage.addListener(function (answer) { /* your code */ })
port.postMessage({cmd: "shutdown"});
background.js
背景.js
chrome.runtime.onConnect.addListener(function (port) {
port.onMessage.addListener(function (request) {
if (request.cmd === "shutdown") {
shutdown();
}
}
}
UPD.While this way of content-backgrounf communication is fully functional, the current specs advice using chrome.runtime.getBackgroundPage()
to call a function in the background script is the requests shouldn't be asynchronous (thus, less coding is needed and easier to read the code). See the manualand other answers to this post to clarify this matter.
更新。虽然这种内容背景通信方式功能齐全,但当前chrome.runtime.getBackgroundPage()
用于在后台脚本中调用函数的规范建议是请求不应该是异步的(因此,需要更少的编码并且更容易阅读代码)。请参阅此帖子的手册和其他答案以澄清此问题。