Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run a Greasemonkey function after an AJAX update?

I have a GM script that adds buttons next to certain hyperlinks on Facebook pages. However, any AJAX update nukes the buttons in that section of page (i.e. when you click 'view more comments' - all the buttons on that thread vanish)

What I desire to do:
Re-run my GM function after certain AJAX operations complete - mainly when viewing more comments & loading a new page (I.e click to a different group page, this is all AJAXed)

what I currently do:
Have a 30 second loop that just re-runs my code every 30 seconds. not ideal. Setting a shorter loop will just cause lag
Edit - it actually makes more and more buttons. Super not ideal!

How you can help:
what code should I use to trigger my function after AJAX finishes loading something? What should I be doing so that my code only fires upon my desired AJAX updates, such as the 'new page' & 'more comments' updates?
Edit - I need to re-run my code only on the updated bit (is this even possible?)

Notes:
I have searched a bit and find a number answers for plain jQuery, asp.net, etc., but not the GM-jQ combo.

like image 265
RozzA Avatar asked Apr 13 '12 03:04

RozzA


1 Answers

This is a common problem.

Use the waitForKeyElements() function as shown in this answer.

By default, it will handle every new element, of the type that you specify via jQuery selector, once each, and within milliseconds of that new element appearing.

~~~
Link to the target page and specify exactly what you are doing for a more specific application.

like image 122
Brock Adams Avatar answered Sep 30 '22 14:09

Brock Adams