From a0b2f9a7f167d187ac81491a37c6b6f9bfc9f401 Mon Sep 17 00:00:00 2001 From: Andreas Treubert Date: Fri, 27 Sep 2024 08:44:05 +0200 Subject: [PATCH 1/2] some improvements --- Gemfile.lock | 9 +++++---- README.md | 6 ++++++ ext/Rakefile | 4 ++-- lib/webview_ruby.rb | 6 ++++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 33e897d..1efd365 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - webview_ruby (0.1.0) + webview_ruby (0.1.1) ffi ffi-compiler rake @@ -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) @@ -39,6 +39,7 @@ GEM PLATFORMS arm64-darwin-20 arm64-darwin-21 + x86_64-linux DEPENDENCIES minitest (~> 5.0) diff --git a/README.md b/README.md index 46fa119..8b876f8 100644 --- a/README.md +++ b/README.md @@ -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 ``` @@ -95,6 +96,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). diff --git a/ext/Rakefile b/ext/Rakefile index 641b415..35712d5 100644 --- a/ext/Rakefile +++ b/ext/Rakefile @@ -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 diff --git a/lib/webview_ruby.rb b/lib/webview_ruby.rb index 5731b48..29bcd7f 100644 --- a/lib/webview_ruby.rb +++ b/lib/webview_ruby.rb @@ -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 @@ -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 From 929a92fc5c390aa83d26dc84e8de1f97da6f0456 Mon Sep 17 00:00:00 2001 From: Andreas Treubert Date: Fri, 27 Sep 2024 08:47:20 +0200 Subject: [PATCH 2/2] readme --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index 8b876f8..0bbac87 100644 --- a/README.md +++ b/README.md @@ -72,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