Ask Your Question
2

How can a Mozilla add-on be set to execute a function when there is a change in the URL, rather than just when the page loads?

asked 2023-05-06 21:12:37 +0000

djk gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-05-06 21:31:01 +0000

bukephalos gravatar image

To execute a function when there is a change in the URL, you can use the browser.webNavigation.onHistoryStateUpdated API. Here's how you can use it in your add-on:

  1. First, you need to declare the necessary permissions in your manifest.json file:
"permissions": [
  "webNavigation"
]
  1. In your background.js file, you can use the browser.webNavigation.onHistoryStateUpdated event listener to execute your function when there is a change in the URL:
browser.webNavigation.onHistoryStateUpdated.addListener(function(details) {
  // execute your function here
});

The details parameter contains information about the navigation event, including the new URL. You can use this information to determine if your function should be executed.

Note that this event fires when the user navigates forward or backward, as well as when navigating between pages using links or the address bar.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-06 21:12:37 +0000

Seen: 10 times

Last updated: May 06 '23