kaliiiiiiiiii / Selenium-Driverless

undetected Selenium without usage of chromedriver
https://kaliiiiiiiiii.github.io/Selenium-Driverless/
Other
430 stars 52 forks source link

[feature request] add netscape to json and cookie_transformer #188

Closed FreeM1ne closed 3 months ago

FreeM1ne commented 4 months ago

Sharing two functions netscape to json and cookie_transformer cookie transformer is needed if you already have cookies in json format, but not in the format you need I don't know how to create commits or pull requests

chrome_transformer = {"exp": "expires"}

async def netscape_to_json(cookie_strings): # accpet list[str]
    cookies_list = []
    for string in cookie_strings:
        string = string.strip()
        for parts in [string.split("\t"), string.split()]:
            if len(parts) == 6:
                parts.append("")

            elif len(parts) != 7:
                if len(parts) == 8:
                    print([string])
                continue

            try:
                cookie_dict = {
                    "domain": parts[0],
                    "httpOnly": parts[1] == "TRUE",
                    "path": parts[2],
                    "secure": parts[3] == "TRUE",
                    "expirationDate": int(parts[4]),
                    "name": parts[5],
                    "value": parts[6]
                }
                cookies_list.append(cookie_dict)
                break
            except Exception as e:
                ...

    return cookies_list

async def cookie_transformer(cookies, transformer_dict=chrome_transformer): # list[dict]
    chrome_cookies = []

    for cookie in cookies:
        chrome_cookie = {}

        for key in cookie:
            for transformer_key in transformer_dict:
                if transformer_key.lower() in key.lower():
                    chrome_cookie[transformer_dict[transformer_key]] = cookie[key]
                    break

            else:
                chrome_cookie[key] = cookie[key]

        chrome_cookies.append(chrome_cookie)

    return chrome_cookies
kaliiiiiiiiii commented 4 months ago

@FreeM1ne Can you provide an example for the usage for each of the functions?

FreeM1ne commented 4 months ago

Here is an example of a netscape cookie that we convert to json format And then we convert the json cookies into chrome format. you also need a transformer if the cookies are already in json format, but the keys are not suitable for chrome. This is necessary to be able to load any cookies, not only those that were created in browsers and then saved. NetScapeCookie.txt

kaliiiiiiiiii commented 4 months ago

Here is an example of a netscape cookie that we convert to json format And then we convert the json cookies into chrome format.

Oh right - soo netscape => the format chrome uses to parse cookies for Set-Cookie and Cookie headers I suppose? Didn't really realize that.

You also need a transformer if the cookies are already in json format, but the keys are not suitable for chrome. This is necessary to be able to load any cookies, not only those that were created in browsers and then saved.

Soo basically, we need a value for expires? or is the line chrome_transformer = {"exp": "expires"} just representative? If yes, how would an example look? {"expires":int} ?

At thid point (for request interception), I suppose having a json cookie to netscape cookie parser function would be nice as well.

FreeM1ne commented 4 months ago

Netscape is a cookie storage format. Since you only support the original cookie loading format, I thought I should add more formats. Netscape is a very commonly used cookie format. Netscape to json turns netscape into the familiar json.

CookieTransformer is needed as an additional function to change the json cookie format. Conditionally we have a cookie in which instead of "expires" the key is called "expirationDate", in the cookie transformer a person passes a dictionary of keys that should be changed to another key For the example I took the pair "expir" and "expires" because the function does not work by exact comparison It's very difficult to explain

FreeM1ne commented 4 months ago

"At thid point (for request interception), I suppose having a json cookie to netscape cookie parser function would be nice as well." I'm not quite sure what you mean

FreeM1ne commented 4 months ago

I get your point, yes chrome uses them in that format to send them to headers, but I didn't make those functions for queries, but to simplify importing cookies

kaliiiiiiiiii commented 3 months ago

Let's leave that parsing up to the user, as I'm still not 100% sure about how this would be used etc.

@FreeM1ne Feel free to open a PR tho