Skip to content

Commit cca9a0d

Browse files
authored
feat: Incognito startup window (#532)
1 parent b727708 commit cca9a0d

File tree

7 files changed

+18
-4
lines changed

7 files changed

+18
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `Ferrum::Frame#frame_element` returns the element in which the window is embedded [#524]
1414
- `Ferrum::Page#start_screencast` starts sending frames to record screencast [#494]
1515
- `Ferrum::Page#stop_screencast` stops sending frames [#494]
16+
- `Ferrum::Browser#new(incognito: false)` wether to create an incognito profile for the browser startup window, `true` by default.
1617

1718
### Changed
1819

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,8 @@ Ferrum::Browser.new(options)
151151
```
152152

153153
* options `Hash`
154-
* `:headless` (String | Boolean) - Set browser as headless or not, `true` by default.
154+
* `:headless` (Boolean) - Set browser as headless or not, `true` by default.
155+
* `:incognito` (Boolean) - Create an incognito profile for the browser startup window, `true` by default.
155156
* `:xvfb` (Boolean) - Run browser in a virtual framebuffer, `false` by default.
156157
* `:flatten` (Boolean) - Use one websocket connection to the browser and all the pages in flatten mode.
157158
* `:window_size` (Array) - The dimensions of the browser window in which to

lib/ferrum/browser.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ class Browser
4545
# @option options [Boolean] :headless (true)
4646
# Set browser as headless or not.
4747
#
48+
# @option options [Boolean] :incognito (true)
49+
# Create an incognito profile for the browser startup window.
50+
#
4851
# @option options [Boolean] :xvfb (false)
4952
# Run browser in a virtual framebuffer.
5053
#

lib/ferrum/browser/options.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Options
1414
attr_reader :window_size, :logger, :ws_max_receive_size,
1515
:js_errors, :base_url, :slowmo, :pending_connection_errors,
1616
:url, :ws_url, :env, :process_timeout, :browser_name, :browser_path,
17-
:save_path, :proxy, :port, :host, :headless, :browser_options,
17+
:save_path, :proxy, :port, :host, :headless, :incognito, :browser_options,
1818
:ignore_default_browser_options, :xvfb, :flatten
1919
attr_accessor :timeout, :default_user_agent
2020

@@ -27,6 +27,7 @@ def initialize(options = nil)
2727
@window_size = @options.fetch(:window_size, WINDOW_SIZE)
2828
@js_errors = @options.fetch(:js_errors, false)
2929
@headless = @options.fetch(:headless, true)
30+
@incognito = @options.fetch(:incognito, true)
3031
@flatten = @options.fetch(:flatten, true)
3132
@pending_connection_errors = @options.fetch(:pending_connection_errors, true)
3233
@process_timeout = @options.fetch(:process_timeout, PROCESS_TIMEOUT)

lib/ferrum/browser/options/chrome.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ def merge_required(flags, options, user_data_dir)
7878
def merge_default(flags, options)
7979
defaults = except("headless", "disable-gpu") if options.headless == false
8080
defaults ||= DEFAULT_OPTIONS
81+
defaults.delete("no-startup-window") if options.incognito == false
8182
# On Windows, the --disable-gpu flag is a temporary workaround for a few bugs.
8283
# See https://bugs.chromium.org/p/chromium/issues/detail?id=737678 for more information.
8384
defaults = defaults.merge("disable-gpu" => nil) if Utils::Platform.windows?

lib/ferrum/context.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def add_target(params:, session_id: nil)
6161
new_target = Target.new(@client, session_id, params)
6262
# `put_if_absent` returns nil if added a new value or existing if there was one already
6363
target = @targets.put_if_absent(new_target.id, new_target) || new_target
64+
@default_target ||= target
6465

6566
new_pending = Concurrent::IVar.new
6667
pending = @pendings.put_if_absent(target.id, new_pending) || new_pending

lib/ferrum/contexts.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,19 @@ def size
6969

7070
private
7171

72-
# rubocop:disable Metrics/PerceivedComplexity
72+
# rubocop:disable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
7373
def subscribe
7474
@client.on("Target.attachedToTarget") do |params|
7575
info, session_id = params.values_at("targetInfo", "sessionId")
7676
next unless ALLOWED_TARGET_TYPES.include?(info["type"])
7777

7878
context_id = info["browserContextId"]
79+
unless @contexts[context_id]
80+
context = Context.new(@client, self, context_id)
81+
@contexts[context_id] = context
82+
@default_context ||= context
83+
end
84+
7985
@contexts[context_id]&.add_target(session_id: session_id, params: info)
8086
if params["waitingForDebugger"]
8187
@client.session(session_id).command("Runtime.runIfWaitingForDebugger", async: true)
@@ -114,7 +120,7 @@ def subscribe
114120
context&.delete_target(params["targetId"])
115121
end
116122
end
117-
# rubocop:enable Metrics/PerceivedComplexity
123+
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
118124

119125
def discover
120126
@client.command("Target.setDiscoverTargets", discover: true)

0 commit comments

Comments
 (0)