web-view icon indicating copy to clipboard operation
web-view copied to clipboard

Persistent cookies between sessions

Open MGlolenstine opened this issue 4 years ago • 12 comments

How would one keep cookies between sessions?

EDIT: I can save them using the localStorage, but they won't get loaded at the next webpage load.

MGlolenstine avatar Aug 09 '20 10:08 MGlolenstine

I am unsure as to what you mean by "won't get loaded at the next webpage load". Aren't files stored to localstorage consistent between sessions?

Blakeinstein avatar Aug 13 '20 06:08 Blakeinstein

I am unsure as to what you mean by "won't get loaded at the next webpage load". Aren't files stored to localstorage consistent between sessions?

Correct... localStorage does persist, but I need to load cookies for the first website request and I can't access localStorage or cookies without JS, which is a problem, as I have to set cookies before first request, otherwise I just get blocked by "unauthorised".

MGlolenstine avatar Aug 13 '20 06:08 MGlolenstine

You could build the webview first then use eval to execute the necessary javascript and then run the webview..

let mut webview = builder().......
				...build().unwrap();

webview.eval(r#"
	your js code here
"#);

webview.run().unwrap(); ?

Blakeinstein avatar Aug 13 '20 06:08 Blakeinstein

You could build the webview first then use exec to execute the necessary javascript and then run the webview..

let mut webview = builder().......
				...build().unwrap();

webview.exec(r#"
	your js code here
"#);

webview.run().unwrap(); ?

I had no idea I was able to run JS without having to load a website first 😮. I'll try this out and will report back!

MGlolenstine avatar Aug 13 '20 06:08 MGlolenstine

webview doesn't include exec function. The code is located after the .build().unwrap().

error[E0599]: no method named `exec` found for struct `web_view::WebView<'_, bool>` in the current scope
  --> src/main.rs:49:13
   |
49 |     webview.exec(r#"
   |             ^^^^ method not found in `web_view::WebView<'_, bool>`

.eval() on the other hand works!

MGlolenstine avatar Aug 13 '20 06:08 MGlolenstine

Ah shoot my apologies, I got confused between exec and eval for a second since I was using qt recently... Anyways, Glad it works.

Blakeinstein avatar Aug 13 '20 07:08 Blakeinstein

If you have no other issues, close the Issue btw!

Blakeinstein avatar Aug 13 '20 07:08 Blakeinstein

If you have no other issues, close the Issue btw!

I haven't solved the cookies yet, I just managed to get the JS working before the WebView loads ;) I will close it once I have it working! :+1: I'm currently at work, so I'll try more stuff once I get home.

MGlolenstine avatar Aug 13 '20 07:08 MGlolenstine

Has anyone come across a solution for this? I just did a quick test and localStorage doesn't seem to persist between runs.

It would be awesome if we could pass a path to the builder where the cookies can be found/stored. I realise that could be almost impossible if not implemented in the C library though.

khinch avatar Apr 15 '21 07:04 khinch

This is quite a must-have feature and I will explain why.

With no persistent cookies support (or at least to have the ability to load cookies from file) you are pretty much doomed from all application behind login or from applications where you need some kind of personalization.

im-n1 avatar Nov 05 '21 12:11 im-n1

Please give tauri a shot https://github.com/tauri-apps/tauri

Blakeinstein avatar Nov 05 '21 13:11 Blakeinstein

Tauri is a whole different level and tool. Also using another tool doesn't resolve this issue.

im-n1 avatar Nov 05 '21 16:11 im-n1