GithubHelp home page GithubHelp logo

chrome-extension-message's Introduction

Chrome Extension Message System

This repository shows how to make requests between content, background and popup scripts of a chrome extension.

How to build?

Build the script once.

npm i
npm run build

Recompile in save:

npm i
npm start

In chrome go to "chrome://extensions", press Add Extracted Extension (or something like that, I don't know the english translation) and select the build folder which has appeared in the directory.

The main Concept

The main idea is that every script has a router which can send messages or tasks to other routers.

import { Router, Destinations, Task } from "./connection"
const router = new Router(Destinations.BACKGROUND)

If you want to use this system the background script must always have exactly one router, which works as a root node in a star topology. Therefore it is the broker for every other router.

Routers can send messages to each other or tasks. A task has an ID and a payload:

const task = new Task("ID", { /* payload */ })

It can be executed by specifying a target and passing it to the router:

const dest = router.run(Destinations.CONTENT)
const result = (await dest(task)).getPayload()

It will return a message from the handler. A handler can be specified like this:

router.handle("ID", async (task) => {
	/* do something */
	return { /* response */ };
})

This handler waits for the task with the id "ID". Commonly you would place the handler e.g. in the background script and run the task from the content script.

chrome-extension-message's People

Contributors

pfeifenjoy avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.