I am working on a user script and I’ve just found that the script is not run when the main page makes AJAX requests.
Is there any way to fire the user script both on main page load and on AJAX requests?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The smart way to rerun the script’s code on AJAX requests, is to focus on the key bits of the page and check for changes.
For example, suppose a page contained HTML like so:
and you wanted the script to do something with each comment as it came in.
Now you could intercept all AJAX calls,
or listen for(deprecated), or useDOMSubtreeModifiedMutationObservers, but these methods can get tricky, finicky, and overly complicated.A simpler, more robust way to get ajax-ified content on a wild page is to poll for it using something like the
waitForKeyElementsfunction, below.For example, this script will highlight comments that contain “beer”, as they AJAX-in:
Update:
For convenience,
waitForKeyElements()is now hosted on GitHub.This answer shows an example of how to use the hosted function.