turing
turing

Reputation: 157

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. It works fine, "popup.html" is currently open. But when it closes, my background.js stops working.

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.

Here is my 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.

Upvotes: 1

Views: 126

Answers (1)

Luka Čelebić
Luka Čelebić

Reputation: 1093

Based on your comment you are declaring your background.js in the content script.

You need to declare it inside your manifest.json like this for example:

"background": {
    "persistent": true,
    "scripts": ["lib/jquery-3.1.1.min.js", "background.js"]
},

You can read about Chrome Extensions architecture here.

Upvotes: 1

Related Questions