-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwithInterceptLink.js
73 lines (63 loc) · 1.87 KB
/
withInterceptLink.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
import React from 'react';
import { Linking } from 'react-native';
import URI from 'urijs';
import PropTypes from 'prop-types';
export default WrappedWebView => class extends React.Component {
static propTypes = {
callbackToWebpage: PropTypes.func.isRequired,
onWebRequest: PropTypes.func,
onShouldStartLoadWithRequest: PropTypes.func,
};
static defaultProps = {
onShouldStartLoadWithRequest: () => true,
onWebRequest: () => Promise.resolve({}),
};
onShouldStartLoadWithRequest(req) {
const { callbackToWebpage, onWebRequest, onShouldStartLoadWithRequest } = this.props;
const { url } = req || {};
if (!url) {
return false;
}
const uri = URI(url);
const host = uri.host();
const scheme = uri.scheme();
const path = uri.path();
const idStr = /id\d+$/;
const httpx = /(^http)|(^https)/;
const query = uri.search(true);
if (httpx.test(url)) {
if ((host === 'itunes.apple.com' && idStr.test(path)) || host === 'a.app.qq.com') {
Linking.openURL(url);
return false;
}
return true;
}
const { isSetCallback, callback_name: callbackName } = query;
if (scheme === 'mobile') {
Promise.resolve(onWebRequest({ scheme, method: host, query }))
.then((resp) => {
if (!callbackName) return;
if (isSetCallback) return;
callbackToWebpage(callbackName, resp);
})
.catch((e) => {
if (!callbackName) return;
if (isSetCallback) return;
callbackToWebpage(callbackName, e);
});
return false;
}
if (onShouldStartLoadWithRequest) {
return onShouldStartLoadWithRequest(req);
}
return true;
}
render() {
return (
<WrappedWebView
{...this.props}
onShouldStartLoadWithRequest={req => this.onShouldStartLoadWithRequest(req)}
/>
);
}
};