From 3602f77343ee2db608c6ed917e671b50cb647acc Mon Sep 17 00:00:00 2001 From: Skylar Ittner Date: Tue, 26 Nov 2019 21:45:46 -0700 Subject: [PATCH] Make barcode scanner work on Cordova browser platform (close #28) --- license-credits.md | 6 +- package.json | 4 +- scripts/remove_bloat.sh | 3 +- www/assets/js/platform.js | 196 ++++++++++++++++++++------------------ www/pages/credits.html | 6 +- 5 files changed, 114 insertions(+), 101 deletions(-) diff --git a/license-credits.md b/license-credits.md index ad2e6ea..727d1ac 100644 --- a/license-credits.md +++ b/license-credits.md @@ -120,7 +120,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The following software may be included in this product: leaflet. A copy of the source code may be downloaded from git://github.com/Leaflet/Leaflet.git. This software contains the following license and notice below: -Copyright (c) 2010-2018, Vladimir Agafonkin +Copyright (c) 2010-2019, Vladimir Agafonkin Copyright (c) 2010-2011, CloudMade All rights reserved. @@ -1907,7 +1907,7 @@ The Apache Software Foundation (http://www.apache.org/). ----- -The following software may be included in this product: cordova-plugin-inappbrowser, cordova-plugin-whitelist. A copy of the source code may be downloaded from https://github.com/apache/cordova-plugin-inappbrowser (cordova-plugin-inappbrowser), https://github.com/apache/cordova-plugin-whitelist (cordova-plugin-whitelist). This software contains the following license and notice below: +The following software may be included in this product: cordova-plugin-device, cordova-plugin-inappbrowser, cordova-plugin-whitelist. A copy of the source code may be downloaded from https://github.com/apache/cordova-plugin-device (cordova-plugin-device), https://github.com/apache/cordova-plugin-inappbrowser (cordova-plugin-inappbrowser), https://github.com/apache/cordova-plugin-whitelist (cordova-plugin-whitelist). This software contains the following license and notice below: Apache License Version 2.0, January 2004 @@ -2962,7 +2962,7 @@ THE SOFTWARE. ----- -The following software may be included in this product: es-to-primitive, is-callable, is-date-object, is-symbol, string.prototype.trimleft, string.prototype.trimright. A copy of the source code may be downloaded from git://github.com/ljharb/es-to-primitive.git (es-to-primitive), git://github.com/ljharb/is-callable.git (is-callable), git://github.com/ljharb/is-date-object.git (is-date-object), git://github.com/ljharb/is-symbol.git (is-symbol), git://github.com/es-shims/String.prototype.trimLeft.git (string.prototype.trimleft), git://github.com/es-shims/String.prototype.trimRight.git (string.prototype.trimright). This software contains the following license and notice below: +The following software may be included in this product: es-to-primitive, is-callable, is-date-object, is-symbol, string.prototype.trimleft, string.prototype.trimright. A copy of the source code may be downloaded from git://github.com/ljharb/es-to-primitive.git (es-to-primitive), git://github.com/ljharb/is-callable.git (is-callable), git://github.com/ljharb/is-date-object.git (is-date-object), git://github.com/inspect-js/is-symbol.git (is-symbol), git://github.com/es-shims/String.prototype.trimLeft.git (string.prototype.trimleft), git://github.com/es-shims/String.prototype.trimRight.git (string.prototype.trimright). This software contains the following license and notice below: The MIT License (MIT) diff --git a/package.json b/package.json index 4e50390..d875c38 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "cordova-plugin-inappbrowser": {}, "cordova-plugin-powermanagement-netsyms": {}, "cordova-plugin-whitelist": {}, - "phonegap-plugin-barcodescanner": {} + "phonegap-plugin-barcodescanner": {}, + "cordova-plugin-device": {} }, "platforms": [ "android", @@ -28,6 +29,7 @@ "dependencies": { "cordova-android": "^8.1.0", "cordova-browser": "^6.0.0", + "cordova-plugin-device": "^2.0.3", "cordova-plugin-inappbrowser": "^3.1.0", "cordova-plugin-powermanagement-netsyms": "git+https://source.netsyms.com/Netsyms/cordova-plugin-powermanagement", "cordova-plugin-whitelist": "^1.3.4" diff --git a/scripts/remove_bloat.sh b/scripts/remove_bloat.sh index be73130..aefb98f 100755 --- a/scripts/remove_bloat.sh +++ b/scripts/remove_bloat.sh @@ -29,6 +29,7 @@ cd jsbarcode rm -rf .dockerignore .eslintignore .eslintrcautomation bower.json CONTRIBUTING.md docker-compose.yml Dockerfile example gulpfile.js jsbarcode.d.ts README.md src test .travis.yml rm -rf dist/barcodes rm -rf dist/JsBarcode.all.js +cd .. cd framework7 rm -rf components @@ -36,8 +37,8 @@ rm -rf lazy-components rm -rf less rm -rf modules rm -rf utils - cd .. + cd material-design-icons rm -rf action rm -rf alert diff --git a/www/assets/js/platform.js b/www/assets/js/platform.js index 826b8a5..b2902e3 100644 --- a/www/assets/js/platform.js +++ b/www/assets/js/platform.js @@ -64,98 +64,7 @@ var watchLocation = function (success, error) { } } -function initCordova() { - platform_type = "cordova"; - - // Handle back button to close things - document.addEventListener("backbutton", function (event) { - router.back({force: true, ignoreCache: true}); - }, false); - - document.addEventListener("deviceready", function () { - if (localStorage.getItem("wakelock") == "true") { - window.powerManagement.acquire(function () { - console.log('Wakelock acquired'); - }, function () { - console.log('Failed to acquire wakelock'); - }); - } else { - window.powerManagement.release(function () { - console.log('Wakelock released'); - }, function () { - console.log('Failed to release wakelock'); - }); - } - }, false); - - openBrowser = function (url) { - cordova.InAppBrowser.open(url, '_blank', 'location=yes'); - } - - openExternalBrowser = function (url) { - window.open(url, '_system', ''); - } - - scanBarcode = function (success, error) { - cordova.plugins.barcodeScanner.scan( - function (result) { - if (!result.cancelled) { - success(result.text); - } - }, - function (err) { - if (typeof error == "function") { - error(err); - } - }, - { - showTorchButton: true, - showFlipCameraButton : true, - prompt: "Scan barcode", - resultDisplayDuration: 0, - disableSuccessBeep: true, - formats: "QR_CODE,DATA_MATRIX,CODE_39,CODE_93,CODE_128,CODABAR,PDF_417,AZTEC,MAXICODE" - } - ); - } -} - -function initNW() { - platform_type = "nw"; - platform_theme = "md"; - - openBrowser = function (url) { - nw.Window.open(url, { - id: url - }, function (browserwin) { - // Add menubar so the user can navigate around if they click a link - var browsermenu = new nw.Menu({type: 'menubar'}); - browsermenu.append(new nw.MenuItem({ - label: "Back", - click: function () { - browserwin.window.history.back(); - } - })); - browsermenu.append(new nw.MenuItem({ - label: "Forward", - click: function () { - browserwin.window.history.forward(); - } - })); - browsermenu.append(new nw.MenuItem({ - label: "Home", - click: function () { - browserwin.window.location.href = url; - } - })); - browserwin.menu = browsermenu; - }); - } - - openExternalBrowser = function (url) { - require('nw.gui').Shell.openExternal(url); - } - +function setupHTML5BarcodeScanner() { $("body").append(''); scanBarcode = function (success, error) { @@ -200,13 +109,114 @@ function initNW() { }; } +function initCordova() { + platform_type = "cordova"; + // Handle back button to close things + document.addEventListener("backbutton", function (event) { + router.back({force: true, ignoreCache: true}); + }, false); + document.addEventListener("deviceready", function () { + if (localStorage.getItem("wakelock") == "true") { + window.powerManagement.acquire(function () { + console.log('Wakelock acquired'); + }, function () { + console.log('Failed to acquire wakelock'); + }); + } else { + window.powerManagement.release(function () { + console.log('Wakelock released'); + }, function () { + console.log('Failed to release wakelock'); + }); + } + }, false); + openBrowser = function (url) { + cordova.InAppBrowser.open(url, '_blank', 'location=yes'); + } + + openExternalBrowser = function (url) { + window.open(url, '_system', ''); + } + + if (typeof device != "undefined" && device.platform != "browser") { + scanBarcode = function (success, error) { + cordova.plugins.barcodeScanner.scan( + function (result) { + if (!result.cancelled) { + success(result.text); + } + }, + function (err) { + if (typeof error == "function") { + error(err); + } + }, + { + showTorchButton: true, + showFlipCameraButton: true, + prompt: "Scan barcode", + resultDisplayDuration: 0, + disableSuccessBeep: true, + formats: "QR_CODE,DATA_MATRIX,CODE_39,CODE_93,CODE_128,CODABAR,PDF_417,AZTEC,MAXICODE" + } + ); + }; + } else { + setupHTML5BarcodeScanner(); + } +} + +function initNW() { + platform_type = "nw"; + platform_theme = "md"; + openBrowser = function (url) { + nw.Window.open(url, { + id: url + }, function (browserwin) { + // Add menubar so the user can navigate around if they click a link + var browsermenu = new nw.Menu({type: 'menubar'}); + browsermenu.append(new nw.MenuItem({ + label: "Back", + click: function () { + browserwin.window.history.back(); + } + })); + browsermenu.append(new nw.MenuItem({ + label: "Forward", + click: function () { + browserwin.window.history.forward(); + } + })); + browsermenu.append(new nw.MenuItem({ + label: "Home", + click: function () { + browserwin.window.location.href = url; + } + })); + browserwin.menu = browsermenu; + }); + } + + openExternalBrowser = function (url) { + require('nw.gui').Shell.openExternal(url); + } + + + setupHTML5BarcodeScanner(); +} + function initBrowser() { platform_type = "browser"; platform_theme = "md"; - openBrowser = function (url) { window.open(url); } + + openExternalBrowser = function (url) { + window.open(url); + } + + setupHTML5BarcodeScanner(); } function initPlatform() { diff --git a/www/pages/credits.html b/www/pages/credits.html index 043deec..8a3590d 100644 --- a/www/pages/credits.html +++ b/www/pages/credits.html @@ -225,7 +225,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. The following software may be included in this product: leaflet. A copy of the source code may be downloaded from git://github.com/Leaflet/Leaflet.git. This software contains the following license and notice below: -Copyright (c) 2010-2018, Vladimir Agafonkin +Copyright (c) 2010-2019, Vladimir Agafonkin Copyright (c) 2010-2011, CloudMade All rights reserved. @@ -2012,7 +2012,7 @@ The Apache Software Foundation (http://www.apache.org/). ----- -The following software may be included in this product: cordova-plugin-inappbrowser, cordova-plugin-whitelist. A copy of the source code may be downloaded from https://github.com/apache/cordova-plugin-inappbrowser (cordova-plugin-inappbrowser), https://github.com/apache/cordova-plugin-whitelist (cordova-plugin-whitelist). This software contains the following license and notice below: +The following software may be included in this product: cordova-plugin-device, cordova-plugin-inappbrowser, cordova-plugin-whitelist. A copy of the source code may be downloaded from https://github.com/apache/cordova-plugin-device (cordova-plugin-device), https://github.com/apache/cordova-plugin-inappbrowser (cordova-plugin-inappbrowser), https://github.com/apache/cordova-plugin-whitelist (cordova-plugin-whitelist). This software contains the following license and notice below: Apache License Version 2.0, January 2004 @@ -3067,7 +3067,7 @@ THE SOFTWARE. ----- -The following software may be included in this product: es-to-primitive, is-callable, is-date-object, is-symbol, string.prototype.trimleft, string.prototype.trimright. A copy of the source code may be downloaded from git://github.com/ljharb/es-to-primitive.git (es-to-primitive), git://github.com/ljharb/is-callable.git (is-callable), git://github.com/ljharb/is-date-object.git (is-date-object), git://github.com/ljharb/is-symbol.git (is-symbol), git://github.com/es-shims/String.prototype.trimLeft.git (string.prototype.trimleft), git://github.com/es-shims/String.prototype.trimRight.git (string.prototype.trimright). This software contains the following license and notice below: +The following software may be included in this product: es-to-primitive, is-callable, is-date-object, is-symbol, string.prototype.trimleft, string.prototype.trimright. A copy of the source code may be downloaded from git://github.com/ljharb/es-to-primitive.git (es-to-primitive), git://github.com/ljharb/is-callable.git (is-callable), git://github.com/ljharb/is-date-object.git (is-date-object), git://github.com/inspect-js/is-symbol.git (is-symbol), git://github.com/es-shims/String.prototype.trimLeft.git (string.prototype.trimleft), git://github.com/es-shims/String.prototype.trimRight.git (string.prototype.trimright). This software contains the following license and notice below: The MIT License (MIT)