From 90bd91c7c6f69540a30d0a21439057ebed4f4cf1 Mon Sep 17 00:00:00 2001 From: hejianxian <531601727@qq.com> Date: Wed, 27 Nov 2019 23:15:33 +0800 Subject: [PATCH] chore: fix bug and add docs --- README.md | 58 +++++++++++++++++++++++++++++++--- example/index.html | 30 ++++++++++++++++-- index.js | 37 +++++++++++----------- lib/@w6s/cordova-import.js | 37 +++++++++++----------- lib/@w6s/cordova-import.min.js | 2 +- package.json | 13 ++++++-- 6 files changed, 131 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 3aea961..bdb3208 100644 --- a/README.md +++ b/README.md @@ -2,10 +2,58 @@ > Simple CordovaJs import util. -## Usage +### 如何使用 +当前该js的发布地址为`//workplus.io/cordova-import.js`,可在[Release页面]()进行下载。 + +> 单页面应用(SPA)引入一次即可,多页面应用需每个页面都逐个引入 + +简单地在`index.html`头部引入即可: + +```html + + + + + + + + + Cordova Import Demo + + +
+ + +``` + +### 可选参数 + +当前仅支持传入`type`类型,可选的值为`http`及`local`,其中`http`为默认的方式,设置方式如下: + +```html + ``` -npm install -npm run build -npm test -``` \ No newline at end of file + +注意,如果你的页面将可能在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', + }, + ... +}; +``` + + + diff --git a/example/index.html b/example/index.html index e160174..8b268ce 100644 --- a/example/index.html +++ b/example/index.html @@ -3,9 +3,35 @@ - Document + + + Cordova Import Demo - +
+

+

+

+
+ \ No newline at end of file diff --git a/index.js b/index.js index e8b2f8c..14b7cdb 100644 --- a/index.js +++ b/index.js @@ -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; @@ -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(); diff --git a/lib/@w6s/cordova-import.js b/lib/@w6s/cordova-import.js index 5d3db72..864f8d9 100644 --- a/lib/@w6s/cordova-import.js +++ b/lib/@w6s/cordova-import.js @@ -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'; } @@ -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(); diff --git a/lib/@w6s/cordova-import.min.js b/lib/@w6s/cordova-import.min.js index a6c28d7..549c018 100644 --- a/lib/@w6s/cordova-import.min.js +++ b/lib/@w6s/cordova-import.min.js @@ -2,4 +2,4 @@ * @w6s/cordova-import.js v1.0.0 * (c) 2019 WorkPlusFE */ -!function(o){"function"==typeof define&&define.amd?define(o):o()}(function(){"use strict";var s={sdkUri:"//workplus.io/cordova-import.js",importTypes:["http","local"],defaultImportType:"http",cordovaImportUri:{iOS:"//workplus.io/ios.cordova.min.js",android:"//workplus.io/android.cordova.min.js",local:"applocal://cordova.min.js"},pcSDKUri:""};!function(){var o=navigator.userAgent.toLowerCase();if(~o.indexOf("workplus")){var t,e=(t=o).indexof("iphone")?"iOS":!!t.indexOf("android")&&"android";if(e){var r=function(){for(var o=document.getElementsByTagName("script"),t=null,e=0;e