Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable_scripts non-global option #268

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

### Added

- Add `:disable_scripts` non-global option. (@osbre)

## [1.12.0] - 2024-07-12

### Added
Expand Down
11 changes: 11 additions & 0 deletions lib/chromic_pdf/pdf/protocols/navigate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ defmodule ChromicPDF.Navigate do
import ChromicPDF.ProtocolMacros

steps do
if_option :disable_scripts do
call(
:disable_scripts,
"Emulation.setScriptExecutionDisabled",
[{"value", :disable_scripts}],
%{}
)

await_response(:scripts_disabled, [])
end

if_option :set_cookie do
call(:set_cookie, "Network.setCookie", &Map.fetch!(&1, :set_cookie), %{})
await_response(:cookie_set, [])
Expand Down
5 changes: 4 additions & 1 deletion lib/chromic_pdf/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ defmodule ChromicPDF.Supervisor do
required(:attribute) => binary()
}}

@type disable_scripts_option :: {:disable_scripts, boolean()}

@type navigate_option ::
{:set_cookie, map()}
| evaluate_option()
| wait_for_option()
| disable_scripts_option()

@type pdf_option ::
{:print_to_pdf, map()}
Expand Down Expand Up @@ -209,14 +212,14 @@ defmodule ChromicPDF.Supervisor do
@type global_option ::
{:name, atom()}
| {:offline, boolean()}
| {:disable_scripts, boolean()}
| {:unhandled_runtime_exceptions, :ignore | :log | :raise}
| {:console_api_calls, :ignore | :log | :raise}
| {:max_session_uses, non_neg_integer()}
| {:session_pool, [session_pool_option()]}
| {:ignore_certificate_errors, boolean()}
| {:ghostscript_pool, [ghostscript_pool_option()]}
| {:on_demand, boolean()}
| disable_scripts_option()
| local_chrome_option()
| inet_chrome_option()

Expand Down
11 changes: 10 additions & 1 deletion test/integration/pdf_generation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -226,9 +226,18 @@ defmodule ChromicPDF.PDFGenerationTest do
end
)
end

@tag :pdftotext
test "scripts are not evaluated" do
params = [disable_scripts: true]

print_to_pdf({:html, test_dynamic_html()}, params, fn text ->
refute String.contains?(text, "Dynamic content from Javascript")
end)
end
end

describe "with disable_scripts: true" do
describe "with disable_scripts: true in supervisor" do
setup do
start_supervised!({ChromicPDF, disable_scripts: true})
:ok
Expand Down