Prompts the user for text to find and text to replace it with, then performs a case-insensitive search and replace on all text content of the current page.
This bookmarklet allows for quick, client-side modification of text content on any webpage. It's useful for:
- Correcting common typos or substituting terms for readability or analysis.
- Temporarily altering text for screenshots or demonstrations.
- Seeing how a page looks with different terminology.
- A simple way to play with webpage content (changes are client-side only).
- User Prompts: Uses
prompt()to ask for the search term and the replacement term. - Recursive DOM Traversal: Searches through all text nodes within the
document.body. - Case-Insensitive Global Replace: Uses a regular expression with
giflags to replace all occurrences, ignoring case.
- Easy Mode:
- Go to the Bookmarklet Installer
- Drag the generated link to your bookmarks bar.
- Hard Mode:
- Copy the entire JavaScript code from the find_and_replace.js file.
- Go to the generic Bookmarklet Installer.
- Paste the code into the installer.
- Name the bookmarklet (e.g., "Find/Replace Text").
- Drag the generated link to your bookmarks bar.
- Navigate to any webpage.
- Click the "Find/Replace Text" bookmarklet.
- A prompt will appear asking "Find:". Enter the text you want to search for.
- Another prompt will appear asking "Replace with:". Enter the text you want to use as a replacement.
- The bookmarklet will then iterate through the page content and replace all occurrences of the "find" text with the "replace" text.
replaceTextInHtml(find, replace, element)Function:- This is a recursive function. If
elementis not provided, it defaults todocument.body. - Creates a
RegExpobject from thefindstring withgiflags (global, case-insensitive). - Iterates through
element.childNodes:- If a child node is a text node (
Node.TEXT_NODE), it replaces itstextContentusingnode.textContent.replace(regex, replace). - If a child node is an element node, it calls
replaceTextInHtmlrecursively with that child element.
- If a child node is a text node (
- This is a recursive function. If
- User Input:
- Prompts the user for
textToFindusingprompt("Find:"). - Prompts the user for
textToReplaceusingprompt("Replace with:").
- Prompts the user for
- Execution:
- Calls
replaceTextInHtml(textToFind, textToReplace)to start the process fromdocument.body.
- Calls
- Changes are client-side and temporary; reloading the page will restore the original text.
- The replacement affects only text nodes. It won't change text in attributes (like
alttext for images ortitleattributes) unless those attributes are directly rendered as text nodes by the browser in some unusual way. - Special characters in the
findstring might be interpreted as regular expression metacharacters. For a literal search, these would need to be escaped (though the current script doesn't do this). - If the user cancels either prompt (or enters nothing for "Find:"), the behavior might be unexpected (e.g., replacing empty strings or errors if "Find:" is empty).
The full source code is available on GitHub.
MIT License - See LICENSE
Created by Oskar Austegard