dom-script-hijack

1.0.1 • Public • Published

dom-script-hijack

dom-script-hijack provides a simple and convenient way to hijack scripts on web pages. This package allows you to intercept and modify the behavior of existing JavaScript code executed within the context of a web page.

Installation

You can install dom-script-hijack using npm:

npm install dom-script-hijack

Usage

Once installed, you can import the hijackScript function from the dom-script-hijack package and use it to hijack scripts on web pages. The function takes two parameters: scriptToHijack and transformer.

The hijackScript function sets up a MutationObserver to listen for changes in the DOM. When a script matching the provided scriptToHijack is added to the page, the transformer function is called to modify its content. The original script is then removed, and a new script element with the modified content is inserted in its place.

Here's an example usage:

import { hijackScript } from 'dom-script-hijack';

hijackScript(
	/some-script.js/, 
	txt => txt.replace('console.log', 'console.info')
)

In the example above, the (first) script with a src matching some-script.js will be hijacked. The transformer function modifies the script content by replacing all occurrences of console.log with console.info.

Dependencies (0)

    Dev Dependencies (2)

    Package Sidebar

    Install

    npm i dom-script-hijack

    Weekly Downloads

    1

    Version

    1.0.1

    License

    0BSD

    Unpacked Size

    5.94 kB

    Total Files

    5

    Last publish

    Collaborators

    • alowaniak