cocoawebview 0.1.3 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/cocoawebview/cocoawebview.m +15 -4
- data/lib/cocoawebview/version.rb +1 -1
- data/lib/cocoawebview.rb +23 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0cef856d1dd01d84abd5dfaf56c4e72f1913ed1299a279cef6cbdb050a344d14
|
4
|
+
data.tar.gz: a548286f028a040cf9081512cb56547643d2eefdcc07861d3dac0f03a105bd4d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 122819e31ab8b0cea3ed0e2d11a594ef6c519864dd0f864d266e0782cd6934e269aba4eaa909d1d67ddb2a608aa356c0da6cf90d9722ead983cb626fee8fc92a
|
7
|
+
data.tar.gz: 63233fcba0572fbdf4b471b008b2cfe235bfae903ff8c854ac518ae7480c613251bc470f0b97d78ee6b429ab5bc47c96bda6e0b6e552cf88881df331c6c6f55b
|
@@ -32,7 +32,7 @@ VALUE webview_eval(VALUE self, VALUE code);
|
|
32
32
|
}
|
33
33
|
@end
|
34
34
|
|
35
|
-
@interface CocoaWebview : NSWindow {
|
35
|
+
@interface CocoaWebview : NSWindow <WKScriptMessageHandler> {
|
36
36
|
WKWebView *webView;
|
37
37
|
VALUE rb_cocoawebview;
|
38
38
|
BOOL showDevTool;
|
@@ -62,6 +62,15 @@ VALUE webview_eval(VALUE self, VALUE code);
|
|
62
62
|
return self;
|
63
63
|
}
|
64
64
|
|
65
|
+
- (void)userContentController:(WKUserContentController *)userContentController
|
66
|
+
didReceiveScriptMessage:(WKScriptMessage *)message {
|
67
|
+
if ([message.name isEqualToString:@"native"]) {
|
68
|
+
const char *body = [message.body UTF8String];
|
69
|
+
VALUE rb_body = rb_str_new_cstr(body);
|
70
|
+
rb_funcall(rb_cocoawebview, rb_intern("webview_msg_handler"), 1, rb_body);
|
71
|
+
}
|
72
|
+
}
|
73
|
+
|
65
74
|
- (void)setDevTool:(BOOL)flag {
|
66
75
|
showDevTool = flag;
|
67
76
|
}
|
@@ -79,11 +88,15 @@ VALUE webview_eval(VALUE self, VALUE code);
|
|
79
88
|
}
|
80
89
|
|
81
90
|
- (void)addWebViewToWindow:(NSWindow *)window {
|
91
|
+
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
|
92
|
+
[userContentController addScriptMessageHandler:self name:@"native"];
|
93
|
+
|
82
94
|
// Create a configuration if needed
|
83
95
|
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
|
84
96
|
|
85
97
|
[[config preferences] setValue:@YES forKey:@"fullScreenEnabled"];
|
86
98
|
|
99
|
+
config.userContentController = userContentController;
|
87
100
|
if (showDevTool) {
|
88
101
|
[[config preferences] setValue:@YES forKey:@"developerExtrasEnabled"];
|
89
102
|
}
|
@@ -110,9 +123,6 @@ VALUE webview_eval(VALUE self, VALUE code);
|
|
110
123
|
[window setContentView:webView];
|
111
124
|
|
112
125
|
webView.navigationDelegate = self;
|
113
|
-
|
114
|
-
NSString *html = @"<html><body><h1>Hello from WKWebView</h1><script>function sayHello() { console.log('Hello JS!'); }</script></body></html>";
|
115
|
-
[webView loadHTMLString:html baseURL:nil];
|
116
126
|
}
|
117
127
|
|
118
128
|
// Called when the web view finishes loading
|
@@ -165,6 +175,7 @@ VALUE nsapp_run(VALUE self) {
|
|
165
175
|
|
166
176
|
VALUE webview_initialize(VALUE self, VALUE debug) {
|
167
177
|
rb_iv_set(self, "@var", rb_hash_new());
|
178
|
+
rb_iv_set(self, "@bindings", rb_hash_new());
|
168
179
|
BOOL flag = NO;
|
169
180
|
if (debug == Qtrue) {
|
170
181
|
flag = YES;
|
data/lib/cocoawebview/version.rb
CHANGED
data/lib/cocoawebview.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'json'
|
3
4
|
require_relative "cocoawebview/version"
|
4
5
|
require_relative "cocoawebview/cocoawebview"
|
5
6
|
|
@@ -21,5 +22,27 @@ module CocoaWebview
|
|
21
22
|
def webview_did_load
|
22
23
|
puts "CocoaWebview did loaded"
|
23
24
|
end
|
25
|
+
|
26
|
+
def bind(name, &block)
|
27
|
+
param_info = block.parameters
|
28
|
+
param_count = param_info.count { |type, _| type == :req || type == :opt }
|
29
|
+
args = (1..param_count).map { |i| "arg#{i}" }.join(", ")
|
30
|
+
code = %`
|
31
|
+
function #{name}(#{args}) {
|
32
|
+
body = {"function": "#{name}", args: [#{args}]};
|
33
|
+
window.webkit.messageHandlers.native.postMessage(JSON.stringify(body));
|
34
|
+
}
|
35
|
+
`
|
36
|
+
@bindings[name] = block
|
37
|
+
self.eval(code)
|
38
|
+
end
|
39
|
+
|
40
|
+
def webview_msg_handler(msg)
|
41
|
+
hash = JSON.parse(msg)
|
42
|
+
function = hash["function"]
|
43
|
+
args = hash["args"]
|
44
|
+
callback = @bindings[function]
|
45
|
+
callback.call(*args) if callback
|
46
|
+
end
|
24
47
|
end
|
25
48
|
end
|