-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
60 lines (52 loc) · 1.44 KB
/
lib.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
'use strict';
import invariant from 'invariant';
import merge from 'lodash.merge';
import config from './config';
import {
isHttpsProtocol,
inIframe,
isiOSPlatform,
isAndroidPlatform,
isX5Webview,
getCordovaJsUriByPlatform,
} from './utils';
export default class CordovaImportLib {
constructor(host) {
if (host) {
this.config = merge(config, { SDK_HOST: host });
} else {
this.config = config;
}
}
get userAgent() {
return window.navigator.userAgent.toLowerCase();
}
/**
* 返回注入的地址
*
* 默认使用 local模式,当检测到是 https协议 (安卓) 或在 iframe 中打开时,强制使用 http 模式
*/
getImportUri() {
// 以 es模块 初始化的情况下,不传人 SDK_HOST,默认只需要支持 local
if (this.mustUseHttpModel() && this.config.SDK_HOST) {
const platform = this.getCurrentPlatformByUserAgent();
invariant(platform, 'Please open in the correct webview');
return getCordovaJsUriByPlatform(this.config, platform);
}
return this.config.cordovajs.local;
}
mustUseHttpModel() {
if (inIframe()) return true;
if (isHttpsProtocol() && isAndroidPlatform(this.userAgent) && !isX5Webview()) return true;
return false;
}
getCurrentPlatformByUserAgent() {
if (isiOSPlatform(this.userAgent)) {
return 'iOS';
}
if (isAndroidPlatform(this.userAgent)) {
return 'android';
}
return false;
}
}