Skip to content

Commit

Permalink
fix(scanner-component): avoid direct usage of navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
odahcam committed Mar 14, 2018
1 parent a3202b9 commit 172561e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/app/modules/zxing-scanner/zxing-scanner.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class ZXingScannerComponent implements AfterViewInit, OnDestroy, OnChange
*/
private codeReader: BrowserQRCodeReader = new BrowserQRCodeReader(1500);

/**
* Says if some native API is supported.
*/
private isMediaDevicesSuported: boolean;

/**
* Says if some native API is supported.
*/
Expand Down Expand Up @@ -129,7 +134,8 @@ export class ZXingScannerComponent implements AfterViewInit, OnDestroy, OnChange
* Constructor to build the object and do some DI.
*/
constructor() {
this.isEnumerateDevicesSuported = !!(navigator.mediaDevices && navigator.mediaDevices.enumerateDevices);
this.isMediaDevicesSuported = !!(navigator && navigator.mediaDevices);
this.isEnumerateDevicesSuported = !!(this.isMediaDevicesSuported && navigator.mediaDevices.enumerateDevices);
}

/**
Expand Down Expand Up @@ -250,6 +256,11 @@ export class ZXingScannerComponent implements AfterViewInit, OnDestroy, OnChange
*/
askForPermission(): EventEmitter<boolean> {

if (!this.isMediaDevicesSuported) {
console.error('zxing-scanner', 'askForPermission', 'Can\'t get user media, this is not supported.');
return;
}

// Will try to ask for permission
navigator
.mediaDevices
Expand Down

0 comments on commit 172561e

Please sign in to comment.