2

I'm currently working on a Chrome extension that modifies content on a user's Tumblr dashboard, which uses infinite scrolling. However whenever the use scrolls down, a function needs to run again.

Here's a basic run-down of how I've got it working right now:

  • User loads page
  • Extensions modifies elements on page
  • User scrolls down
  • Triggers infinite scrolling
  • Next page loads below current one
  • More content loads

After that final step, I need step 2 to trigger again and have the new content modified.

I've tried .binding elements such as the entire <body>, the container div around the elements, and to no avail.

How do I trigger a function so that it runs when the content of a page changes (specifically the Tumblr dashboard)?

jQuery is fine, by the way.

Joshua Merriman
  • 925
  • 1
  • 8
  • 13

2 Answers2

2

You should set up a MutationObserver in your content script to watch for insertions of elements you want to modify.

See this question for more details.

Also, the Mutation Summary library might work well in your case.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
0

You can try jQuery.ajaxComplete. It runs whenever there is an ajax request completed. You could have something like

$( document ).ajaxComplete(function( event, xhr, settings ) {
    if (settings.url === 'tumblr.com/update') { //obviously change the update url
        //do your thing
    }
});

Of course the best way would be to find the actual function that gets fired on the scroll and modify it to fire yours on its success. But give that a shot.

jbarnett
  • 984
  • 5
  • 7
  • A bit of a problem there that normally extension code runs in a separate context, so jQuery instance (if even present) won't receive those events. It's possible to inject into the page context, but it's an additional layer of complexity. – Xan Nov 29 '14 at 22:18
  • @jbarnett "the best way would be to find the actual function that gets fired on the scroll and modify it to fire yours on its success." How would I go about doing that? – Joshua Merriman Nov 30 '14 at 06:55