Commit 4aa6bdb 1 parent 0cd6920 commit 4aa6bdb Copy full SHA for 4aa6bdb
File tree 1 file changed +15
-6
lines changed
functional-samples/tutorial.quick-api-reference
1 file changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -5,16 +5,25 @@ const updateTip = async () => {
5
5
const response = await fetch ( 'https://extension-tips.glitch.me/tips.json' ) ;
6
6
const tips = await response . json ( ) ;
7
7
const randomIndex = Math . floor ( Math . random ( ) * tips . length ) ;
8
- await chrome . storage . local . set ( { tip : tips [ randomIndex ] } ) ;
8
+ return chrome . storage . local . set ( { tip : tips [ randomIndex ] } ) ;
9
9
} ;
10
10
11
- // Create a daily alarm and retrieves the first tip when extension is installed.
12
- chrome . runtime . onInstalled . addListener ( ( { reason } ) => {
13
- if ( reason === 'install' ) {
14
- chrome . alarms . create ( { delayInMinutes : 1 , periodInMinutes : 1440 } ) ;
11
+ const ALARM_NAME = 'tip' ;
12
+
13
+ // Check if alarm exists to avoid resetting the timer.
14
+ // The alarm might be removed when the browser session restarts.
15
+ async function createAlarm ( ) {
16
+ const alarm = await chrome . alarms . get ( ALARM_NAME ) ;
17
+ if ( typeof alarm === 'undefined' ) {
18
+ chrome . alarms . create ( ALARM_NAME , {
19
+ delayInMinutes : 1 ,
20
+ periodInMinutes : 1440
21
+ } ) ;
15
22
updateTip ( ) ;
16
23
}
17
- } ) ;
24
+ }
25
+
26
+ createAlarm ( ) ;
18
27
19
28
// Retrieve tip of the day
20
29
chrome . alarms . onAlarm . addListener ( updateTip ) ;
You can’t perform that action at this time.
0 commit comments