Johnny
Johnny

Reputation: 127

chrome extension chome.tabs.onUpdate running twice?

//In background.js
chrome.tabs.onUpdated.addListener(function(tabid, info, tab) {
    console.log(info);
});

I'm try to set it up so onUpdate runs once every time a youtube video page loads. I want wait for load after a youtube video loads. When I load directly from a url I get what I expect, but when I navigate to a suggested video, there are 2 loading/complete cycles. One for the current video, One for the next video.

Direct Url

This is the output for a direct url youtube video.

Navigating

This is what happens if I navigate to a youtube video while watching another youtube video.

In my manifest I have a simple background.js script, permission for webNaviagtion and Tabs.

I don't know why it's running twice.

Upvotes: 1

Views: 1580

Answers (1)

woxxom
woxxom

Reputation: 73856

tabs.onUpdated is invoked for each frame/iframe, the Youtube watch page loads one.

webNavigation API provides frameId to the event listeners, the main page has frameId == 0.

Note that Youtube doesn't reload the page when the user goes from one video to another or from the main Youtube page to a video page so to detect these changes you may use chrome.webNavigation.onHistoryStateUpdated event listener or a content script that will listen to the DOM events used by the site, namely "spfdone".

Upvotes: 2

Related Questions