Skip to content

Commit 2e76ffa

Browse files
committed
updated doc
1 parent c16160f commit 2e76ffa

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

README.md

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,33 @@ This example can be found in `examples` folder.
8686

8787
```js
8888
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+
}
9195
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+
}
94102
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+
}
97108
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+
});
100116
`;
101117

102118
var Sample2 = React.createClass({

0 commit comments

Comments
 (0)