|
@@ -1,5 +1,4 @@
|
1
|
|
-import escapeStringRegexp from 'escape-string-regexp';
|
2
|
|
-import { Linking, UIManager as NotTypedUIManager } from 'react-native';
|
|
1
|
+import { UIManager as NotTypedUIManager } from 'react-native';
|
3
|
2
|
import {
|
4
|
3
|
WebViewNavigationEvent,
|
5
|
4
|
OnShouldStartLoadWithRequest,
|
|
@@ -8,52 +7,24 @@ import {
|
8
|
7
|
|
9
|
8
|
const UIManager = NotTypedUIManager as CustomUIManager;
|
10
|
9
|
|
11
|
|
-const defaultOriginWhitelist = ['http://*', 'https://*'];
|
12
|
|
-
|
13
|
|
-const extractOrigin = (url: string): string => {
|
14
|
|
- const result = /^[A-Za-z][A-Za-z0-9+\-.]+:(\/\/)?[^/]*/.exec(url);
|
15
|
|
- return result === null ? '' : result[0];
|
16
|
|
-};
|
17
|
|
-
|
18
|
|
-const originWhitelistToRegex = (originWhitelist: string): string =>
|
19
|
|
- `^${escapeStringRegexp(originWhitelist).replace(/\\\*/g, '.*')}`;
|
20
|
|
-
|
21
|
|
-const passesWhitelist = (
|
22
|
|
- compiledWhitelist: ReadonlyArray<string>,
|
23
|
|
- url: string,
|
24
|
|
-) => {
|
25
|
|
- const origin = extractOrigin(url);
|
26
|
|
- return compiledWhitelist.some(x => new RegExp(x).test(origin));
|
27
|
|
-};
|
28
|
|
-
|
29
|
|
-const compileWhitelist = (
|
30
|
|
- originWhitelist: ReadonlyArray<string>,
|
31
|
|
-): ReadonlyArray<string> =>
|
32
|
|
- ['about:blank', ...(originWhitelist || [])].map(originWhitelistToRegex);
|
33
|
|
-
|
34
|
10
|
const createOnShouldStartLoadWithRequest = (
|
35
|
11
|
loadRequest: (
|
36
|
12
|
shouldStart: boolean,
|
37
|
13
|
url: string,
|
38
|
14
|
lockIdentifier: number,
|
39
|
15
|
) => void,
|
40
|
|
- originWhitelist: ReadonlyArray<string>,
|
41
|
16
|
onShouldStartLoadWithRequest?: OnShouldStartLoadWithRequest,
|
42
|
17
|
) => {
|
43
|
18
|
return ({ nativeEvent }: WebViewNavigationEvent) => {
|
44
|
|
- let shouldStart = true;
|
45
|
19
|
const { url, lockIdentifier } = nativeEvent;
|
46
|
20
|
|
47
|
|
- if (!passesWhitelist(compileWhitelist(originWhitelist), url)) {
|
48
|
|
- Linking.openURL(url);
|
49
|
|
- shouldStart = false;
|
50
|
|
- }
|
51
|
|
-
|
52
|
|
- if (onShouldStartLoadWithRequest) {
|
53
|
|
- shouldStart = onShouldStartLoadWithRequest(nativeEvent);
|
|
21
|
+ if (
|
|
22
|
+ onShouldStartLoadWithRequest
|
|
23
|
+ && !onShouldStartLoadWithRequest(nativeEvent)
|
|
24
|
+ ) {
|
|
25
|
+ loadRequest(false, url, lockIdentifier);
|
54
|
26
|
}
|
55
|
|
-
|
56
|
|
- loadRequest(shouldStart, url, lockIdentifier);
|
|
27
|
+ loadRequest(true, url, lockIdentifier);
|
57
|
28
|
};
|
58
|
29
|
};
|
59
|
30
|
|
|
@@ -66,8 +37,4 @@ const getViewManagerConfig = (
|
66
|
37
|
return UIManager.getViewManagerConfig(viewManagerName);
|
67
|
38
|
};
|
68
|
39
|
|
69
|
|
-export {
|
70
|
|
- defaultOriginWhitelist,
|
71
|
|
- createOnShouldStartLoadWithRequest,
|
72
|
|
- getViewManagerConfig,
|
73
|
|
-};
|
|
40
|
+export { createOnShouldStartLoadWithRequest, getViewManagerConfig };
|