您好,我是 Chrome 扩展程序的新手,通过一些示例,我制作了一个 Chrome 扩展程序。
在 chrome 扩展中,我编写了下面的代码,用于运行 background.js
menifest.json
"background": {
"scripts": ["src/bg/background.js"],
"persistent": false
}
backgroud.js
var config = {
apiKey: "AIzaSyAqPnuP-EeU2WXhwJ_3Ks_er0KbTKE-BNg",
authDomain: "hellofirebase-a1794.firebaseapp.com",
databaseURL: "https://hellofirebase-a1794.firebaseio.com"
};
firebase.initializeApp(config);
var commentsRef = firebase.database().ref().child('hellofirebase-a1894');
commentsRef.on('child_changed', function(data) {
var opt = {
type: "basic",
title: "Primary Title",
message: "Primary message to display "+data.val(),
iconUrl: "url_to_small_icon"
}
chrome.notifications.create("notifi"+1, opt, null);
});
Firebase 数据结构看起来像
hellofirebase-a1794
|
|
-------betteryLevel : 13
因此,Firebase 实时数据库中的任何值发生变化,我应该从 chrome 扩展程序收到通知,但没有任何反应,请任何人帮助我如何解决此问题。
最佳答案
如果您想要后台页面,则必须删除 persistent: false
选项。您现在拥有的是an event page ,它只会在“需要时”运行。后台页面将连续运行,如您所愿。
关于javascript - 加载浏览器时在后台持续运行 chrome 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849912/