From f24136d2aff12ae3f2919e8f7cbe395ef309319a Mon Sep 17 00:00:00 2001 From: Ibrahim Awwal Date: Wed, 3 Jul 2024 18:24:08 -0700 Subject: [PATCH] Add option to allow using the browser's default context This would grant access to persistent state such as cookies that are associated with the default browser profile. --- lib/ferrum/browser.rb | 3 +++ lib/ferrum/browser/options.rb | 3 ++- lib/ferrum/contexts.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/ferrum/browser.rb b/lib/ferrum/browser.rb index 9626ad1d..ccf7b801 100644 --- a/lib/ferrum/browser.rb +++ b/lib/ferrum/browser.rb @@ -125,6 +125,9 @@ class Browser # @option options [Hash] :env # Environment variables you'd like to pass through to the process. # + # @option options [Boolean] :use_default_context + # When true, allows using the default browser context that has access to the browser's persistent state. + # def initialize(options = nil) @options = Options.new(options) @client = @process = @contexts = nil diff --git a/lib/ferrum/browser/options.rb b/lib/ferrum/browser/options.rb index 987f9442..962cbe8c 100644 --- a/lib/ferrum/browser/options.rb +++ b/lib/ferrum/browser/options.rb @@ -15,7 +15,7 @@ class Options :js_errors, :base_url, :slowmo, :pending_connection_errors, :url, :ws_url, :env, :process_timeout, :browser_name, :browser_path, :save_path, :proxy, :port, :host, :headless, :browser_options, - :ignore_default_browser_options, :xvfb, :flatten + :ignore_default_browser_options, :xvfb, :flatten, :use_default_context attr_accessor :timeout, :default_user_agent def initialize(options = nil) @@ -45,6 +45,7 @@ def initialize(options = nil) @base_url = parse_base_url(@options[:base_url]) if @options[:base_url] @url = @options[:url].to_s if @options[:url] @ws_url = @options[:ws_url].to_s if @options[:ws_url] + @use_default_context = @options.fetch(:use_default_context, false) @options = @options.merge(window_size: @window_size).freeze @browser_options = @options.fetch(:browser_options, {}).freeze diff --git a/lib/ferrum/contexts.rb b/lib/ferrum/contexts.rb index d6e28ea4..a493ac4d 100644 --- a/lib/ferrum/contexts.rb +++ b/lib/ferrum/contexts.rb @@ -11,7 +11,7 @@ class Contexts def initialize(client) @contexts = Concurrent::Map.new @client = client - @default_context = create_default_context + @default_context = create_default_context if @client.options.use_default_context subscribe auto_attach discover