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   "webkitNotifications.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   var notification = window.webkitNotifications.createNotification(
     15     '48.png',                      // The image.
     16     hour + time[2] + ' ' + period, // The title.
     17     'Time to make the toast.'      // The body.
     18   );
     19   notification.show();
     20 }
     21 
     22 // Conditionally initialize the options.
     23 if (!localStorage.isInitialized) {
     24   localStorage.isActivated = true;   // The display activation.
     25   localStorage.frequency = 1;        // The display frequency, in minutes.
     26   localStorage.isInitialized = true; // The option initialization.
     27 }
     28 
     29 // Test for notification support.
     30 if (window.webkitNotifications) {
     31   // While activated, show notifications at the display frequency.
     32   if (JSON.parse(localStorage.isActivated)) { show(); }
     33 
     34   var interval = 0; // The display interval, in minutes.
     35 
     36   setInterval(function() {
     37     interval++;
     38 
     39     if (
     40       JSON.parse(localStorage.isActivated) &&
     41         localStorage.frequency <= interval
     42     ) {
     43       show();
     44       interval = 0;
     45     }
     46   }, 60000);
     47 }
     48