Skip to content

Commit

Permalink
Add --user-data-dir to chrome flags:
Browse files Browse the repository at this point in the history
Adding a temporary directory as --user-data-dir to chrome flags will
trick chrome into thinking a new user is running chrome, and therefore
wont interfere with the currently running session.
  • Loading branch information
ayjayt committed Aug 19, 2024
1 parent e7423e4 commit 63e38c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
4 changes: 4 additions & 0 deletions devtools/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import sys
import subprocess
import signal
import tempfile


class Browser:
def __init__(self, debug=None, path=None, headless=True):
self.pipe = Pipe()
self.temp_dir = tempfile.TemporaryDirectory()

if not debug: # false o None
stderr = subprocess.DEVNULL
Expand All @@ -28,6 +30,7 @@ def __init__(self, debug=None, path=None, headless=True):

new_env = os.environ.copy()
new_env["CHROMIUM_PATH"] = path
new_env["USER_DATA_DIR"] = self.temp_dir.name
if headless:
new_env["HEADLESS"] = "--headless"

Expand Down Expand Up @@ -74,6 +77,7 @@ def close_browser(self):
self.subprocess.terminate()
self.subprocess.wait(5)
self.subprocess.kill()
self.temp_dir.cleanup()

def send_command(self, command, params=None, cb=None):
return self.protocol.send_command(self, command, params, cb)
4 changes: 4 additions & 0 deletions devtools/chrome_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,15 @@
else:
path = os.environ["CHROMIUM_PATH"]

user_data_dir = os.environ["USER_DATA_DIR"]

cli = [
path,
"--remote-debugging-pipe",
"--disable-breakpad",
"--allow-file-access-from-files",
f"--user-data-dir={user_data_dir}",
"--no-first-run"
]

if "HEADLESS" in os.environ:
Expand Down

0 comments on commit 63e38c4

Please sign in to comment.