manifest.json
JavaScript
x
18
18
1
{
2
"manifest_version": 2,
3
"name": "App name",
4
"description": "Description goes here",
5
"version": "1.0",
6
"background": {
7
"scripts": ["background.js"]
8
},
9
"permissions": [
10
"tabs",
11
"alarms"
12
],
13
"browser_action": {
14
"default_icon": "icon.png",
15
"default_popup": "popup.html"
16
}
17
}
18
I trying to create a function to make a popup “great” every minute like this:
JavaScript
1
4
1
chrome.alarms.onAlarm.addListener(function(){
2
alert('great');
3
});
4
Could someone please tell why is it not triggering that alert. I check the console, no error was displayed.
Advertisement
Answer
Here is the simplest working example I can think of, warning it is very annoying as when the alarm is on it alerts “Beep” every 12 seconds. It uses a popup browser action to switch the alarm on and off.
manifest.json
JavaScript
1
22
22
1
{
2
"manifest_version": 2,
3
4
"name": "Alarm test",
5
"description": "This extension alarms.",
6
"version": "1.0",
7
8
"permissions": [
9
"alarms"
10
],
11
12
"background": {
13
"scripts": ["eventPage.js"],
14
"persistent": false
15
},
16
17
"browser_action": {
18
"default_icon": "icon.png",
19
"default_popup": "popup.html"
20
}
21
}
22
popup.html
JavaScript
1
14
14
1
<!doctype html>
2
<html>
3
<head>
4
<title>Alarms Popup</title>
5
6
<script src="popup.js"></script>
7
</head>
8
<body>
9
<a href="" id="alarmOn">ON</a>
10
<a href="" id="alarmOff">OFF</a>
11
</ul>
12
</body>
13
</html>
14
popup.js
JavaScript
1
24
24
1
var alarmClock = {
2
3
onHandler : function(e) {
4
chrome.alarms.create("myAlarm", {delayInMinutes: 0.1, periodInMinutes: 0.2} );
5
window.close();
6
},
7
8
offHandler : function(e) {
9
chrome.alarms.clear("myAlarm");
10
window.close();
11
},
12
13
setup: function() {
14
var a = document.getElementById('alarmOn');
15
a.addEventListener('click', alarmClock.onHandler );
16
var a = document.getElementById('alarmOff');
17
a.addEventListener('click', alarmClock.offHandler );
18
}
19
};
20
21
document.addEventListener('DOMContentLoaded', function () {
22
alarmClock.setup();
23
});
24
And the important bit in eventPage.js
JavaScript
1
4
1
chrome.alarms.onAlarm.addListener(function(alarm) {
2
alert("Beep");
3
});
4