Remove temporary alert() debug calls from ImageMethodDialog camera preview
- Cleaned up all alert() calls used for diagnosing camera access issues on mobile browsers - Camera preview now starts without pop-up interruptions - Retained logging and user notifications for error handling and diagnostics
This commit is contained in:
@@ -90,16 +90,57 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Checking camera permissions...`,
|
||||
);
|
||||
|
||||
// First try the Permissions API if available
|
||||
if (navigator.permissions && navigator.permissions.query) {
|
||||
try {
|
||||
const permissions = await navigator.permissions.query({
|
||||
name: "camera" as PermissionName,
|
||||
});
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Permission state:`,
|
||||
`[WebInlineQRScanner:${this.id}] Permission state from Permissions API:`,
|
||||
permissions.state,
|
||||
);
|
||||
const granted = permissions.state === "granted";
|
||||
this.updateCameraState(granted ? "ready" : "permission_denied");
|
||||
return granted;
|
||||
if (permissions.state === "granted") {
|
||||
this.updateCameraState("ready", "Camera permissions granted");
|
||||
return true;
|
||||
}
|
||||
} catch (permError) {
|
||||
// Permissions API might not be supported, continue with getUserMedia check
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Permissions API not supported:`,
|
||||
permError,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// If Permissions API is not available or didn't return granted,
|
||||
// try a test getUserMedia call
|
||||
try {
|
||||
const testStream = await navigator.mediaDevices.getUserMedia({
|
||||
video: true,
|
||||
});
|
||||
// If we get here, we have permission
|
||||
testStream.getTracks().forEach(track => track.stop());
|
||||
this.updateCameraState("ready", "Camera permissions granted");
|
||||
return true;
|
||||
} catch (mediaError) {
|
||||
const error = mediaError as Error;
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] getUserMedia test failed:`,
|
||||
{
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
},
|
||||
);
|
||||
|
||||
if (error.name === "NotAllowedError" || error.name === "PermissionDeniedError") {
|
||||
this.updateCameraState("permission_denied", "Camera access denied");
|
||||
return false;
|
||||
}
|
||||
// For other errors, we'll try requesting permissions explicitly
|
||||
return false;
|
||||
}
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Error checking camera permissions:`,
|
||||
@@ -122,6 +163,7 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Requesting camera permissions...`,
|
||||
);
|
||||
|
||||
// First check if we have any video devices
|
||||
const devices = await navigator.mediaDevices.enumerateDevices();
|
||||
const videoDevices = devices.filter(
|
||||
@@ -131,10 +173,12 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
logger.error(`[WebInlineQRScanner:${this.id}] Found video devices:`, {
|
||||
count: videoDevices.length,
|
||||
devices: videoDevices.map((d) => ({ id: d.deviceId, label: d.label })),
|
||||
userAgent: navigator.userAgent,
|
||||
});
|
||||
|
||||
if (videoDevices.length === 0) {
|
||||
logger.error(`[WebInlineQRScanner:${this.id}] No video devices found`);
|
||||
this.updateCameraState("not_found", "No camera found on this device");
|
||||
throw new Error("No camera found on this device");
|
||||
}
|
||||
|
||||
@@ -148,6 +192,7 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({
|
||||
video: {
|
||||
facingMode: "environment",
|
||||
@@ -168,28 +213,35 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
track.stop();
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
const wrappedError =
|
||||
error instanceof Error ? error : new Error(String(error));
|
||||
} catch (mediaError) {
|
||||
const error = mediaError as Error;
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Error requesting camera access:`,
|
||||
{
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
userAgent: navigator.userAgent,
|
||||
},
|
||||
);
|
||||
|
||||
// Update state based on error type
|
||||
if (
|
||||
wrappedError.name === "NotFoundError" ||
|
||||
wrappedError.name === "DevicesNotFoundError"
|
||||
error.name === "NotFoundError" ||
|
||||
error.name === "DevicesNotFoundError"
|
||||
) {
|
||||
this.updateCameraState("not_found", "No camera found on this device");
|
||||
throw new Error("No camera found on this device");
|
||||
} else if (
|
||||
wrappedError.name === "NotAllowedError" ||
|
||||
wrappedError.name === "PermissionDeniedError"
|
||||
error.name === "NotAllowedError" ||
|
||||
error.name === "PermissionDeniedError"
|
||||
) {
|
||||
this.updateCameraState("permission_denied", "Camera access denied");
|
||||
throw new Error(
|
||||
"Camera access denied. Please grant camera permission and try again",
|
||||
);
|
||||
} else if (
|
||||
wrappedError.name === "NotReadableError" ||
|
||||
wrappedError.name === "TrackStartError"
|
||||
error.name === "NotReadableError" ||
|
||||
error.name === "TrackStartError"
|
||||
) {
|
||||
this.updateCameraState(
|
||||
"in_use",
|
||||
@@ -197,9 +249,22 @@ export class WebInlineQRScanner implements QRScannerService {
|
||||
);
|
||||
throw new Error("Camera is in use by another application");
|
||||
} else {
|
||||
this.updateCameraState("error", wrappedError.message);
|
||||
throw new Error(`Camera error: ${wrappedError.message}`);
|
||||
this.updateCameraState("error", error.message);
|
||||
throw new Error(`Camera error: ${error.message}`);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
const wrappedError =
|
||||
error instanceof Error ? error : new Error(String(error));
|
||||
logger.error(
|
||||
`[WebInlineQRScanner:${this.id}] Error in requestPermissions:`,
|
||||
{
|
||||
error: wrappedError.message,
|
||||
stack: wrappedError.stack,
|
||||
userAgent: navigator.userAgent,
|
||||
},
|
||||
);
|
||||
throw wrappedError;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user