Current section

Files

Jump to
chromic_pdf lib chromic_pdf pdf protocols spawn_session.ex
Raw

lib/chromic_pdf/pdf/protocols/spawn_session.ex

# SPDX-License-Identifier: Apache-2.0
defmodule ChromicPDF.SpawnSession do
@moduledoc false
import ChromicPDF.ProtocolMacros
@version Mix.Project.config()[:version]
steps do
call(:create_browser_context, "Target.createBrowserContext", [], %{"disposeOnDetach" => true})
await_response(:browser_context_created, ["browserContextId"])
call(:create_target, "Target.createTarget", ["browserContextId"], %{"url" => "about:blank"})
await_response(:target_created, ["targetId"])
call(:attach, "Target.attachToTarget", ["targetId"], %{"flatten" => true})
await_notification(
:attached,
"Target.attachedToTarget",
[{["targetInfo", "targetId"], "targetId"}],
["sessionId"]
)
call(:set_user_agent, "Emulation.setUserAgentOverride", [], %{
"userAgent" => "ChromicPDF #{@version}"
})
if_option {:offline, true} do
call(
:offline_mode,
"Network.emulateNetworkConditions",
[],
%{
"offline" => true,
"latency" => 0,
"downloadThroughput" => 0,
"uploadThroughput" => 0
}
)
# Intentionally not awaiting the response to speed up session spawning.
end
# Enable Runtime (JS) events, mostly for Runtime.exceptionThrown.
# Again, no need to wait for result.
if_option {:unhandled_runtime_exceptions, [:log, :raise]} do
call(:runtime_enable, "Runtime.enable", [], %{})
end
if_option :disable_scripts do
call(
:disable_scripts,
"Emulation.setScriptExecutionDisabled",
[{"value", :disable_scripts}],
%{}
)
# Intentionally not awaiting the response to speed up session spawning.
end
if_option {:ignore_certificate_errors, true} do
call(:ignore_certificate_errors, "Security.setIgnoreCertificateErrors", [], %{
"ignore" => true
})
await_response(:certificate_errors_ignored, [])
end
call(:enable_page, "Page.enable", [], %{})
await_response(:page_enabled, [])
include_protocol(ChromicPDF.ResetTarget)
output(["targetId", "sessionId"])
end
end