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

Daniel Herr
  • 16,897
  • 4
  • 42
  • 57
Johnny
  • 127
  • 1
  • 10

1 Answers1

2

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

Community
  • 1
  • 1
wOxxOm
  • 53,493
  • 8
  • 111
  • 119
  • Thanks for the reply. I just tried onHistoryStateUpdate and I'm still getting it firing twice, both logs have a frameId of 0 as well. – Johnny Nov 21 '15 at 22:39
  • Which means that the history state was updated twice by the page. – wOxxOm Nov 21 '15 at 22:50
  • Ah okay, I guess my question now would be how do I set it up so it only runs on the page I'm going to. It runs for the current youtube url and then next url. http://imgur.com/lcKjZc6 – Johnny Nov 21 '15 at 22:53
  • Well you can check the tab's current url with `chrome.tabs.get(details.tabId, ....)` - if both are equal then it's the new page url or use some other way. – wOxxOm Nov 21 '15 at 22:57
  • Or. Youtube use history.pushState API. and chrome.tabs.onUpdate doesn't handle it yet. See http://crbug.com/465709 – Sungguk Lim Nov 22 '15 at 18:54