replace-all-text-nodes
v1.0.1
Published
replace all current and new text nodes on your page
Downloads
3
Readme
replace-all-text-nodes
Does what the name says. Replace all text nodes on the page (i.e. all text on the page), with an option to use a MutationObserver or repeat full-page replacement periodically to continue replacing text for new elements added via javascript.
Created for use in imposters and concentraitor.
API
function replaceAllFromNode(node, replaceFn, opts) {
node
: the node to begin the tree search at. This works recursively downwards.replaceFn
: a function which replaces text. This can be done in any way desired.opts
: an object with any of the following keys:inputsToo
: if truthy, also modifies any input boxes. Note that this may cause performance issues or unexpected UI behavior as the user will have their text replaced while typing.notNow
: doesn't immediately callreplaceAll
timeouts
: array of milliseconds, at which replaceAll will run over the document again. These milliseconds are all relative to the point when this function was called, not to each other.repeat
: milliseconds to repeat runningreplaceAllFromNode
(this is not encouraged; aMutationObserver
used byfutureNodesToo
is preferable for performance reasons unless supporting older browsers which do not supportMutationObserver
)futureNodesToo
: adds a MutationObserver to the page, mutating all elements added to the page. If this is specified, the function returns theMutationObserver
used so that it can be started and stopped as desired.
function replaceAllInPage(replaceFn, opts) {
Convenience method which calls replaceAllFromNode
with node
set to document
.
function watchFutureNodes(replaceFn, opts) {
Implementation of futureNodesToo
in replaceAllInPage
. Creates and returns a MutationObserver
which applies replaceFn
to any added or modified text nodes when started.
To Use
Installation:
npm install --save replace-all-text-nodes
browserify -r replace-all-text-nodes your-script.js -o output.js
In your-script.js:
var ReplaceNodes = require('replace-all-text-nodes');
ReplaceNodes.replaceAllInPage(...);