
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
dom-mutator
Advanced tools

Apply persistent DOM mutations on top of anything (static HTML, React, Vue, etc.)
It's like using jQuery element.innerHTML = "My New Title"
, but it persists your change even if something external resets the HTML. Plus, it will automatically apply to any new matching elements added to the page later.
Features:
yarn add dom-mutator
OR npm install --save dom-mutator
.
import mutate from "dom-mutator";
// mutate(css selector, mutation type, value)
const stop = mutate("#greeting", "setHTML", "hello");
// works even if the selector doesn't exist yet
document.body.innerHTML += "<div id='greeting'></div>";
//**** div innerHTML = "hello" at this point!
// external changes are ignored and the mutation persists
document.getElementById('greeting').innerHTML = 'something new';
//**** div innerHTML = "hello" still!
// Stop mutating the element
stop();
//**** div innerHTML = "something new" (the last external value)
For setAttribute
, the "value" is in the format {attribute}="{value}"
(e.g. href="/about"
).
When you call mutate
, we start watching the document for elements matching the selector to appear. We do this with a single shared MutationObserver on the body.
When a matching element is found, we attach a separate MutationObserver filtered to the exact attribute being mutated. If an external change happens (e.g. from a React render), we re-apply your mutation on top of the new baseline value.
When stop
is called, we undo the change and go back to the last externally set value. We also disconnect the element's MutationObserver to save resources.
While the library is waiting for elements to appear, it runs document.querySelectorAll
every time a batch of elements is added to the DOM.
This is fast enough for most cases, but if you want more control, you can pause and resume the global MutationObserver.
One example use case is if you are making a ton of DOM changes that you know have nothing to do with the elements you are watching. You would pause right before making the changes and resume after.
import {disconnectGlobalObserver, connectGlobalObserver} from "dom-mutator";
// Pause
disconnectGlobalObserver();
// ... do a bunch of expensive DOM updates
// Resume
connectGlobalObserver();
Built with TSDX.
npm start
or yarn start
to rebuild on file change.
npm run build
or yarn build
to bundle the package to the dist
folder.
npm test --coverage
or yarn test --coverage
to run the Jest test suite with coverage report.
npm run lint --fix
or yarn lint --fix
to lint your code and autofix problems when possible.
FAQs
For those times you need to apply persistent DOM changes on top of HTML you don’t control.
The npm package dom-mutator receives a total of 192,974 weekly downloads. As such, dom-mutator popularity was classified as popular.
We found that dom-mutator demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.