0

I'm working on a Chrome Extension and would like to have some part of the website's content be replaced.

So the website's HTML is: <div id="ad" class="ad-container "></div> they have multiple 'id', but the class name remains the same.

The code I want to replace it with is: <script src="link"></script>

I was looking into something called 'InnerHTML", and saw this example: document.body.innerHTML = document.body.innerHTML.replace('hello', 'hi'); but I'm not sure how to replace it with HTML, or change it so it works with a script tag.

Any help?

7
  • 2
    Remind me not to download your "extension" Commented May 19, 2020 at 12:42
  • 2
    So, you want to recreate adblock? Why not have a look at their code, its open source... Commented May 19, 2020 at 12:54
  • @RenevanderLende you have a point. thanks!! Commented May 19, 2020 at 13:01
  • @Liam haha! it's blocking ads, nothing bad! Commented May 19, 2020 at 13:02
  • I think @Liam meant that you are creating a 'some extension' but don't know basic JS, contemplating your final result.... Commented May 19, 2020 at 13:04

1 Answer 1

1

That's scary though, be careful doing that.

for(let element of document.querySelectorAll('.ad-container')){
  let script = document.createElement('script')
  script.innerHTML = element.innerHTML
  document.body.appendChild(script)
  element.remove()
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.