Skip to content

Commit

Permalink
Allows disable javascript from HTML source in the page (#407)
Browse files Browse the repository at this point in the history
* feat: Allows disable javascript from HTML source

* fixup! feat: Allows disable javascript from HTML source
  • Loading branch information
erickgskovik authored Sep 26, 2023
1 parent 7ae00e7 commit 986e3e2
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [Unreleased](https://github.com/rubycdp/ferrum/compare/v0.14...main) ##

### Added
- `Ferrum::Page#disable_javascript` disables the JavaScript from the HTML source

### Changed

Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,19 @@ browser.evaluate("window.__injected") # => 42
```


## Emulation

#### disable_javascript

Disables Javascripts from the loaded HTML source.
You can still evaluate JavaScript with `evaluate` or `execute`.
Returns nothing.

```ruby
browser.disable_javascript
```


## Frames

#### frames : `Array[Frame] | []`
Expand Down
3 changes: 2 additions & 1 deletion lib/ferrum/browser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Browser
evaluate evaluate_on evaluate_async execute evaluate_func
add_script_tag add_style_tag bypass_csp
on position position=
playback_rate playback_rate=] => :page
playback_rate playback_rate=
disable_javascript] => :page
delegate %i[default_user_agent] => :process

attr_reader :client, :process, :contexts, :options, :window_size, :base_url
Expand Down
9 changes: 9 additions & 0 deletions lib/ferrum/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ def resize(width: nil, height: nil, fullscreen: false)
mobile: false)
end

#
# Disables JavaScript execution from the HTML source for the page.
#
# This doesn't prevent users evaluate JavaScript with Ferrum.
#
def disable_javascript
command("Emulation.setScriptExecutionDisabled", value: true)
end

#
# The current position of the browser window.
#
Expand Down
16 changes: 16 additions & 0 deletions spec/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,20 @@
expect { page.go_to("/ferrum/really_slow") }.to raise_error(Ferrum::PendingConnectionsError)
end
end

describe "#disable_javascript" do
it "disables javascripts on page" do
page.disable_javascript

expect { page.go_to("/ferrum/js_error") }.not_to raise_error
end

it "allows javascript evaluation from Ferrum" do
page.disable_javascript

page.evaluate("document.body.innerHTML = '<p>text</p>'")

expect(page.main_frame.body).to eq("<html><head></head><body><p>text</p></body></html>")
end
end
end

0 comments on commit 986e3e2

Please sign in to comment.