0
votes

I made a simple temporary addon:

manifest.json

{

  "manifest_version": 2,
  "name": "Content script test",
  "version": "1.0",

  "description": "TEST",

  "content_scripts": [
    {
      "matches": ["*://*.telex.hu/*"],
      "js": ["content-script.js"]
    }
  ]

}

content-script.js

console.log("*** Content script is running for " + document.location.href);

window.addEventListener(
        'load',
        function(){ console.log("*** Hello World!"); },
        true
    );

I tested it on FireFox and Chrome.

When I open a new browser tab, and load in a page, for example https://telex.hu, then my addon runs correctly (I see the logs in the developers console). If I right-click a link on the page then the selected page (same domain) opens in a new tab and the addon runs correctly. But if I left-click a link and open the selected page (same domain) in the same tab, then nothing happens - the addon doesn't run (no log shows up on the console)...

What is the explanation? And what should I do to make the addon run every case???

1

1 Answers

0
votes

The solution is:

content-script.js

let lastUrl = location.href; 
new MutationObserver(() => {
  const url = location.href;
  if (url !== lastUrl) {
    lastUrl = url;
    onUrlChange();
  }
}).observe(document, {subtree: true, childList: true});

function onUrlChange() {
  console.log('*** URL changed!', location.href);
}