detecting when a page actually loads in a tab

7,351 views
Skip to first unread message

Paolo Casaschi

unread,
Jun 3, 2011, 2:36:02 PM6/3/11
to Chromium-extensions
hello,

in my extension I use the tab.onUpdated event to detect when a
different page is loaded on each tab (using
chrome.tabs.onUpdated.addListener).

I need to do that in order to clear some info I collect about the page
and start fresh for the new page that will be loading soon.

i just noticed that the even is fired also when the user clicks on a
"#somewhere" link on the page, that changes the URL, positioning the
browser view on a different part of the page but NOT actually
reloading the page. As a result, my event listener function is called
even if it should not; this is bad not only because some operation is
repeated, but also because in some cases some of my extension info
might be lost.

Remembering the URL and detecting when only the hash is changing might
not be enough, what if a user is forcing a reload of the page with a
different URL (something like typing the new hash manually and force
loading the page) or for the same URL for that matter.

What I'd need is a way to detect WHEN A PAGE IS ACTUALLY LOADED from
the network for that tab.

Any suggestion how to do that?

Boris Smus

unread,
Jun 3, 2011, 5:48:03 PM6/3/11
to Paolo Casaschi, Chromium-extensions
Sounds like you aren't checking for status == 'completed' in your callback. This link might be useful: http://stackoverflow.com/questions/2149917/chrome-extensions-how-to-know-when-a-tab-has-finished-loading-from-the-bg-page

- Boris


--
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.


Paolo Casaschi

unread,
Jun 3, 2011, 6:37:04 PM6/3/11
to Chromium-extensions
Actually I was checking for "loading".
I need to clear some variables that a content script is using. When
the "complete" status is received it's too late, the content script
already did its task.
I might have to re-write the whole content script also, but I hope for
something simpler than
1) register the event with a "loading" status
2) do what needs to be done in the content script
3) wait for the "complete" status and do something. if instead another
"loading" is received do something else
this looks ugly


On Jun 3, 10:48 pm, Boris Smus <s...@chromium.org> wrote:
> Sounds like you aren't checking for status == 'completed' in your callback.
> This link might be useful:http://stackoverflow.com/questions/2149917/chrome-extensions-how-to-k...
>
> - Boris
>
> On Fri, Jun 3, 2011 at 11:36 AM, Paolo Casaschi <news...@casaschi.net>wrote:
>
>
>
>
>
>
>
> > hello,
>
> > in my extension I use the tab.onUpdated event to detect when a
> > different page is loaded on each tab (using
> > chrome.tabs.onUpdated.addListener).
>
> > I need to do that in order to clear some info I collect about the page
> > and start fresh for the new page that will be loading soon.
>
> > i just noticed that the even is fired also when the user clicks on a
> > "#somewhere" link on the page, that changes the URL, positioning the
> > browser view on a different part of the page but NOT actually
> > reloading the page. As a result, my event listener function is called
> > even if it should not; this is bad not only because some operation is
> > repeated, but also because in some cases some of my extension info
> > might be lost.
>
> > Remembering the URL and detecting when only the hash is changing might
> > not be enough, what if a user is forcing a reload of the page with a
> > different URL (something like typing the new hash manually and force
> > loading the page) or for the same URL for that matter.
>
> > What I'd need is a way to detect WHEN A PAGE IS ACTUALLY LOADED from
> > the network for that tab.
>
> > Any suggestion how to do that?
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Chromium-extensions" group.
> > 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.

joelpt

unread,
Jun 4, 2011, 11:54:00 AM6/4/11
to Chromium-extensions
I think you can get what you're after by having a sendRequest() fire
directly from your content script.

In content_script.js:
chrome.extension.sendRequest({request: {op:
"pageLoadStarted"}});

In background.html:
chrome.extension.onRequest.addListener(function(request, sender,
sendResponse) {
console.log('onRequest: ' + JSON.stringify(request));
console.log(JSON.stringify(sender));
});

This fires right after tabUpdated and whenever a page starts loading.
It only fires when the page is actually re/loaded from the web.

Alternately, check out the webNavigation.onBeforeNavigate event in the
Chrome experimental APIs.

Joel

Adam

unread,
Jun 6, 2011, 4:27:14 PM6/6/11
to Chromium-extensions
I think a content script is the way to to.

But make sure to specify in your manifest file to run the content
script from "document_start"

Then in your content script you can use sendRequest immediately, then
another sendReuqest onDOMContentLoaded and onLoad. That should give
you all the info you need.

-Adam
Reply all
Reply to author
Forward
0 new messages