Ubuntu Chrome: how to read cookie with python script

I am creating a small application that consists of two parts: one of them is displayed inside the Chrome browser, and the other is a local application programmed in Python.

In Chrome, the user has <select> to select his / her preferred language. This information is stored in Chrome in a cookie.

I would like to know if it is possible to get this language setting (for example, reading a cookie), so when I launch a local application, it will be displayed in the same language that the user already selected with Chrome.

I cannot change the locale of the system (although this probably makes sense). This should remain in English, but if the user selects Chinese as their preferred language via Chrome and then launches a local application, I would like this application to start in Chinese.

I looked at the command line switches for Chrome, but I did not see anything too useful. At best, the parameter is --enable-file-cookies , and then try to open and analyze the cookie ... anyway, but all the information I could find is pretty vague.

Thank you in advance!

Update A further search ( 1 , 2 ) seems to indicate that Chrome stores cookies using SQL lite. I am studying this. Maybe there is hope with this ...

+7
source share
1 answer

Yes, as I mentioned in the comments on my question, sqlite3 sounded promising ... That day when I learn to read, I will conquer the world!

Anyway, just in case, useful for someone else:

 #!/usr/bin/env python import os import sqlite3 import pwd _cookieName = "preferredLanguage" def getPreferredLanguageFromCookieDB(): retval="en-US" cookieDBFilename = os.path.join(pwd.getpwuid(1000).pw_dir, ".config/google-chrome/Default/Cookies") if os.path.isfile(cookieDBFilename): connection = sqlite3.connect(cookieDBFilename) querier = connection.cursor() numCookiesMatching = int(querier.execute('SELECT COUNT(*) FROM cookies WHERE (host_key="127.0.0.1" or host_key="localhost") and name="%s"' % (_cookieName)).fetchone()[0]) if numCookiesMatching == 1: retval = querier.execute('SELECT value FROM cookies WHERE (`cookies`.`host_key`="127.0.0.1" or `cookies`.`host_key`="localhost") and `cookies`.`name` = "%s"' % (_cookieName)).fetchone()[0] elif numCookiesMatching == 0: print("::getPreferredLanguageFromCookieDB > No cookie for '%s' found. Assuming wizard hasn't run yet, which is weird, but not critical" % (_cookieName)) retval="en-US" else: raise KeyError("Found %s cookies matching %s in file %s. This shouldn't have happened" % (numCookiesMatching, _cookieName, cookieDBFilename)) retval=None else: print("::getPreferredLanguageFromCookieDB > Cookie 'db' (actually, file) %s doesn't exist" % (cookieDBFilename)) retval="en-US" return retval if __name__ == "__main__": print "Prefered language: %s" % getPreferredLanguageFromCookieDB() 

This little snippet connects to the Cookies database (actually it's just a file, but anyway ...) and read the cookie value " preferredLanguage " issued either by localhost or 127.0.0.1 . It will crash if there is more than one " preferredLanguage " cookie issued by the local host.

+8
source