1

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:

  1. 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.

  1. 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:

  1. Is it possible to delay the navigation? setTimeout in js is async so the navigation is not blocked in any way.
  2. 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)?
PaulG
  • 13,871
  • 9
  • 56
  • 78
danny barlev
  • 224
  • 1
  • 4
  • 1
    The only way is to override the navigation function used by the site and do it in the [page context](/a/9517879). For example you can hook `History.prototype.pushState`. – wOxxOm Oct 17 '19 at 08:26

1 Answers1

0

I can suggest you a fallback.

Will it work if you save element x and y coordinates just before applying the selector generation function so when it fails, you can go back to previous route and apply the selector generation on element with saved x and y coordinates ?

It may work only if selector generation function is the bottleneck.

eugeneK
  • 10,750
  • 19
  • 66
  • 101