1

From my experience, a content script that matches a url in the manifest will fire right away when going on the url. Is there anyway to choose when that content script will fire, and if it is possible to re-fire a content script more than once?

Right now I want to change the text of data generated in a pagination. So page 1 of the pagination would be successfully changed when I go on the url. After clicking on page 2, since the page doesn't refresh when changing the data on the pagination, every other page thats not the first page will remain unchanged

keurig
  • 79
  • 7
  • 1
    See also [Is there a JavaScript / jQuery DOM change listener?](https://stackoverflow.com/a/39508954) – wOxxOm Apr 15 '20 at 06:42
  • @wOxxOm I have a question about best practices if you are able to help that is related to why I want to fire a content script more than once but is not worth another thread. I want to inject html on a certain element and it works when it first loads but the parent element gets re-rendered via AJAX so it won't be there after I click a button, is it okay to use MutationObserver to check if the parent element is missing my injected html and then keep injecting my html in the MutationObserver listener for loop if it's not there? I am able to do this but wondering about good/bad practice. – keurig Apr 16 '20 at 06:04
  • 1
    Use devtools performance profiler to see if your code causes a noticeable slowdown. If it does then try [optimizing your observer](https://stackoverflow.com/a/39332340) or find another trigger like a [page event](https://stackoverflow.com/a/34100952). – wOxxOm Apr 16 '20 at 06:34

1 Answers1

1

In my extensions a preferred the earliest time possible with run_at to inject the script. In the script itself i check for the things i need.

MutationObserver could be a thing for this`to check for DOM modifications.

Brain Foo Long
  • 2,057
  • 23
  • 28