Lee Thomas
Lee Thomas

Reputation: 107

chrome.alarms.onAlarm throws Uncaught TypeError, cannot read property 'onAlarm' of undefined

I'm writing a Chrome extension, and I keep getting an error message that chrome.alarms is undefined.

My manifest.json file:

{
  "manifest_version": 2,

  "name": "C",
  "description": "whatever",

  "version": "1.0",

  "background": {
    "scripts": ["background.js"],
    "persistent": false
    },

  "permissions": ["background", "storage", "notifications", "alarms"],

   "browser_action": {
      "default_icon": "logo.png",
      "default_title": "C",
      "default_popup": "popup.html"
    }
}

In my background.js file:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    chrome.alarms.create('arbitrary', {
        when: 1000,
        periodInMinutes: 0.05
    });
});

chrome.alarms.onAlarm.addListener(function (alarm) {
   console.log('alarm called');
});

In my popup.js file:

$(document).ready(function() {
   chrome.runtime.sendMessage({addressInfo: 'text'});
});

I have it loaded on my computer as an unpacked extension, so periodInMinutes and when 1 minute constraints specified by the Chrome API documentation don't apply here.

Upvotes: 2

Views: 3611

Answers (2)

Debarghya Kundu
Debarghya Kundu

Reputation: 1

Reloading the manifests.json file solves the problem.

Upvotes: 0

Lee Thomas
Lee Thomas

Reputation: 107

I figured out the problem shortly after posting this. If anyone wants to know, all I had to do was reload the extension from the extensions page in Chrome. I guess changes in the manifest file don't apply unless the extension is reloaded.

Upvotes: 3

Related Questions