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 all 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
2 changes: 1 addition & 1 deletion lib/chromic_pdf.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule ChromicPDF do
end

Note that this doesn't prevent other features like the `evaluate` option from working, it
solely applies to scripts being supplied by the rendered page itself.
solely applies to scripts being supplied by the rendered page itself. Also, you can override this global option by passing the `disable_scripts` option to `print_to_pdf/2` or `print_to_pdfa/2` directly.

### Running in offline mode

Expand Down
2 changes: 1 addition & 1 deletion lib/chromic_pdf/pdf/connection/inet.ex
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ if Code.ensure_loaded?(WebSockex) do
:inets.start()

url = String.to_charlist("http://#{host}:#{port}/json/version")
headers = [{'accept', 'application/json'}]
headers = [{~c"accept", ~c"application/json"}]
http_request_opts = [ssl: [verify: :verify_none]]

case :httpc.request(:get, {url, headers}, http_request_opts, []) do
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
22 changes: 21 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 Expand Up @@ -258,6 +267,17 @@ defmodule ChromicPDF.PDFGenerationTest do
assert String.contains?(text, "hello from script")
end)
end

@tag :pdftotext
test "scripts are evaluated when local option is set" do
params = [
disable_scripts: false
]

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

describe "offline mode" do
Expand Down
4 changes: 3 additions & 1 deletion test/integration/support/get_targets.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ defmodule ChromicPDF.GetTargets do
def run do
{:ok, target_infos} =
ChromicPDF.Supervisor.with_services(ChromicPDF, fn services ->
ChromicPDF.Browser.run_protocol(services.browser, __MODULE__, skip_session_use_count: true)
ChromicPDF.Browser.run_protocol(services.browser, __MODULE__,
skip_session_use_count: true
)
end)

for %{"targetId" => target_id, "url" => url} <- target_infos,
Expand Down