-
-
Notifications
You must be signed in to change notification settings - Fork 282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Event or method to intercept page redirects/navigation #456
Comments
I'm interested in trying to tackle this - no idea if what I produce will be usable though. |
Unfortunately, having taken a brief look at the code available, I'm not sure this feature could be implemented without significant rewriting of the existing library, though I also don't have a complete grasp of how everything works yet. With so much of the navigation logic occurring in FFI/callbacks, there isn't really a way to pre-empt navigation or notify that a navigation has occurred. The best solution I can think of so far is to utilise a local proxy. This would ensure that all network requests made by the webview are captured (JavaScript and a custom protocol handler as described in the original issue would not cover cases where JavaScript inserts links after page load, for example). The good news is that it should be possible to implement inserting a proxy on Windows and Linux, the bad news is the macOS categorically has no way of doing this as proxying a WKWebView shows no signs of ever being implemented. Therefore, I'll be making an attempt at implementing passing a proxy value on Windows and Linux, but won't be pursuing a "proper" solution to this issue. |
My advice is to consider a cross platform approach that would work under specific circumstances. It does not need to work for everyone all the time, but it can be seen as one option under specific premises. This is the benefit of feature gating. |
Alright, I've given this another shot "properly". What I've discovered however is that this can only be done wit breaking changes in either wry or tao. This leaves two options, I believe:
Actually, now that I think about this, we could add a builder method on |
I've effectively got what I believe would be a working implementation of this, for windows at least. However, the approach I took does not seem to work due to the fact that the library is not actually multithreaded - that is, the thread executing the webview callback functions also appears to be the same thread that executes the event loop. This means that you cannot, as I attempted, pass a oneshot channel to the event loop whilst executing the (webview2) navigation event handler so that you could decide whether to cancel a navigation "at runtime". I'll continue implementing the event loop approach as I have (we can still deliver an event detailing a navigation that has already happened) but it will only be possible to cancel a navigation by defining preset conditions. I'll create a PR for this shortly - the Windows functionality will be the best tested/written as I don't have access to a linux or mac machine at the moment. |
I have opened PR #519 which would close this issue. It currently includes an implementation for Windows and a basic example. There's some open points of discussion I mention there that could use some clarification, |
Is your feature request related to a problem? Please describe.
I'd like to be able to detect or monitor when the URL of a webview changes, for instance, when clicking on a link. This can be helpful for implementing things like custom download behaviour.
Describe the solution you'd like
Either a new event describing the URL the current webview or a new webview has navigated to OR a function that can be called on a the WebviewBuilder which allows the registration of a callback which is executed whenever navigation ocurrs.
Describe alternatives you've considered
You could register HTTP/HTTPS as custom protocols, do the fetch yourself in rust then pass the resulting data through, though this seems a bit extreme if you want to just say log page navigations.
You could also potentially insert an
onbeforeunload
handler using thewith_initialization_script
builder function, though that might not catch certain navigations events or popups (which may themselves be another problem altogether). There could be a number of other ways to do this through pure javascript too.Would you assign yourself to implement this feature?
Additional context
There may be an obvious way to do this that I haven't seen, but I can't find it.
The text was updated successfully, but these errors were encountered: