I've made two different attempts to replace all occurrences of a single word from a webpage using JQuery in a Chrome extension. Both attempts kind of worked, but neither worked as a universal method for replacing all occurrences of a word from a webpage.
How can I write a script that replaces all occurrences of a certain word in a webpage?
Read on for details of the 2 different attempts which both kind of worked but failed for different reasons.
Attempt 1: Replace the text of nodes that have no children. This fails in cases where child nodes are used for formatting. For example, it fails when parsing the following:
<p>StringToReplace <strong>doesn't</strong> get replaced!</p>
The exact code I used for this attempt is:
$("*").each(function () {
if ($(this).children().length == 0) {
$(this).text(replaceStrings($(this).text()));
}
}
(replaceStrings is a separate function with a bunch of arbitrary calls to replace)
Attempt 2: Replace the HTML of nodes that probably only contain text (e.g. p
). This fails because some of the webpages my script needs to work on have text appearing right inside tags like body. If I try replacing the HTML of body, it has an undesired side-effect of breaking functionality on some pages. IMO it would be a nightmare to try to handle every edge case where site functionality is impaired by replacing HTML of body
or div
s high up the DOM tree.
The code I used for this second attempt:
$("*").each(function () {
if (
$(this)[0].nodeName.toLowerCase() == "font"
|| $(this)[0].nodeName.toLowerCase() == "span"
|| $(this)[0].nodeName.toLowerCase() == "p"
//|| $(this)[0].nodeName.toLowerCase() == "body"
// || $(this)[0].nodeName.toLowerCase() == "div"
){
$(this).html(replaceStrings($(this).html()));
}
}
How can I write a script that replaces all occurrences of a certain word in a webpage?
Thanks!