@Suzie I have done a study, but probably need some helps from you and other expert to really make it happen.
The logic is as below:
-
Use custom code to trigger the front camera to scan the QR Code instead of building a custom block carrying the scanner function.
-
Opening the QR Code, which is the designated page for record update, in Chrome Browser
-
Automatically press the button (the only button on screen), and trigger update function in modal page. Then, press the âconfirmâ button automatically to finish updating the hidden input.
-
Aftering showing the designated parent page for 5 seconds, the chrome tab closes and trigger the camera again to complete the loop.
I am using the ChatGPT to work out the code below, but not really working, probably, there is something need to be adjusted and hope that those expert can have a look and realize this workaround with only a simple set of custom code for a button.
document.addEventListener('DOMContentLoaded', function() {
// Step 1: Triggering the frontend camera to scan QR Code
function startQRScan() {
navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } })
.then(function (stream) {
var videoElement = document.querySelector('video');
videoElement.srcObject = stream;
videoElement.play();
scanQRCode();
}).catch(function (err) {
console.error(err);
});
}
function scanQRCode() {
// QR Code scanning
const video = document.createElement('video');
const canvasElement = document.getElementById('canvas');
const canvas = canvasElement.getContext('2d');
let qrCodeId;
let scanning = true;
function drawLine(begin, end, color) {
canvas.beginPath();
canvas.moveTo(begin.x, begin.y);
canvas.lineTo(end.x, end.y);
canvas.lineWidth = 4;
canvas.strokeStyle = color;
canvas.stroke();
}
navigator.mediaDevices.getUserMedia({ video: { facingMode: "environment" } })
.then(function (stream) {
video.srcObject = stream;
video.setAttribute('playsinline', true);
video.play();
checkQrCode();
})
.catch(function (err) {
console.error(err);
});
function checkQrCode() {
if (scanning) {
canvasElement.hidden = false;
canvasElement.height = video.videoHeight;
canvasElement.width = video.videoWidth;
canvas.drawImage(
video,
0,
0,
canvasElement.width,
canvasElement.height
);
const imageData = canvas.getImageData(
0,
0,
canvasElement.width,
canvasElement.height
);
const code = jsQR(imageData.data, imageData.width, imageData.height, {
inversionAttempts: 'dontInvert',
});
if (code && code.data !== qrCodeId) {
qrCodeId = code.data;
console.log('QR Code found: ' + qrCodeId);
stopQRScan();
openUrlInChrome(qrCodeId);
} else {
requestAnimationFrame(checkQrCode);
}
}
}
function stopQRScan() {
scanning = false;
}
function openUrlInChrome(qrCodeId) {
const url = qrCodeId;
window.open(url, '_blank');
}
}
// Step 2: Opening the QR code in Chrome browser
function openQRCodeInChrome() {
startQRScan();
}
// Step 3 and Step 4: Automatically pressing the button to open modal and confirming it
function openAndConfirmModal() {
const button = document.querySelector('.MuiButton-containedPrimary');
if (button) {
button.click();
setTimeout(function () {
const modal = document.querySelector('.modal');
if (modal) {
modal.querySelector('.css-1ozfax3').click();
setTimeout(function () {
location.reload();
}, 5000);
}
}, 1000);
}
}
// Invoke the above functions in sequence
openQRCodeInChrome();
setTimeout(openAndConfirmModal, 15000);
});
In the designated page, the following code need to be put on custom code header:
<script>
function openAndConfirmModal() {
const button = document.querySelector('.MuiButton-containedPrimary');
if (button) {
button.click();
setTimeout(function () {
const modal = document.querySelector('.modal');
if (modal) {
modal.querySelector('.css-1ozfax3').click();
setTimeout(function () {
location.reload();
}, 5000);
}
}, 1000);
}
}
setTimeout(openAndConfirmModal, 15000);
</script>
Hope to be able to achieve the automation process with simple coding in two pages.