Skip to content
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

Start clean instance of webview #531

Closed
bvukotic opened this issue May 26, 2020 · 13 comments
Closed

Start clean instance of webview #531

bvukotic opened this issue May 26, 2020 · 13 comments
Labels

Comments

@bvukotic
Copy link

Specification

  • pywebview version: 3.2
  • platform / version: Any

Description

It would be useful to start "clean" instance of pywebview (like incognito mode). Currently, cookies are remembered somewhere in the web engine, so each time I start webview session is not clean, some logins are remembered, etc...
It would be nice to have something like

webview.start(clean=True)

JavaFX WebView starts clean every time (by default)

@r0x0r
Copy link
Owner

r0x0r commented May 26, 2020

Good suggestion. Clean state could be set default.

@bvukotic
Copy link
Author

Can I do this in my python code?
I am on windows 10, I guess I (we) use mshtml, can I call the code mentioned here
https://stackoverflow.com/questions/13793261/disable-cookie-read-write-in-webbrowser-c-sharp-application

but form python?

@r0x0r
Copy link
Owner

r0x0r commented Jun 18, 2020 via email

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@analogaio
Copy link

This would be a great addition to webview

@r0x0r
Copy link
Owner

r0x0r commented Nov 20, 2020

@Ksengine if you want to help, this issue would be a great choice.

@Ksengine
Copy link
Contributor

I can do qt and gtk.

@obfusk
Copy link
Contributor

obfusk commented Jan 18, 2021

In my experience, the gtk webview doesn't store cookies [edit: by default].
I opened #687 to not save cookies in qt webengine.

@Ksengine
Copy link
Contributor

Example: how can you store and apply cookie jar to gtk webkit

from gi.repository import Gtk, WebKit, Soup
cookiejar = Soup.CookieJarText.new("<Your cookie path>", False)
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
session = WebKit.get_default_session()
session.add_feature(cookiejar)

@r0x0r
Copy link
Owner

r0x0r commented Mar 8, 2021

@obfusk 's PR merged into the incognito-mode branch.

@Ksengine
Copy link
Contributor

Ksengine commented Apr 1, 2021

https://github.com/cztomczak/cefpython/blob/v66.0/examples/snippets/network_cookies.py

"""
Implement RequestHandler.CanGetCookies and CanSetCookie
to block or allow cookies over network requests.
"""

from cefpython3 import cefpython as cef


def main():
    cef.Initialize()
    browser = cef.CreateBrowserSync(
        url="http://www.html-kit.com/tools/cookietester/",
        window_title="Network cookies")
    browser.SetClientHandler(RequestHandler())
    cef.MessageLoop()
    del browser
    cef.Shutdown()


class RequestHandler(object):
    def __init__(self):
        self.getcount = 0
        self.setcount = 0

    def CanGetCookies(self, frame, request, **_):
        # There are multiple iframes on that website, let's log
        # cookies only for the main frame.
        if frame.IsMain():
            self.getcount += 1
            print("-- CanGetCookies #"+str(self.getcount))
            print("url="+request.GetUrl()[0:80])
            print("")
        # Return True to allow reading cookies or False to block
        return True

    def CanSetCookie(self, frame, request, cookie, **_):
        # There are multiple iframes on that website, let's log
        # cookies only for the main frame.
        if frame.IsMain():
            self.setcount += 1
            print("-- CanSetCookie @"+str(self.setcount))
            print("url="+request.GetUrl()[0:80])
            print("Name="+cookie.GetName())
            print("Value="+cookie.GetValue())
            print("")
        # Return True to allow setting cookie or False to block
        return True


if __name__ == '__main__':
    main()

@r0x0r
Copy link
Owner

r0x0r commented Mar 11, 2022

I have finally implemented this. EdgeChromium, CEF, GTK, QT and Cocoa are supported. PR here #869

Incognito is now private_mode and is enabled by default. Implementation for each platform is rather hairy, but private mode works more reliably than non-private mode. Notable open issues are

  1. Built-in HTTP server starts on a random port by default. In order to support persistent local storage, a fixed port must be used. This PR introduces a default http server port 42001, which is used when private_mode=False and no custom http port is supplied
  2. Cocoa's nonPersistentMode does not work as expected, namely it preserves cookies over sessions. I have spent a fair time investigating this and found out that cookies come from NSHTTPCookieStorage in persistent mode. Why is it like this is beyond me. As a workaround all the storage is manually deleted in private_mode before launching webview. A drawback of this solution is that it deletes data for non-private sessions as well.

r0x0r added a commit that referenced this issue Mar 19, 2022
#531 Incognito mode and cookie support
@r0x0r r0x0r unpinned this issue Apr 6, 2022
@r0x0r r0x0r closed this as completed Jan 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants