Skip to content

Commit

Permalink
chore: fix bug and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
hejianxian committed Nov 27, 2019
1 parent 44f833a commit 90bd91c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 46 deletions.
58 changes: 53 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,58 @@

> Simple CordovaJs import util.
## Usage
### 如何使用

当前该js的发布地址为`//workplus.io/cordova-import.js`,可在[Release页面]()进行下载。

> 单页面应用(SPA)引入一次即可,多页面应用需每个页面都逐个引入
简单地在`index.html`头部引入即可:

```html
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<!-- 直接添加即可 -->
<script src="//workplus.io/cordova-import.js" type="text/javascript"></script>

<title>Cordova Import Demo</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
```

### 可选参数

当前仅支持传入`type`类型,可选的值为`http``local`,其中`http`为默认的方式,设置方式如下:

```html
<script src="//workplus.io/cordova-import.js?type=local" type="text/javascript"></script>
```
npm install
npm run build
npm test
```

注意,如果你的页面将可能在iframe里加载,请勿使用`local`类型。`local`类型适合用于对网络性能要求较高的场合。

### 配置说明

```js
const config = {
// 【重要】SDK 的地址,必须正确,否则影响脚本注入
sdkUri: '//workplus.io/cordova-import.js',
...

// 不同平台的 CordovaJs 地址
cordovaImportUri: {
iOS: '//workplus.io/ios.cordova.min.js',
android: '//workplus.io/android.cordova.min.js',
local: 'applocal://cordova.min.js',
},
...
};
```



30 changes: 28 additions & 2 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,35 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!-- 正式测试请替换 src 为 config.sdkUri,必须保持一致,否则注入 script 失败 -->
<script src="../lib/@w6s/cordova-import.js?type=local" type="text/javascript"></script>
<title>Cordova Import Demo</title>
</head>
<body>

<div id="app">
<h3 id="cordova-version"></h3>
<p id="user-agent"></p>
<p id="location"></p>
</div>
<script type="text/javascript">
const cordovaVersionElm = document.getElementById('cordova-version');
const locationElm = document.getElementById('location');

const userAgentElm = document.getElementById('user-agent');
userAgentElm.innerHTML = navigator.userAgent;

document.addEventListener('deviceready', function() {
cordovaVersionElm.innerHTML = `CordovaJs v${cordova.version}`;

cordova.exec(function(res) {
locationElm.innerHTML = JSON.stringify(res);
}, function(err) {
locationElm.innerHTML = JSON.stringify(err);
},
'WorkPlus_Location',
'getLocation',
[]);
}, false);
</script>
</body>
</html>
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ function getImportUriByPlatformAndType(platform, type) {
};

function getCurrentPlatformByUserAgent(userAgent) {
if (userAgent.indexof('iphone')) {
if (userAgent.indexOf('iphone') > -1) {
return 'iOS';
}
if (userAgent.indexOf('android')) {
if (userAgent.indexOf('android') > -1) {
return 'android';
}
return false;
Expand All @@ -51,28 +51,29 @@ function scriptGenerator(uri) {
};

function cordovaImportInit () {
const userAgent = navigator.userAgent.toLowerCase();
try {
const userAgent = navigator.userAgent.toLowerCase();

// 非 WorkPlus webview 不进行任何处理
if (userAgent.indexOf('workplus') === -1) return;
// 非 WorkPlus webview 不进行任何处理
if (userAgent.indexOf('workplus') === -1) return;

const platform = getCurrentPlatformByUserAgent(userAgent);
if (!platform) return;

const SDKScript = getSDKScriptTag();
const search = SDKScript.src.split('?')[1];
const platform = getCurrentPlatformByUserAgent(userAgent);
if (!platform) return;
const SDKScript = getSDKScriptTag();
if (!SDKScript) return;

let opts = {};
if (search) {
try {
const search = SDKScript.src.split('?')[1];
let opts = {};
if (search) {
opts = parse(search);
} catch (err) {
console.err(err);
}
}

const importUri = getImportUriByPlatformAndType(platform, opts.type);
scriptGenerator(importUri);
const importUri = getImportUriByPlatformAndType(platform, opts.type);
scriptGenerator(importUri);
} catch (error) {
console.error(error);
}
};

cordovaImportInit();
37 changes: 19 additions & 18 deletions lib/@w6s/cordova-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@
}

function getCurrentPlatformByUserAgent(userAgent) {
if (userAgent.indexof('iphone')) {
if (userAgent.indexOf('iphone') > -1) {
return 'iOS';
}

if (userAgent.indexOf('android')) {
if (userAgent.indexOf('android') > -1) {
return 'android';
}

Expand All @@ -109,25 +109,26 @@
}

function cordovaImportInit() {
var userAgent = navigator.userAgent.toLowerCase(); // 非 WorkPlus webview 不进行任何处理

if (userAgent.indexOf('workplus') === -1) return;
var platform = getCurrentPlatformByUserAgent(userAgent);
if (!platform) return;
var SDKScript = getSDKScriptTag();
var search = SDKScript.src.split('?')[1];
var opts = {};

if (search) {
try {
try {
var userAgent = navigator.userAgent.toLowerCase(); // 非 WorkPlus webview 不进行任何处理

if (userAgent.indexOf('workplus') === -1) return;
var platform = getCurrentPlatformByUserAgent(userAgent);
if (!platform) return;
var SDKScript = getSDKScriptTag();
if (!SDKScript) return;
var search = SDKScript.src.split('?')[1];
var opts = {};

if (search) {
opts = parse(search);
} catch (err) {
console.err(err);
}
}

var importUri = getImportUriByPlatformAndType(platform, opts.type);
scriptGenerator(importUri);
var importUri = getImportUriByPlatformAndType(platform, opts.type);
scriptGenerator(importUri);
} catch (error) {
console.error(error);
}
}
cordovaImportInit();

Expand Down
2 changes: 1 addition & 1 deletion lib/@w6s/cordova-import.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
"description": "Simple CordovaJs import util.",
"main": "lib/@w6s/cordova-import.js",
"main:min": "lib/@w6s/cordova-import.min.js",
"publishConfig": {
"access": "public"
},
"scripts": {
"test": "ava test/index.js",
"pretest": "npm run build",
"build:common": "rollup -c",
"build:prod": "cross-env NODE_ENV=production rollup -c",
"build": "npm run build:common && npm run build:prod",
"watch": "rollup -c -w",
"prepublish": "npm test"
"prepublish": "npm test",
"example": "npx serve ./"
},
"keywords": [],
"keywords": [
"WorkPlusFE",
"w6s",
"cordova-import",
"cordova"
],
"author": "WorkPlusFE",
"license": "MIT",
"devDependencies": {
Expand Down

0 comments on commit 90bd91c

Please sign in to comment.