Home | History | Annotate | Download | only in notifications
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 /*
      6   Displays a notification with the current time. Requires "notifications"
      7   permission in the manifest file (or calling
      8   "Notification.requestPermission" beforehand).
      9 */
     10 function show() {
     11   var time = /(..)(:..)/.exec(new Date());     // The prettyprinted time.
     12   var hour = time[1] % 12 || 12;               // The prettyprinted hour.
     13   var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
     14   new Notification(hour + time[2] + ' ' + period, {
     15     icon: '48.png',
     16     body: 'Time to make the toast.'
     17   });
     18 }
     19 
     20 // Conditionally initialize the options.
     21 if (!localStorage.isInitialized) {
     22   localStorage.isActivated = true;   // The display activation.
     23   localStorage.frequency = 1;        // The display frequency, in minutes.
     24   localStorage.isInitialized = true; // The option initialization.
     25 }
     26 
     27 // Test for notification support.
     28 if (window.Notification) {
     29   // While activated, show notifications at the display frequency.
     30   if (JSON.parse(localStorage.isActivated)) { show(); }
     31 
     32   var interval = 0; // The display interval, in minutes.
     33 
     34   setInterval(function() {
     35     interval++;
     36 
     37     if (
     38       JSON.parse(localStorage.isActivated) &&
     39         localStorage.frequency <= interval
     40     ) {
     41       show();
     42       interval = 0;
     43     }
     44   }, 60000);
     45 }
     46