Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome extension alarm not being triggered

I am working on a Chrome extension that needs to make use of chrome.alarms. The problem is that the alarm is not being triggered or is being triggered with a very inconsistent behavior.

The extension is build based on objects that live inside event pages, as recommended in Manifest v2. Also, I have tab events in the extension, that sometimes wake up the event pages.

Please help me create a chrome.alarm that actually gets triggered roughly every n minutes, or explain to me what I am doing wrong, or suggest an alternative.

Below, you can find the relevant pieces of code.

manifest.json

{
  "manifest_version": 2,
  "browser_action": {
    "default_popup": "index.html"
  },
  "permissions": [
    "<all_urls>",
    "storage",
    "tabs",
    "alarms"
  ],
  "background": {
    "scripts": [
      "js/backend/monitor.js",
      "js/backend/bootstrap.js"
    ],
    "persistent": false
  }
}

bootstrap.js

// Exaggerating with the validation on purpose
chrome.alarms.get('timeToSendToApi', function(alarm) {
  if (typeof alarm === 'undefined' || alarm.name !== 'timeToSendToApi') {
    chrome.alarms.create('timeToSendToApi', {periodInMinutes: 2});
  }
});
Main.monitor.start();

monitor.js

Main.monitor = (function() {

  function send() {
    // Do stuff
  }    

  function tabUpdatedHandler(tabId, changeInfo, tab) {
    // Do stuff
  }

  function tabActivatedHandler(activeInfo) {
    // Do stuff
  }

  function focusLost() {
    // Do stuff
  }

  function start(callback) {
    chrome.tabs.onUpdated.addListener( tabUpdatedHandler );
    chrome.tabs.onActivated.addListener( tabActivatedHandler );
    chrome.windows.onFocusChanged.addListener( focusLost );
    chrome.alarms.onAlarm.addListener(function(alarm) {
      if(alarm.name === 'timeToSendToApi') {
        send();
      }
    });    
  }

  return {
    start: start
  };
}());
like image 337
Interfector Avatar asked Oct 21 '22 21:10

Interfector


1 Answers

It's likely you've hit an issue that we just noticed in the code. About four months ago, a change landed that we now understand could have caused the behavior you're seeing. Given that you saw this in November, you were probably on the dev or canary channel.

Sorry for the hassle; we'll get this fixed ASAP and possibly merged into broader channels.

like image 89
sowbug Avatar answered Oct 27 '22 08:10

sowbug