[英]How can javascript be executed in background without opening popup.html in Chrome extension?
I'm developing an extension for Google Chrome that requires a Firebase database.我正在为需要 Firebase 数据库的 Google Chrome 开发扩展程序。 It works fine, "popup.html" is currently open.
它工作正常,“popup.html”当前已打开。 But when it closes, my background.js stops working.
但是当它关闭时,我的 background.js 停止工作。
I need a solution that will allow background.js to work in the background, even when the popup.html is closed, or the browser is minimized.我需要一个允许 background.js 在后台工作的解决方案,即使在 popup.html 关闭或浏览器最小化时也是如此。
Here is my background.js:这是我的 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)
As you see, it uses "setInterval" function to do some actions in background every 3 seconds.如您所见,它使用“setInterval”函数每 3 秒在后台执行一些操作。
Based on your comment you are declaring your background.js
in the content script.根据您的评论,您在内容脚本中声明了
background.js
。
You need to declare it inside your manifest.json
like this for example:您需要在
manifest.json
像这样声明它,例如:
"background": {
"persistent": true,
"scripts": ["lib/jquery-3.1.1.min.js", "background.js"]
},
You can read about Chrome Extensions architecture here .您可以在此处阅读有关 Chrome 扩展程序架构的信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.