2

I'm writing chrome extension for a site. This site has an infinite scroll feature which loads additional details in a div as the user scrolls. I'm writing a chrome extension that can automatically scroll to the bottom of the document and load all the details for the user, the extension keeps scrolling till no more additional details are available. The extension works perfectly when the tab is active, however when the tab or the chrome window is not active, the extension merely scrolls to the bottom of the page and no additional details are loaded in the div till the tab becomes active again.

Is there a way I can force chrome to load the additional details even when the tab is inactive ? I have tried using window.focus() or element.focus(), however these do not work.

matthias_h
  • 11,356
  • 9
  • 22
  • 40
bluenile
  • 5,673
  • 3
  • 16
  • 29

1 Answers1

3

That's because the infinite scroll function checks if the page has been scrolled all the way down its bottom using an event listener, and, if so, loads new elements. If the tab is not active, the onscroll listener will not be executed, so the infinitescroll function will not load new elements.

If you want to load more elements you have to find and call the function of the infinitescroll that loads the elements, so you don't need to scroll down and trigger the event listener.

To call the function you'll need to inject the code into the page context. If you don't know how to do it, you'll find this answer useful (thanks Xan for the link).

Community
  • 1
  • 1
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 1
    Addendum: to call the function you will need [injection into the page context](http://stackoverflow.com/questions/9515704/building-a-chrome-extension-inject-code-in-a-page-using-a-content-script). – Xan Oct 12 '14 at 19:45
  • @Xan Thanks a lot, added the link to the answer – Marco Bonelli Oct 12 '14 at 19:55
  • @MarcoBonelli the JavaScript on the webpage is very complex and I'm unable to determine which function is loading the elements and what parameters are being passed to the function. Is there any alternative way that might trigger the function? – bluenile Oct 12 '14 at 23:10
  • @bluenile unfortunately I don't think so. You can Try firing an event using the jQuery.trigger method, but I don't really think it would work. Given that, you should still find the right event (and the right properties) to trigger, and that's not very easy. – Marco Bonelli Oct 13 '14 at 07:32
  • 1
    @MarcoBonelli Thanks for your help, I really appreciate your help. Your guidance made me learn many new things. The issue was solved by raising the scroll event by injecting the following code - var evt = document.createEvent('HTMLEvents'); evt.initEvent('scroll', false, true); document.getElementById('profiles').dispatchEvent(evt); – bluenile Oct 14 '14 at 18:33