Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

[WIP] Open external links in external browser #21

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 22 additions & 15 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,26 @@ export default class App extends React.Component {
});

// JS injected to `WebView`
// Embedded website will change its functionality based on this.
appInfoJavaScript = 'window.trMobileApp=' + JSON.stringify(this.appInfo) + ';';
injectedJavaScript = `
function postMessage(message) {
if (window.ReactNativeWebView && typeof window.ReactNativeWebView.postMessage == 'function') {
window.ReactNativeWebView.postMessage(JSON.stringify(message));
} else {
console.error("'window.ReactNativeWebView.postMessage' is not a function");
}
}

// This is needed because we want to subscribe notifications only
// if user is authenticated window.ReactNativeWebView.postMessage
// accepts one argument, data, which will be available on the event
// object, event.nativeEvent.data. data must be a string.
if (window.user && window.user._id) {
postMessage({ "action": "authenticated" });
}

// Embedded website will change its functionality based on this.
window.trMobileApp = ${JSON.stringify(this.appInfo)};
`;

componentWillMount() {
// Subscribe to push notifications
Expand Down Expand Up @@ -266,18 +284,7 @@ export default class App extends React.Component {
// I.e. on each URL change
_handleLoadEnd = () => {
console.log('handleLoadEnd');
this.webView.injectJavaScript(
// This is needed because we want to subscribe notifications only
// if user is authenticated window.ReactNativeWebView.postMessage
// accepts one argument, data, which will be available on the event
// object, event.nativeEvent.data. data must be a string.
`
if (window.user && window.user._id && typeof window.ReactNativeWebView.postMessage === 'function') {
window.ReactNativeWebView.postMessage('{ "action": "authenticated" }');
}
${this.appInfoJavaScript}
`
);
this.webView.injectJavaScript(this.injectedJavaScript);
};

_handleError = error => {
Expand All @@ -303,7 +310,7 @@ export default class App extends React.Component {
<StatusBar backgroundColor={this.styles.statusBar.backgroundColor} barStyle="default" />
<WebView
domStorageEnabled
injectedJavaScript={this.appInfoJavaScript}
injectedJavaScript={this.injectedJavaScript}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this line is actually needed at all, since we're loading the same code on every onLoadEnd, but I donno 🤷‍♀️

onError={this._handleError}
onLoadEnd={this._handleLoadEnd}
onMessage={this.appInfo.os === 'ios' ? null : this._handleMessage}
Expand Down