Class: Puppeteer::Launcher::Chrome::DefaultArgs

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/puppeteer/launcher/chrome.rb

Instance Method Summary collapse

Constructor Details

#initialize(chrome_arg_options) ⇒ DefaultArgs

Returns a new instance of DefaultArgs.

Parameters:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/puppeteer/launcher/chrome.rb', line 140

def initialize(chrome_arg_options)
  # See https://github.com/GoogleChrome/chrome-launcher/blob/main/docs/chrome-flags-for-tools.md
  user_disabled_features = Chrome.get_features('--disable-features', chrome_arg_options.args)
  if user_disabled_features.any?
    Chrome.remove_matching_flags(chrome_arg_options.args, '--disable-features')
  end

  user_enabled_features = Chrome.get_features('--enable-features', chrome_arg_options.args)
  if user_enabled_features.any?
    Chrome.remove_matching_flags(chrome_arg_options.args, '--enable-features')
  end

  enabled_features = [
    'NetworkServiceInProcess2',
    *user_enabled_features,
  ].reject(&:empty?)

  disabled_features = [
    'Translate',
    'BackForwardCache',
    'AcceptCHFrame',
    'MediaRouter',
    'OptimizationHints',
    'IPH_ReadingModePageActionLabel',
    'ReadAnythingOmniboxChip',
    'WebUIReloadButton',
    *user_disabled_features,
  ].reject(&:empty?).reject { |feature| enabled_features.include?(feature) }

  chrome_arguments = [
    '--allow-pre-commit-input',
    '--disable-background-networking',
    '--disable-background-timer-throttling',
    '--disable-backgrounding-occluded-windows',
    '--disable-breakpad',
    '--disable-client-side-phishing-detection',
    '--disable-component-extensions-with-background-pages',
    '--disable-component-update',
    '--disable-default-apps',
    '--disable-dev-shm-usage',
    "--disable-features=#{disabled_features.join(',')}",
    '--disable-hang-monitor',
    '--disable-ipc-flooding-protection',
    '--disable-popup-blocking',
    '--disable-prompt-on-repost',
    '--disable-renderer-backgrounding',
    '--disable-sync',
    '--enable-automation',
    # TODO(sadym): remove '--enable-blink-features=IdleDetection' once
    # IdleDetection is turned on by default.
    '--enable-blink-features=IdleDetection',
    "--enable-features=#{enabled_features.join(',')}",
    '--export-tagged-pdf',
    '--force-color-profile=srgb',
    '--metrics-recording-only',
    '--no-first-run',
    '--password-store=basic',
    '--use-mock-keychain',
  ]

  if chrome_arg_options.user_data_dir
    chrome_arguments << "--user-data-dir=#{chrome_arg_options.user_data_dir}"
  end

  if chrome_arg_options.devtools?
    chrome_arguments << '--auto-open-devtools-for-tabs'
  end

  if chrome_arg_options.headless?
    chrome_arguments.concat([
      '--headless',
      '--hide-scrollbars',
      '--mute-audio',
    ])
  end

  # helper for Docker
  # https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
  if %w[1 true].include?(ENV['PUPPETEER_RUBY_NO_SANDBOX'])
    ['--no-sandbox', '--disable-setuid-sandbox'].each do |arg|
      unless chrome_arguments.include?(arg)
        chrome_arguments << arg
      end
    end
  end

  if chrome_arg_options.enable_extensions
    if chrome_arg_options.enable_extensions.is_a?(Array) && !chrome_arg_options.enable_extensions.empty?
      extension_paths = chrome_arg_options.enable_extensions.map do |path|
        File.expand_path(path)
      end
      joined_paths = extension_paths.join(',')
      chrome_arguments << "--disable-extensions-except=#{joined_paths}"
      chrome_arguments << "--load-extension=#{joined_paths}"
    end
  else
    chrome_arguments << '--disable-extensions'
  end

  if chrome_arg_options.args.all? { |arg| arg.start_with?('-') }
    chrome_arguments << 'about:blank'
  end

  chrome_arguments.concat(chrome_arg_options.args)

  @chrome_arguments = chrome_arguments
end

Instance Method Details

#each(&block) ⇒ Object



248
249
250
251
252
# File 'lib/puppeteer/launcher/chrome.rb', line 248

def each(&block)
  @chrome_arguments.each do |opt|
    block.call(opt)
  end
end