crzyonez777 crzyonez777 - 2 months ago 9x
Javascript Question

Chrome extension - "webNavigation.onComitted" event is fired twice

I'm developing a chrome extension now.

I came across this problem that "webNavigation.onComitted" event is fired twice on when I access facebook.

This is the code I have now, but it prints "Facebook" twice in console.

// while logging in, it prints "facebook" in console.
chrome.webNavigation.onCommitted.addListener(function(e) {


}, {url: [{urlPrefix : ''}]})

On the other hand, this code works ok.
When I access, it prints "youtube" just once in console as I expect.

// on accessing, it prints "youtube" in console.
chrome.webNavigation.onCommitted.addListener(function(e) {


}, {url: [{urlPrefix : ''}]})

Anyway, I want it load only once when accessing

How can I make it work??

If anyone knows how to fix this problem,please help me out!!

Thanks in advance!!

What I tried

I tried all other event types such as "onBeforeNavigate", "onCompleted".
But it doesn't solve my problem.

Useful related post on stackoverflow

Chrome extension, javascript: Why is this firing twice?

This page is similar to my problem but, this page is talking about "tabs.onUpdated" event.

since "tabs.onUpdated" event doesn't provide event filter, I prefer using "webNavigation" API.



frameId ( integer )

0 indicates the navigation happens in the tab content window; a positive value indicates navigation in a subframe. Frame IDs are unique within a tab.

If you add console.log(e) inside your event handler, you'll see that the first event has frameId = 0, while the second event has a positive number, which means it belongs to an iframe. If you are only interested in main window navigation, just add a check for e.frameId === 0 at the beginning of your event handler.