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???
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.