Skip to content
Open
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
9 changes: 5 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
webview_ruby (0.1.0)
webview_ruby (0.1.1)
ffi
ffi-compiler
rake
Expand All @@ -10,9 +10,9 @@ GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
ffi (1.15.5)
ffi-compiler (1.0.1)
ffi (>= 1.0.0)
ffi (1.17.0-x86_64-linux-gnu)
ffi-compiler (1.3.2)
ffi (>= 1.15.5)
rake
minitest (5.15.0)
parallel (1.21.0)
Expand All @@ -39,6 +39,7 @@ GEM
PLATFORMS
arm64-darwin-20
arm64-darwin-21
x86_64-linux

DEPENDENCIES
minitest (~> 5.0)
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ You can bind a ruby function to a JavaScript one before the webview is in runnin
```ruby
webview.bind("exampleFunc") do
print("got called by js")
"returned string" #do not use "return" as keyword, because that exits the current thread
end
```

Expand All @@ -71,6 +72,14 @@ end

You can only use positional arguments, keyword ones wouldn't work.

To get the return value in javascript, do the following:

```javascript
exampleFunc('a','b').then(result => {
alert(result);
})
```

### Run JS code from ruby

You can invoke JS code to be run asynchrounously (the result of the execution won't be returned to you) by running
Expand All @@ -95,6 +104,11 @@ webview.init("console.log('running at initialisation')")

It is guaranteed that code is executed before window.onload.

## Caveats
* you can only open new windows (next to the main window) via a new ruby process
* the new created window is not stacked to the other in the task bar
* popups don't work

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Maaarcocr/webview_ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/webview_ruby/blob/master/CODE_OF_CONDUCT.md).
Expand Down
4 changes: 2 additions & 2 deletions ext/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ FFI::Compiler::CompileTask.new('webview-ext') do |c|
c.cxxflags << "-std=c++11"
c.ldflags << "-framework WebKit"
elsif RbConfig::CONFIG['host_os'] =~ /linux/
c.cxxflags << `pkg-config --cflags gtk+-3.0 webkit2gtk-4.0`.strip
c.ldflags << `pkg-config --libs gtk+-3.0 webkit2gtk-4.0`.strip
c.cxxflags << `pkg-config --cflags gtk+-3.0 webkit2gtk-4.1`.strip
c.ldflags << `pkg-config --libs gtk+-3.0 webkit2gtk-4.1`.strip
else
puts "Unsupported operating system prolly windows, check rake file in ext directory in source code and add correct flags from here https://github.com/webview/webview#webview-for-cc-developers "
end
Expand Down
6 changes: 4 additions & 2 deletions lib/webview_ruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module WebviewRuby
attach_function :webview_navigate, [:pointer, :string], :void
attach_function :webview_destroy, [:pointer], :void
attach_function :webview_bind, [:pointer, :string, :pointer, :pointer], :void
attach_function :webview_return, [:pointer, :string, :int, :string], :void
attach_function :webview_eval, [:pointer, :string], :void
attach_function :webview_init, [:pointer, :string], :void

Expand Down Expand Up @@ -61,9 +62,10 @@ def bind(name, func=nil, &block)
if func
func(*params)
else
block.call(*params)
result = block.call(*params)
WebviewRuby.webview_return(@window, seq, 0, result.to_json)
end
rescue StandardError => e
rescue Exception => e
print("Error occured: #{e.full_message}. \n\n Going to terminate\n")
terminate
end
Expand Down