Page 1 of 1

Background Tasks

Posted: Fri Nov 25, 2016 2:20 pm
by CodenStuff
Hello Coders,

Let's say you want to create an extension that needs to do some work in the background, a good example would be an RSS feed. Now you'll probably want your extension to check every now and then to see if anything new has been added to the feed and alert the user that new content is available.

This is quite easy to accomplish.

In your manifest.json file add the background permission:
Code: Select all
  "background": {
      "scripts": ["background.js"]
  },
Now you simply add your code, which you want to run in the background, in to a new file called background.js

Here's an example background.js file which updates the extension badge text every second:
Code: Select all
var i = 0;
window.setInterval(function () {
    chrome.browserAction.setBadgeText({ text: String(i) });
    i = i + 1;
}, 1000);
That is all you need

Happy coding :)