Detecting page load completion across all frames

2,230 views
Skip to first unread message

Alex

unread,
Mar 1, 2010, 12:31:41 PM3/1/10
to Chromium-extensions
I have a content script that gets loaded for every frame in a page.
The content script looks through the DOM and gets back to the
background page with some info. The background page should then
generate a report based on findings from all content scripts from a
given tab.

How does one figure out when to display the report? That is, how do
you detect when all content scripts for a tab have finished processing
and gotten back to the background page?

One approach is to continuously update the report as new findings come
in, but it would be best to display the complete version once, as soon
as all content scripts finish reporting.

I could scan the top-level document to get a list of all frames, and
then wait until all frames have reported in, but what if there are
frames within frames? Is there a more direct way?

Mohamed Mansour

unread,
Mar 1, 2010, 2:50:03 PM3/1/10
to Alex, Chromium-extensions
Perhaps you can keep track when the content script is loaded, by sending a ping event to the background page telling it that we have one available content script for that tab. 

The background page keeps track on how many content scripts been loaded, then within your content script, you will listen for events from the background page via extension messaging http://code.google.com/chrome/extensions/messaging.html

Everytime the content script finishes fetching the data you need, it will "sendResponse" the data object back to the background page. You can keep counting how many responses you receive, if they are equal, you know that you have successfully processed all frames.

So, just tell your background page that I have a content script, then through extensions messaging, your content scripts listens for some sort of action your background page requests. And once the background pages receives all the ACKs, you can safely assume it has been processed.

-
Mohamed Mansour
m...@chromium.org



--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.


Alex

unread,
Mar 1, 2010, 5:02:10 PM3/1/10
to Chromium-extensions
I'm concerned about race conditions in this case. What if you get a
page with a slow-loading frame? The top-level document will finish
processing way before the frame does. This strategy will work if all
ping events are guaranteed to come in before any scan result events.
Would getting content scripts to run at "document_start" to send ping
events achieve this?

http://code.google.com/chrome/extensions/content_scripts.html:
"In the case of "document_start", the files are injected after any
files from "css", but before any other DOM is constructed or any other
script is run."

Or could the "document_start" for a slow-loading frame come after a
"document_idle"-triggered top-level document's content script. Then we
won't know how many scripts are out there to wait to receive info
from.


On Mar 1, 2:50 pm, Mohamed Mansour <m0.interact...@gmail.com> wrote:
> Perhaps you can keep track when the content script is loaded, by sending a
> ping event to the background page telling it that we have one available
> content script for that tab.
>
> The background page keeps track on how many content scripts been loaded,
> then within your content script, you will listen for events from the

> background page via extension messaginghttp://code.google.com/chrome/extensions/messaging.html


>
> Everytime the content script finishes fetching the data you need, it will "

> sendResponse<http://groups.google.com/a/chromium.org/group/chromium-extensions/bro...>"

> > To post to this group, send email to chromium-extensi...@chromium.org.


> > To unsubscribe from this group, send email to

> > chromium-extensions+unsubscr...@chromium.org<chromium-extensions%2Bunsubscr...@chromium.org>

Mohamed Mansour

unread,
Mar 1, 2010, 7:34:00 PM3/1/10
to Alex, Chromium-extensions
I thought the chrome.extension.onUpdate should fire the complete event when the whole page has been loaded while doing an example to show you.

Basically, the "complete" tab status event is being fired numerous times in "random" order, sometimes 1, 2 or 3. I noticed that the last content script callback is being fired before the last status change.

I filed the bug in the tracker:

I will see what the extension guys say.

-
Mohamed Mansour
m...@chromium.org


To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.

Alex

unread,
Mar 2, 2010, 11:29:07 AM3/2/10
to Chromium-extensions
Thank you for investigating further!

When you say chrome.extension.onUpdate, do you mean
chrome.tabs.onUpdated? Is the idea here that if the "complete" event
always fires after all the frames have loaded, this is an easy way to
get a count of all frames/content scripts in a tab?

I am going to try recursively scanning for frames first and going from
there once all frames have been detected.


On Mar 1, 7:34 pm, Mohamed Mansour <m0.interact...@gmail.com> wrote:
> I thought the chrome.extension.onUpdate should fire the complete event when
> the whole page has been loaded while doing an example to show you.
>
> Basically, the "complete" tab status event is being fired numerous times in
> "random" order, sometimes 1, 2 or 3. I noticed that the last content script
> callback is being fired before the last status change.
>
> I filed the bug in the tracker:http://code.google.com/p/chromium/issues/detail?id=37149
>
> I will see what the extension guys say.

>  <http://code.google.com/p/chromium/issues/detail?id=37149>

> > <chromium-extensions%2Bunsubscr...@chromium.org<chromium-extensions%252Bunsubscr...@chromium.org>

Alex

unread,
Mar 8, 2010, 12:51:56 PM3/8/10
to Chromium-extensions
So is chrome.tabs.onUpdated supposed to receive a status of "complete"
when the entire tab, frames included, finishes loading? But instead,
according to the bug filed above, it sometimes fires before one or
more frames finish, and sometimes fires multiple times. Is this right?

Mohamed Mansour

unread,
Mar 8, 2010, 2:02:13 PM3/8/10
to Alex, Chromium-extensions
As far as I know, it should, but don't take my word. Maybe once this bug gets resolved (http://code.google.com/p/chromium/issues/detail?id=37149) it will be functioning again. No owners so far for that bug.

-
Mohamed Mansour
m...@chromium.org


Reply all
Reply to author
Forward
0 new messages