I'm using Chrome extension and want to run a function that generate selector from the element the user clicked on.
On static pages it's working, the problem is, when the user clicks on a link in SPA site, the route is faster then the function, and then the DOM changes before the function generates the selector.
What I tried so far:
- I tried to intercept the navigation in the background page using this extension API
chrome.webNavigation.onBeforeNavigate.addListener(function callback).
It intercepts it properly, but I can't find any way to delay the navigation. The navigation is still happening as usual.
- I also tried to block the request using webRequest extension API, but I can't disable the blocking after the function finishes running. (I don't believe that this is the proper way)
My questions:
- Is it possible to delay the navigation? setTimeout in js is async so the navigation is not blocked in any way.
- Is there any other way to wait for function to finish running before the route changes (promises don't help because the route still happens before it get resolved)?