0

trying to replace parts of html code in a page. Unfortunately the parts are multiline and I can't use specific div ids/class since they are used in the same page elsewhere (can't change that, not my fault)

For example:

<div class="c2c" style="display: none;">
  <div class="left" style="height:12px;">
    <span >A test</span>
  </div>

That is with breaks and spaces..

I have tried this

document.body.innerHTML= document.body.innerHTML.replace(/<div class=\"c2c\" style=\"display: none;\">(\r\n|\r|\n)?\s*<div class=\"left\" style=\"height:12px;\">(\r\n|\r|\n)?\s*<span >A test<\/span>/g,"<div class=\"c2c\" style=\"display: block !important;\"><div class=\"left\" style=\"height:12px;\"><span >A test<\/span>");

and also some other variations but with no result..

any ideas?

Thank you

N.K.K.
  • 3
  • 2
  • Your code will be breaking sites that add event listeners programmatically. Note that there's no need to replace HTML in this case: simply add a CSS style (e.g. via GM_addStyle or create it yourself) with something like `.c2c:not(#\0) { display: block !important }` – wOxxOm Jan 07 '21 at 17:11
  • Thank you for your time However my code will run for a local device only on an IP like 192.168.1.x that has embedded code This embedded html code has only inline css and not an external css file (idiots) Also I need to replace other parts of the html like commented sections etc. So your solution I guess won't help me much. If someone knows why the regex in Greasemonkey doesn't work as it should please inform me – N.K.K. Jan 07 '21 at 18:58
  • My solution works regardless of whether an inline CSS was used. See examples of using GM_addStyle. – wOxxOm Jan 07 '21 at 20:49
  • Thank you once more for your input and your time. However since I have to change other parts too that are not CSS (the code I wrote above was an example only) and since those parts are multiline I have to look for another solution. – N.K.K. Jan 08 '21 at 06:23
  • Try [Javascript .replace command replace page text?](https://stackoverflow.com/a/7275856) – wOxxOm Jan 08 '21 at 06:27

0 Answers0