Does the Chrome Extension alarm go off when Chrome reopens after a timeout?

When using warnings from the Google Chrome extension, the alarm will be turned off if it has been set, and Chrome will be closed and reopen after the alarm expires.

How can i stop this?

Here is a small code example to explain what I mean.

/* If we perform Browser Action to create alarm, then close the browser, wait about 2 minutes for the alarm to expire and then reopen the browser, the alarm will go off and the DoSomething function will get called twice, once by the onStartup event and once by the onAlarm event. */ chrome.browserAction.onClicked.addListener(function (tab) { chrome.alarms.create('myAlarm', { delayInMinutes : 2.0 }); }); chrome.alarms.onAlarm.addListener(function (alarm) { console.log('Fired alarm!'); if (alarm.name == 'myAlarm') { createListener(); } }); chrome.runtime.onStartup.addListener(function () { console.log('Extension started up...'); DoSomething(); }); function DoSomething() { alert('Function executed!'); } 

So, if you read the comment at the top of my sample code, you will see what happens.

However, I want the alarm to be cleared if the browser is closed, since I want the DoSomething function to be executed only on the onStartup event, if the browser has just started, and let the alarm only perform the DoSomething function after the browser starts, and my code creates a new alarm.

I never want the alarm to remain after closing the browser, and then onAlarm runs when the browser is reopened.

How can this be achieved?

+4
source share
1 answer

It is not possible for the Chrome extension to run some code reliably when you close the browser.

Instead of clearing after shutdown, just make sure that old alarms do not start at startup. This can be achieved by creating a unique (for the session) identifier.

If you use event pages, save the identifier in chrome.storage.local (remember to set the storage permission in the manifest file). Otherwise, save it in the global area.

 // ID generation: chrome.runtime.onStartup.addListener(function () { console.log('Extension started up...'); chrome.storage.local.set({ alarm_suffix: Date.now() }, function() { // Initialize your extension, eg create browser action handler // and bind alarm listener doSomething(); }); }); // Alarm setter: chrome.storage.local.get('alarm_suffix', function(items) { chrome.alarms.create('myAlarm' + items.alarm_suffix, { delayInMinutes : 2.0 }); }); // Bind alarm listener. Note: this must happen *after* the unique ID has been set chrome.alarms.onAlarm.addListener(function(alarm) { var parsedName = alarm.name.match(/^([\S\s]*?)(\d+)$/); if (parsedName) { alarm.name = parsedName[0]; alarm.suffix = +parsedName[1]; } if (alarm.name == 'myAlarm') { chrome.storage.local.get('alarm_suffix', function(data) { if (data.alarm_suffix === alarm.suffix) { doSomething(); } }); } }); 

If you use not , using event pages, but regular background pages, just save the variable globally (advantage: read / write id becomes synchronous, which requires less code):

 chrome.runtime.onStartup.addListener(function () { window.alarm_suffix = Date.now(); }); chrome.alarms.create('myAlarm' + window.alarm_suffix, { delayInMinutes : 2.0 }); chrome.alarms.onAlarm.addListener(function(alarm) { var parsedName = alarm.name.match(/^([\S\s]*?)(\d+)$/); if (parsedName) { alarm.name = parsedName[0]; alarm.suffix = +parsedName[1]; } if (alarm.name == 'myAlarm') { if (alarm.suffix === window.alarm_suffix) { doSomething(); } } }); 

Or just use the good old setTimeout to achieve the same goal without side effects.

 setTimeout(function() { doSomething(); }, 2*60*1000); // 2 minutes 
+5
source

All Articles