-
Notifications
You must be signed in to change notification settings - Fork 55
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
Expose programmatic access to the travellog #1093
Comments
As I could not find anything I played around with it. As in the picture I wanted to present to the user the available pages to GoBack or GoForward. Using Navigation and Frame Complete events I am pretty sure that I am catching the history. One complication is to work out where the user is in my generated history list. For example if the user just keeps clicking links then everything is OK as all the history is for the back button. However as soon as a user chooses to GoBack then you need to split your history between Back - Current - Forward. For example if the user navigates from Bing, then to Github, then to Microsoft and then to Stack Overflow my history list would be; 0,1,2 would all show on a list for GoBack. 3 is the current page and would not show on any list. If the user clicks the GoBack button ONCE then the history list would look like this. 0,1 would be on the GoBack list. 2 is the current page. 3 would now show on a forward list. The Problem The expectation after choosing "0 - Bing" would be But the result because I use the Navigate method is this; WebView2 resets its History and now CanGoForward returns false. The alternative would be to call GoBack multiple times. This would step WebView2 back in its history but would mean you have to navigate to all the pages in between. I really hope I am missing something in my logic or in the API. If I am not, then giving users a history list to choose from will not work so well at least not as they would expect. Edit: Of course one can use Javascript window.history.go(-2) etc. |
Hey @darbid - you are right, we don't currently have the API available that would make this straightforward, and the current workaround is to use the HTML5 History API + Navigation events to build the list. I don't think we've specifically gotten a request to expose the travellog in the past - do you want me to open this as a scenario on our backlog? |
@champnic my use case is to have a session history so as to mirror what normal browsers do when right clicking or long clicking on the back and forward buttons, however, I do recognize this is not a must have feature and def would have a very low priority for the development team. At best this is a nice to have. The issue I am seeing is being able to sync the history of the browser with my history. One example is This Android site. If you choose the menu items at the top there is no navigation (Start and Complete) but there are HistoryChanges and DocumentTitle changes. However, at the time of the History Change it is not clear what the Document Title is (it can be empty, old or something totally different). I am currently assuming that a document title change and History Change without a navigation starting is something I need to save for my session history. Further the HistoryChange event exposes nothing. It is up to the developer to get the URL at that point in time. Given the async way WebView2 works it is possible (but I have not experienced it yet) that by the time the Source property is read that it is not the same as the URL which was the reason for the HistoryChange. HTML5 History API Other APIs Suggestion on Developer Text would it be possible to Remark on why this event is fired. From what I can see if I make one navigation the HistoryChanged event fires twice. At the time of the first history change event the Source url has not changed. The second time it fires the Source url has changed. Is this event connected with the Window Popstate event or as I suspect is this event being raised from an internal Webview2 property with a property changing and property changed event? Minimal Feature Suggestion |
I've added this as a scenario on our backlog. Can you clarify this statement for me?
What do you mean by "actual history of the user"? How does that differ from what the javascript API provides? |
You referred to the HTML5 History API above. My understanding is that this is only good to us in Javascript to go back / forward (couple of other methods) and to get the length of the collection. There is no way to actually view the session history items with this API. |
Oh gotcha, yes. But by using the javascript API you avoid calling |
To help anyone work on a SessionHistory / TravelLog here are a few things from my experience. In my subclass of WebView2 I have 3 properties
In my opinion you need to listen to NavigationStarting, NavigationCompleted, HistoryChanged and DocumentTitleChanged.
I have left part of my code which is still being tested to understand more fully what happens. The basic idea is that I add the NavigationID to the NavigationTracker.
Most important is to remove the NavigationID. Then if we have a History Change then we get some details and add to our travel log or SessionHistory.
HistoryChanged event seems to fire twice so I ignore a change if it was the previous URL. I learnt this from the C++ MS example.
I use the DocumentTitleChanged event because HistoryChanges take place without a navigation. As I want to save the documenttitle in my collection then this event seems perfect for me. Maybe source change would also work. I hope this helps someone or if someone else has feedback on my experience that would be great. One other thing is I do not use the WebView2 methods to go back or forward. I only use the Javascript method Window.History.Go(int); you add an int -1 to go back 1 or use a 2 to go forward 2 items in the history.
|
Wow, that was a while ago. You have to roll this yourself which seems a little frustrating as the browser holds the history. In the most simplest form you need to hold a list of urls that have been navigated, then each time a new url is added to that list you need to first check if the url already exists in the list. I did this before the new “Kind” was added, but (with the greatest respect) it is as good as tits on a bull. This kind is at https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2navigationkind?view=webview2-dotnet-1.0.1938.49 “A navigation back or forward to a different entry in the session navigation history, like via CoreWebView2.Back(), location.back(), the end user pressing Alt+Left or other UX, or other mechanisms to navigate back or forward in the current session navigation history.” So the navigation kind tells you if CoreWebView2.Back(), location.back() or the javascript version are used. YOU control those as a developer. You as a developer control how the user navigates (keyboard short cuts maybe harder but possible) the only thing you cannot control is what links the user clicks on in the webpage. Thus this new feature is of marginal help. Will explain below where it helps. What would actually help you and I would be knowing the “session navigation history” which as can now be seen from their text above they know otherwise they would not be able to send the kind. Any way back to your question. So you have a list of the urls that are navigated. Each time there is a new navigation you add to the list, but you check if the url already exists. If you are intending to show the user a list of possible back and forward urls as I have shown then you have to not only keep your list of urls but you need to order them and keep a record of your position in that list. For example
So your list has 5 urls. You hold your current position as 5 as that was the last navigation complete url.
So your list does not change but you keep a record that your position is at 3.
If you do my No.4 example this way then your back and forward method attached to Webview2 still work and are in sync with your list. |
The above method of manually tracking the browsing history is tedious and cannot be persisted to new process instances. So yes, open this new scenario if you haven't already. We need a way to persist and restore the travellog (for reference, we did this with ITravelLogStg and ITravelLogEntry in Internet Explorer). I would prefer it be done independently of the WebView2 user data folder so that we can map it to our specific UI views. Ideally each entry would have title, URI, and icon URI. Any form data is a bonus. I don't know if this is related but browsers now allow you to middle-click the forward/back buttons to do a combination of new window, navigation, and history propagation. I think GoBack() and GoForward() should have two additional parameters: number of positions to navigate from the current position (like the Javascript API), and whether or not to open as a new window. |
As a workaround you can use |
Having a host-application navigation stack is not a viable work around for the lack of access to WebView2's navigation stack. If we maintain our own navigation stack, but WebView2 crashes, and then reloads, WebView2 will lose it's internal navigation stack. That means that the following events (that are outside the control of the host-application) will not succeed:
To fix these use-cases, we need to be able to get & set the navigation stack (or have WebView2 automatically restore it's stack after a crash). |
Related to #4713 It's not just travel logs, you also want to be able to restore the position on each page part of your history. |
Even in Edge page scroll offset is not persisted only the travel logs. |
@MarkIngramUK - Agree that it's less than ideal, but this should be tractable with a few changes (making some assumptions). Definitely expand on the issue if I'm missing something major here, that might help with prioritization:
I'm sure there's implementation nuance that I'm overlooking around handling "your own" navigations in #5, but (naively obvious) doesn't seem prohibitively expensive?
|
There is other state connected to travel log that is less obvious, like page position, form fields, etc. What we really need is something more like an abstract BLOB that we can import and export which contains all of these things. It isn't unreasonable to use a web browser control for actual web browsing; right now the only use case seems to be for creating HTML dialog boxes. |
@aluhrs13 , thanks for the response. It’s possible we could invest in that work, and end up with a half-decent work around, but it’s a heck of a lot more effort than our macOS Browser that’s based on WebKit. Apple provides APIs for this scenario: WKWebView.interactionState
|
@MarkIngramUK - Yeah, I'm familiar with the similar WKWebView and Android WebView APIs. I don't have a ton of first-hand experience though, and Apple's docs are kinda lacking and I'm not finding much with quick search - Do you know what state is actually restored with Transparently - An implementation existing for other WebViews doesn't greatly affect our prioritization here. Given the main use-case requires:
The combination is niche enough that for the apps that need it, the work-around isn't problematic enough for us to make this a higher priority. |
I don't know anything about WebKit or how it handles its state yet the earlier description reads pretty close to what I proposed. I'm trying to lay out the right thing to do for any embedded browser from a bird's eye POV. What if the WV2 doesn't crash but the application simply has to be resumed after updates or end-of-work-day? Not to mention how much this happens during development or diagnosis. Would you say that's niche? What if a host window is displaying 5 or 6 WV2's for real time display ex. financial or logistics or security? This is actually closer to what I'm using WV2 for, watching video feeds from multiple different sources. In that case you either have to maintain all your state in the URL or hope the cookies can figure out which view is which. If we don't control the source website in each WV2 then things get that much harder. |
I am interested in creating the list of possible page titles/urls to navigate backwards and forwards. See the pic of the list from Edge. I am writing a WPF app.
Looking at the current API there is not much there. Am I right? There is a HistoryChanged event but only indicates the history has changed, but does not give you the actual history. I see a history count but not much else.
AB#32488716
The text was updated successfully, but these errors were encountered: