File tree Expand file tree Collapse file tree 1 file changed +24
-8
lines changed Expand file tree Collapse file tree 1 file changed +24
-8
lines changed Original file line number Diff line number Diff line change @@ -86,17 +86,33 @@ This example can be found in `examples` folder.
86
86
87
87
``` js
88
88
const injectScript = `
89
- (function () {
90
- if (WebViewBridge) {
89
+ function webViewBridgeReady(cb) {
90
+ //checks whether WebViewBirdge exists in global scope.
91
+ if (window.WebViewBridge) {
92
+ cb(window.WebViewBridge);
93
+ return;
94
+ }
91
95
92
- WebViewBridge.onMessage = function (message) {
93
- alert('got a message from Native: ' + message);
96
+ function handler() {
97
+ //remove the handler from listener since we don't need it anymore
98
+ document.removeEventListener('WebViewBridge', handler, false);
99
+ //pass the WebViewBridge object to the callback
100
+ cb(window.WebViewBridge);
101
+ }
94
102
95
- WebViewBridge.send("message from webview");
96
- };
103
+ //if WebViewBridge doesn't exist in global scope attach itself to document
104
+ //event system. Once the code is being injected by extension, the handler will
105
+ //be called.
106
+ document.addEventListener('WebViewBridge', handler, false);
107
+ }
97
108
98
- }
99
- }());
109
+ webViewBridgeReady(function (webViewBridge) {
110
+ WebViewBridge.onMessage = function (message) {
111
+ alert('got a message from Native: ' + message);
112
+
113
+ WebViewBridge.send("message from webview");
114
+ };
115
+ });
100
116
` ;
101
117
102
118
var Sample2 = React .createClass ({
You can’t perform that action at this time.
0 commit comments