-
Notifications
You must be signed in to change notification settings - Fork 0
/
Browser.js
159 lines (136 loc) · 4.33 KB
/
Browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import React, { Component } from "react";
import {
StatusBar,
StyleSheet,
View,
Image,
TouchableOpacity,
ActivityIndicator,
} from "react-native";
import { WebView } from "react-native-webview";
import arrowBackIcon from './assets/arrow_back.png';
import arrowNextIcon from './assets/arrow_next.png';
// initial url for the browser
const url = "https://bankidz.com";
// javascript to inject into the window
const injectedJavaScript = `
window.ReactNativeWebView.postMessage('injected javascript works!');
true; // note: this is required, or you'll sometimes get silent failures
`;
class Browser extends Component {
state = {
currentURL: url,
canGoForward: false,
canGoBack: false,
browserRef: null
};
// go to the next page
goForward = () => {
if (this.browserRef && this.state.canGoForward) {
this.browserRef.goForward();
}
};
// go back to the last page
goBack = () => {
if (this.browserRef && this.state.canGoBack) {
this.browserRef.goBack();
}
};
// set the reference for the browser
setBrowserRef = (browser) => {
if (!this.browserRef) {
this.browserRef = browser
}
};
// called when there is an error in the browser
onBrowserError = (syntheticEvent) => {
const { nativeEvent } = syntheticEvent;
console.warn('WebView error: ', nativeEvent)
};
// called when the webview is loaded
onBrowserLoad = (syntheticEvent) => {
const { canGoForward, canGoBack, title } = syntheticEvent.nativeEvent;
this.setState({
canGoForward,
canGoBack,
title
})
};
// called when the navigation state changes (page load)
onNavigationStateChange = (navState) => {
const { canGoForward, canGoBack, title } = navState;
this.setState({
canGoForward,
canGoBack,
title
})
};
// called when the browser sends a message using "window.ReactNativeWebView.postMessage"
onBrowserMessage = (event) => {
console.log('*'.repeat(10));
console.log('Got message from the browser:', event.nativeEvent.data);
console.log('*'.repeat(10));
};
render() {
const { state } = this;
const { currentURL, canGoForward, canGoBack } = state;
return (
<View style={styles.root}>
<View style={{ flex: 0.1, backgroundColor: '#000000' }}>
<StatusBar hidden={false} barStyle="dark-content" translucent={true} />
</View>
<View style={styles.browserContainer}>
<WebView
ref={this.setBrowserRef}
originWhitelist={['*']}
source={{ uri: currentURL }}
onNavigationStateChange={this.onNavigationStateChange}
renderLoading={() => <ActivityIndicator size="large" color="#0000ff" />}
onShouldStartLoadWithRequest={this.filterRequest}
/>
</View>
<View style={styles.browserBar}>
<TouchableOpacity onPress={this.goBack}>
<Image
style={[styles.icon, canGoBack ? {} : styles.disabled]}
source={arrowBackIcon} />
</TouchableOpacity>
<TouchableOpacity onPress={this.goForward}>
<Image
style={[styles.icon, canGoForward ? {} : styles.disabled]}
source={arrowNextIcon} />
</TouchableOpacity>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
browser: {
flex: 1,
},
root: {
flex: 1,
},
icon: {
width: 30,
height: 30,
tintColor: 'white',
resizeMode: 'contain'
},
disabled: {
opacity: 0.3
},
browserBar: {
flex: 0.15,
backgroundColor: 'black',
flexDirection: 'row',
alignItems: 'center',
paddingHorizontal: 60,
justifyContent: 'space-between',
},
browserContainer: {
flex: 2,
}
});
export default Browser;