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

easy-html-injector

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-html-injector

A simple HTML injector for Node.js

  • 1.0.6
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

easy-html-injector

Introduction

easy-html-injector is a simple Node module that allows you to inject HTML elements into an HTML string. The package exports a single function inject(html: string, element: HtmlElement) that takes an HTML string and an object representing an HTML element to be injected, and returns the modified HTML string with the new element inserted at the specified position.

Installation

To install easy-html-injector, use pnpm, npm, or yarn:

pnpm add easy-html-injector
npm install easy-html-injector
yarn add easy-html-injector

Usage

First, import the inject function:

import { inject } from 'easy-html-injector';

The inject function takes two arguments:

  • html: A string containing the HTML that you want to modify.

  • element: An object representing the HTML element that you want to inject. The object has the following properties:

    • parent: A string representing the parent element into which the new element will be injected. This can be 'head', 'body', or any other valid HTML element tag name.
    • position: A string representing the position at which the new element will be inserted. This can be 'prepend' or 'append'.
    • tag: A string representing the tag name of the new element.
    • content (optional): A string representing the content to be inserted into the new element.
    • attributes (optional): An object representing the attributes to be added to the new element. The keys of the object represent attribute names, and the values represent attribute values.

Here is an example usage:

const html = `
  <!DOCTYPE html>
  <html lang="en">
    <head>
      <title>My page</title>
    </head>
    <body>
      <h1>Hello, world!</h1>
    </body>
  </html>
`;

const newElement = {
  parent: 'head',
  position: 'prepend',
  tag: 'meta',
  attributes: {
    charset: 'utf-8',
  },
};

const modifiedHtml = inject(html, newElement);
console.log(modifiedHtml);

This will output:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>My page</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

License

easy-html-injector is released under the MIT License. See the LICENSE file for more details.

Benchmark

BenchmarkAverageMinMaxMedianStd DevVarianceOpsOps/sec
inject0.0160.00300.110.00430.0322.06263162630905
cheerio0.870.354.50.431.21.411471146707

FAQs

Package last updated on 13 Mar 2023

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