0Day Forums
Javascript - Search through the HTML of a site and replace it? - Printable Version

+- 0Day Forums (https://zeroday.vip)
+-- Forum: Coding (https://zeroday.vip/Forum-Coding)
+--- Forum: JScript (https://zeroday.vip/Forum-JScript)
+--- Thread: Javascript - Search through the HTML of a site and replace it? (/Thread-Javascript-Search-through-the-HTML-of-a-site-and-replace-it)



Javascript - Search through the HTML of a site and replace it? - bedrowse403298 - 07-24-2023

Well, just like my title says.
Is it possible to search through a whole page's HTML/CSS, and then replacing certain strings using JavaScript?

I tried to make something on my own but I'm doing it wrong.

var i, sig = document.getElementsByClassName('signaturecontainer');
for (i = 0; i < sig.length; i++)
{
var str = sig[i].innerHTML;
var n = str.replace(/< div style='250px;overflow:scroll;'/g, "< div > style='height:100%'");
}

Why I want to replace the string instead of using .innerHTML and just editing is due to that the div I want to change does not have an ID/Class.

This is what the line(s) I need changed:
><div class="after_content">

<!-- edit note -->
<blockquote class="postcontent lastedited">

Last edited by X; Today at <span class="time">06:32 AM</span>.


</blockquote>
<!-- / edit note -->



<div style="height:250px;overflow: auto;"> // <--- This one.
<blockquote class="signature restore"><div class="signaturecontainer">text here</div></blockquote>
</div>

</div>


RE: Javascript - Search through the HTML of a site and replace it? - Mrundispatchable261 - 07-24-2023

We'll no it is not impossible, with JavaScript you can parse an HTML document by locating the tags is that you are trying to change. For example

<div id="getThisChanged">change this text</div>

By executing this next line of JavaScript, you can change the text inside of the tag

document.getElementById('getThisChanged').innerHTML("text is now changed");

The text will be changed to "text is now changed"



RE: Javascript - Search through the HTML of a site and replace it? - brigantes295614 - 07-24-2023

This line will give trouble:

var new = str.replace(/< div style='250px;overflow:scroll;'/g, "< div > style='height:100%'");

- `new` is a reserved word. Change it to newstr, or whatever.
- Your regex pattern is unlikely to find anything: what it's searching for is not valid CSS
- The string you want to insert is not valid HTML/CSS. It should probably be `<div style='height:100%;'>`

It occurs to me that if you're just tweaking the styling then this is a clumsy way to go. You can change the styling directly with Javascript.

var i, sig = document.getElementsByClassName('signaturecontainer');
for (i = 0; i < sig.length; i++)
{
sig[i].parentNode.style.height = "100%";
}