Files
peworkshop/components/scanner-polyfill.js

84 lines
2.5 KiB
JavaScript

// This is a helper function to ensure QR scanner compatibility
export const ensureScannerCompatibility = () => {
// Add basic polyfills for methods that might be missing
if (typeof Function.prototype.bind !== "function") {
Function.prototype.bind = function (context) {
const fn = this;
return function () {
return fn.apply(context, arguments);
};
};
}
// Ensure MediaDevices API is available
if (typeof window !== "undefined") {
if (!navigator.mediaDevices) {
navigator.mediaDevices = {};
}
// Polyfill for older browsers that don't implement getUserMedia
if (!navigator.mediaDevices.getUserMedia) {
navigator.mediaDevices.getUserMedia = function (constraints) {
const getUserMedia =
navigator.webkitGetUserMedia || navigator.mozGetUserMedia;
// Return a rejected promise if getUserMedia is not implemented
if (!getUserMedia) {
return Promise.reject(
new Error("getUserMedia is not implemented in this browser")
);
}
// Otherwise, wrap the call to the old navigator.getUserMedia with a Promise
return new Promise(function (resolve, reject) {
getUserMedia.call(navigator, constraints, resolve, reject);
});
};
}
// Ensure console methods exist for debugging
if (!window.console) window.console = {};
const methods = ["log", "error", "warn", "info"];
methods.forEach((method) => {
if (!window.console[method]) {
window.console[method] = function () {};
}
});
}
return true;
};
// Helper function to check if camera is available and accessible
export const checkCameraAvailability = async () => {
try {
if (!navigator.mediaDevices || !navigator.mediaDevices.getUserMedia) {
return { available: false, error: "MediaDevices API not supported" };
}
// Try to access the camera with explicit video request
const stream = await navigator.mediaDevices.getUserMedia({
video: true,
audio: false,
});
// If we get here, camera is available - stop the stream
if (stream) {
stream.getTracks().forEach((track) => track.stop());
}
return { available: true };
} catch (error) {
return {
available: false,
error: error.message,
name: error.name,
permission:
error.name === "NotAllowedError" ||
error.name === "PermissionDeniedError"
? "denied"
: "unknown",
};
}
};