0

I'm trying to get the URL changed event on same tab, occurred on link click by the user.

I tried with followings in content script, but having some issues with them.

01-

document.addEventListener('DOMContentLoaded',     
function () {
    //   My code here..
});

02-

window.addEventListener('load', function () {
    //   My code here..
});

03-

function nodeInsertedCallback(event) {
   alert('NICE');
};
document.addEventListener
('DOMNodeInserted',nodeInsertedCallback);

01 and 02 are works fine on first URL navigation and page refresh, but not raises on link click by the user, and page navigates to another URL.

03 Fires many times per page load, and many times on user link click.

I'm looking for event which triggers only once when the tab changes it's URL by user action, or page refresh.

Is there any better solution than these? Any help would be appreciated.

Ruwan Liyanage
  • 333
  • 1
  • 13
  • 1
    When a new page loads in the same tab your old content script will be terminated, and a new instance of the content script will run, so there's no need for such events. However, if the site is a SPA (Single-Page Application) the page doesn't reload so you should use different approaches, see [Is there a JavaScript / jQuery DOM change listener?](https://stackoverflow.com/a/39508954) – wOxxOm Feb 01 '21 at 16:56
  • @wOxxOm I got some issues with other parts of the extension using the solution in the link, elements I added are started to duplicating? – Ruwan Liyanage Feb 02 '21 at 01:32
  • 1
    Then check first if the elements are present and don't add them if they are. – wOxxOm Feb 02 '21 at 06:09
  • @wOxxOm Thank you. I could solve it by cleaning old stuff from the page. – Ruwan Liyanage Feb 02 '21 at 08:38

0 Answers0