我正在开发需要Firebase数据库的Google Chrome扩展程序。它工作正常," popup.html"目前是开放的。但是当它关闭时,我的background.js就会停止工作。
我需要一个允许background.js在后台运行的解决方案,即使关闭popup.html或浏览器已最小化。
这是我的background.js:
var db = firebase.database().ref();
var clipText;
setInterval(function() {
document.addEventListener('paste', function(event) {
clipText = event.clipboardData.getData('Text');
if (clipText != null) {
db.child('data').set(clipText);
} else {
console.log('Cliptext is null...');
}
});
document.execCommand('paste');
}, 3000)
如你所见,它使用" setInterval"用于每隔3秒在后台执行一些操作。
答案 0 :(得分:1)
根据您的评论,您在内容脚本中声明了background.js
。
您需要在manifest.json
内部声明它,例如:
"background": {
"persistent": true,
"scripts": ["lib/jquery-3.1.1.min.js", "background.js"]
},
您可以阅读Chrome扩展程序架构here。