npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ss5-notify

v1.0.15

Published

Simplyfied pushing notifications with permission checks and fallbacks

Downloads

939

Readme

ss5-notify

ss5-notify is a lightweight JavaScript library that provides an easy-to-use interface for browser notifications with fallbacks. It checks for browser support, manages notification permissions, and simplifies the process of creating notifications.

Features

  • Automatically checks if notifications are supported in the browser.

  • Request permission from users for sending notifications.

  • Send notifications with custom title, body, and icon.

  • Support fallback when the browser does not support notifications.

  • Handle click events on notifications.

Installation & Usage via Skypack

You can use ss5-notify directly in your browser without any build tools, thanks to Skypack. Just import it using the Skypack CDN link.

import NotifyJs from 'https://cdn.skypack.dev/ss5-notify'

Usage

In a browser (via Skypack):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>ss5-notify Example</title>
</head>
<body>
  <button id="notify-btn">Show Notification</button>

  <script type="module">
    import NotifyJs from 'https://cdn.skypack.dev/ss5-notify';

    const notifier = new NotifyJs();

    document.getElementById('notify-btn').addEventListener('click', () => {
      notifier.notifyWithPermission('Hello', // Title of the notification
      {
        body: 'World', // Optional body
        icon: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSn5Z6r3O0UIcR02fodDSmW9VKSsS20Gww0DA&s', // Optional icon
        onclick: () => {
          console.log('Notification clicked'); // Handle click event
        },
        fallback: () => {
          alert('Browser does not support notifications.'); // Fallback for unsupported browsers
        }
      });
    });
  </script>
</body>
</html>

Methods

  1. checksupport()

Description: Checks whether the current browser supports notifications.

Returns: true if notifications are supported, false otherwise.

if (notifier.checkSupport()) {
  console.log('Notifications are supported');
} else {
  console.warn('Notifications are not supported');
}
  1. requestPermission()

Description: Requests permission from the user to display notifications. It returns a promise that resolves if permission is granted or rejects if permission is denied.

notifier.requestPermission()
  .then(() => console.log('Notification permission granted'))
  .catch(err => console.error('Notification permission denied:', err));
  1. isGranted()

Description: Checks if the user has already granted permission for notifications.

Returns: true if permission is granted, false otherwise.

if (notifier.isGranted()) {
  console.log('Notification permission has been granted');
} else {
  console.warn('Notification permission has not been granted');
}
  1. notify(title, options ={...})

Description: Displays a notification with the specified title and options. This method will only work if permission is already granted.

The options object can contain the following properties:

  • title: (string): The title of the notification.

  • body: (string): The body of the notification.

  • icon: (string): The URL of the icon to display in the notification.

  • onclick: (function): A callback function to execute when the notification is clicked.

  • fallback: (function): A fallback function to execute if the browser does not support notifications.

notifier.notify('Hello World!', {
  body: 'This is a sample notification',
  icon: 'https://example.com/icon.png',
  onclick: () => console.log('Notification clicked!'),
  fallback: () => alert('Notifications are not supported by your browser.')
});
  1. notifyWithPermission(title, options ={...})

Description: Requests permission if not already granted, and displays a notification after permission is granted. If the user has already granted permission, it directly shows the notification.

The options object can contain the same properties as the notify method.

notifier.notifyWithPermission('Greetings!', {
  body: 'This notification is shown after permission is granted.',
  icon: 'https://example.com/icon.png',
  onclick: () => console.log('Notification clicked!'),
  fallback: () => alert('Your browser does not support notifications.')
});
  1. notifyAfterDelay(title, options ={...}, delay)

Description: Schedules a notification to appear after a specified delay (in milliseconds).

This has the same options as the notify method, along with an additional delay parameter.

// Notify the user after 10 seconds
ss5Notify.notifyAfterDelay('Hey there!', {
  body: 'This is your 10-second reminder',
  icon: 'https://your-icon-url.com/icon.png',
  onclick: () => console.log('You clicked the notification!'),
  fallback: () => alert('Notifications not supported in this browser.')
}, 10000);

This method is useful when you need to notify users after a specific event or with a delay. You can use it to remind users of something after they interact with your site.

Browser Support

This library checks for the browser's notification API support. If the browser does not support notifications, you can provide a fallback option (e.g., an alert or other fallback mechanisms).

License

This project is licensed under the MIT License.