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

@thanhnn/react-chrome-extension-helper

v0.1.2

Published

A helper library to streamline the development of Chrome extensions using React. This package provides utilities for handling context menus, injecting React components into the DOM, and managing messages between Chrome tabs.

Downloads

164

Readme

Chrome Extension Helper

A helper library to streamline the development of Chrome extensions using React. This package provides utilities for handling context menus, injecting React components into the DOM, and managing messages between Chrome tabs.

Installation

Install the package with npm or yarn:

npm install chrome-extension-helper
# or
yarn add chrome-extension-helper

Getting Started

1. Setting Up Context Menus

Define custom context menus with the addContextMenu function:

import { addContextMenu } from 'chrome-extension-helper';

addContextMenu('MY_ACTION', 'My Action Title', { contexts: ['all'] });

2. Injecting React Components

Easily inject React components into the DOM with the injectReact function:

import { injectReact } from 'chrome-extension-helper';
import MyComponent from './MyComponent';

injectReact({
  id: 'my-react-component',
  component: <MyComponent />,
  css: true, // Optional: inject default CSS from 'css/main.css'
  cssString: '.my-class { color: blue; }', // Optional: custom CSS
  fontUrl: 'https://fonts.googleapis.com/css2?family=Roboto&display=swap', // Optional: custom font
});

3. Removing Injected React Components

Remove injected components with the removeReact function:

import { removeReact } from 'chrome-extension-helper';

removeReact('my-react-component');

4. Sending Messages

Communicate between Chrome tabs and the background script using sendChromeMessage:

import { sendChromeMessage } from 'chrome-extension-helper';

sendChromeMessage({
  type: 'MY_ACTION',
  data: { foo: 'bar' },
  callback: (tabs, response) => console.log('Response:', response),
});

5. Setting Up Background Scripts

Use setupBackgroundExtension to register context menus and listeners in the background script:

import { setupBackgroundExtension } from 'chrome-extension-helper';

setupBackgroundExtension([{ id: 'MY_ACTION', title: 'My Action Title' }]);

6. Setting Up Content Scripts

Configure message listeners with setUpContentExtension:

import { setUpContentExtension } from 'chrome-extension-helper';

setUpContentExtension([
  {
    action: 'MY_ACTION',
    callback: ({ data, sendResponse }) => {
      console.log('Received data:', data);
      sendResponse({ success: true });
    },
  },
]);

7. Keyboard-Controlled Component Removal

Easily unmount components with a specified key using the useRemoveReact hook:

import useRemoveReact from 'chrome-extension-helper';

const { onClose } = useRemoveReact('my-react-component', { keyCode: 'Escape' });

// You can also manually call `onClose()` to remove the component.

Types

This library provides the following TypeScript types for ease of use:

  • ChromeMessage<T>: Represents a message sent between Chrome tabs.
  • ChromeActionConfig: Configuration for actions handled in content scripts.
  • ChromeContextMenuConfig: Configuration for custom context menus.