ryohidaka / chrome-notifications

Helper for chrome.notifications API.

Home Page:https://www.npmjs.com/package/chrome-notifications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

chrome-notifications

npm version build License

ko-fi

Overview

Helper for chrome.notifications API.

Notes

To use the chrome.notifications API, declare the "notifications" permission in the manifest:

{
  "name": "My extension",

  "permissions": ["notifications"]
}

Installation

You can install this library using npm:

npm install chrome-notifications

Methods

create

Creates and displays a notification.

import { Notifications } from "chrome-notifications";

const options = {
  type: "basic",
  title: "My Notification",
  message: "This is a sample notification",
  iconUrl: "icon.png",
};

Notifications.create(options, "my-notification-id", (notification) => {
  console.log(notification);
});

// Output:
//   Object{
//     notificationId: "38fde96f-43b1-4736-969e-2da10417c024",
//     type: "basic",
//     title: "My Notification",
//     message: "This is a sample notification",
//     iconUrl: "icon.png"
//   }

update

Updates an existing notification.

import { Notifications } from "chrome-notifications";

const notificationId = "38fde96f-43b1-4736-969e-2da10417c024";
const newOptions = {
  title: "Updated Notification",
  message: "This is an updated notification",
};

Notifications.update(notificationId, newOptions, (notification) => {
  console.log(notification);
});

// Output:
//   Object{
//     notificationId: "38fde96f-43b1-4736-969e-2da10417c024",
//     type: "basic",
//     title: "Updated Notification",
//     message: "This is an updated notification",
//     iconUrl: "icon.png"
//   }

clear

Clears the specified notification.

import { Notifications } from "chrome-notifications";

const notificationId = "38fde96f-43b1-4736-969e-2da10417c024";

Notifications.clear(notificationId, (wasCleared) => {
  console.log(wasCleared);
});

// Output: true

getAll

Retrieves all the notifications of this app or extension.

import { Notifications } from "chrome-notifications";

Notifications.getAll().then((notifications) => {
  console.log(notifications);
});

// Output:
//   Array [
//     Object {
//       notificationId: "131aa054-c1b3-485b-a200-83bdcba8421c",
//       active: true
//     }
//   ]

getPermissionLevel

Retrieves whether the user has enabled notifications from this app or extension.

import { Notifications } from "chrome-notifications";

Notifications.getPermissionLevel().then((permissionLevel) => {
  console.log(permissionLevel);
});

// Output:
//   Object{
//     level: "granted",
//     isGranted: true,
//     isDenied: false
//   }

Link

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Helper for chrome.notifications API.

https://www.npmjs.com/package/chrome-notifications

License:MIT License


Languages

Language:TypeScript 93.1%Language:JavaScript 5.6%Language:Shell 1.3%