Initial commit
This commit is contained in:
68
selenium_cookie_extractor_json.py
Normal file
68
selenium_cookie_extractor_json.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
import time
|
||||
import json
|
||||
from selenium import webdriver
|
||||
from selenium.webdriver.chrome.service import Service
|
||||
|
||||
# Configure Chrome options to run in incognito mode.
|
||||
options = webdriver.ChromeOptions()
|
||||
options.add_argument("--incognito")
|
||||
# Uncomment the following option if you want to auto-open developer tools for each tab.
|
||||
# options.add_argument("--auto-open-devtools-for-tabs")
|
||||
|
||||
# Initialize the Chrome driver.
|
||||
# If ChromeDriver is not in your PATH, specify its location: Service('/path/to/chromedriver')
|
||||
service = Service() # Assumes chromedriver is in your PATH
|
||||
|
||||
driver = webdriver.Chrome(service=service, options=options)
|
||||
|
||||
print("Opening https://chat.google.com ...")
|
||||
driver.get("https://chat.google.com")
|
||||
|
||||
print("\nA new incognito window has been opened.")
|
||||
print("Please log in normally. To inspect cookies manually:")
|
||||
print(" 1. Press F12 to open developer tools.")
|
||||
print(" 2. Navigate to the Application (Chrome) or Storage (Firefox) tab.")
|
||||
print(" 3. Expand Cookies and select https://chat.google.com.")
|
||||
print(" 4. Verify that the COMPASS, SSID, SID, OSID, and HSID cookies are present.")
|
||||
print("\nIMPORTANT: Once you are logged in and have confirmed the cookies (or just logged in),")
|
||||
print("press Enter here. (Remember: to keep the validity of these cookies, close the browser soon!)")
|
||||
input("Press Enter to extract cookies...")
|
||||
|
||||
# Get all cookies
|
||||
all_cookies = driver.get_cookies()
|
||||
|
||||
# Define the cookie names we want (case-insensitive)
|
||||
target_names = {"compass", "ssid", "sid", "osid", "hsid"}
|
||||
extracted = {}
|
||||
|
||||
# For COMPASS cookie, if multiple are present, prefer the one with path == "/"
|
||||
compass_cookie = None
|
||||
|
||||
for cookie in all_cookies:
|
||||
name_lower = cookie["name"].lower()
|
||||
if name_lower in target_names:
|
||||
# Process COMPASS cookie separately to pick the one with path '/'
|
||||
if name_lower == "compass":
|
||||
if cookie.get("path", "") == "/":
|
||||
compass_cookie = cookie # select this cookie
|
||||
elif not compass_cookie:
|
||||
compass_cookie = cookie # take the first if none with '/' are found yet
|
||||
else:
|
||||
# For others, simply store the value
|
||||
extracted[name_lower] = cookie["value"]
|
||||
|
||||
# If we found a COMPASS cookie, add it to the extracted dict.
|
||||
if compass_cookie:
|
||||
extracted["compass"] = compass_cookie["value"]
|
||||
|
||||
# Form JSON object of the extracted cookies.
|
||||
json_data = json.dumps(extracted, indent=2)
|
||||
print("\nExtracted Cookie JSON:")
|
||||
print(json_data)
|
||||
|
||||
print("\nPlease close the browser window soon to avoid invalidating the cookies (Google uses refresh tokens).")
|
||||
time.sleep(5) # wait a few seconds to allow the user to read the output
|
||||
|
||||
# Close the browser window.
|
||||
driver.quit()
|
||||
Reference in New Issue
Block a user