1

I'm working on a Firefox add-on which needs to observe tab url updates made through history.pushState. I'm currently learning this whole WebExtension API for Firefox - it's my first add-on after Greasemonkey userscripts era.

I've tried using this slightly edited code (removed optional filter parameter) presented in the API documentation on MDN:

function logOnHistoryStateUpdated(details) {
    console.log("onHistoryStateUpdated: " + details.url);
    console.log("Transition type: " + details.transitionType);
    console.log("Transition qualifiers: " + details.transitionQualifiers);
}

browser.webNavigation.onHistoryStateUpdated.addListener( logOnHistoryStateUpdated );

This code works for me, when used in background script, with log messages displayed in browser console. What I want is have this code working in content script, but it does not. Simply, no log messages are printed in either console.

To be sure of what is going on, I've added this code:

console.log( typeof browser );
console.log( typeof browser.webNavigation );
console.log( typeof browser.webNavigation.onHistoryStateUpdated );
console.log( typeof browser.webNavigation.onHistoryStateUpdated.addListener );

printed values are as follow:

object
undefined

so, it seems, that webNavigation API is not available in content scripts, am I right or doing something wrong? I did add the webNavigation permission in the manifest (it's the same manifest file I used with working background script). I don't see anything on MDN saying that this API can be used only in bg scripts or can't in content ones.

Soul Reaver
  • 2,012
  • 3
  • 37
  • 52

1 Answers1

3

So it seems that the answer is 'no' or at least, nobody is willing to answer. I've decided to use this API in background script with messaging, so that content scripts will know about detected changes.

Soul Reaver
  • 2,012
  • 3
  • 37
  • 52
  • 1
    Same result in January 2022. When I use the `broswer` object, my Greasemonkey user script stops. However, someone discovered a website may register its own navigation events, which user scripts can listen for. [detect-page-navigation-on-youtube](https://stackoverflow.com/questions/34077641/how-to-detect-page-navigation-on-youtube-and-modify-its-appearance-seamlessly) – devdanke Jan 22 '22 at 00:33