1

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.

2
  • Is your background.js file in the manifest.json? Commented Oct 2, 2017 at 17:33
  • "content_scripts": [{"matches": ["<all_urls>"],"js": ["background.js"]}], Commented Oct 2, 2017 at 17:45

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.