Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@thanhnn/react-chrome-extension-helper

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@thanhnn/react-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.

  • 0.1.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

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.

FAQs

Package last updated on 12 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc