I have site settings set to websites can ask to use the camera. However, when I load the page, nothing happens. JavaScript doesn’t ask for permission to use the camera.
I got this code from a YouTube tutorial.
I made the changes you suggested, didn’t make a difference.
Chrome Site settings - Insecure origins treated as secure
Does this need to be enabled?
From what I have researched, Chrome will only allow the camera to be accessed by https and localhost for development. I’m using localhost.
document.addEventListener("DOMContentLoaded", () => {
let but = document.getElementById("but");
let video = document.getElementById("vid");
let mediaDevices = navigator.mediaDevices;
vid.muted = true;
but.addEventListener("click", () => {
// Accessing the user camera and video.
mediaDevices
.getUserMedia({
video: true,
audio: true,
})
.then((stream) => {
// Changing the source of video to current stream.
video.srcObject = stream;
video.addEventListener("loadedmetadata", () => {
video.play();
});
})
.catch(alert);
});
});
Usually, when a website ask permission, the website is listed under
Allowed to use your camera in Chrome settings
chrome://settings/content/camera
Then change the permission for the website. Refresh website.
For some reason, localhost is not asking for permission.
Sitepoint won’t allow localhost url in post. localhost is replaced by whatever project name is selected in localhost, so http:// followed by project name.
I’m not going to be much help on the localhost side of things as I’m only repeating what I’ve found out on the web.
This was something that was offered to try:
2. Clear Previous Permissions
Chrome might have blocked camera access previously and remembered that choice:
* Go to: `chrome://settings/content/camera`
* Under **"Not allowed to use your camera"**, remove any entries for `localhost`
* Also, visit your localhost URL and click the 🔒 icon → **"Site settings"**
* Set **Camera** to **Allow**
* Reload the page
The JavaScript is executing, however I checked the console in web developer
Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received
Hope this indicates what/where the problem is/lies
CacheStore.js:18 Cache set failed: ReferenceError: caches is not defined at CacheStore.set (CacheStore.js:18:93)
at CacheStore.setWithTTL (CacheStore.js:18:351)
at async GenAIWebpageEligibilityService.getExplicitBlockList (GenAIWebpageEligibil…yService.js:18:1290)
at async GenAIWebpageEligibilityService._shouldShowTouchpoints (GenAIWebpageEligibil…yService.js:18:1595)
at async GenAIWebpageEligibilityService.shouldShowTouchpoints (GenAIWebpageEligibil…yService.js:18:3140)
at async ActionableCoachmark.isEligible (ActionableCoachmark.js:18:2503)
at async ShowOneChild.getRenderPrompt (ShowOneChild.js:18:1750)
at async ShowOneChild.render (ShowOneChild.js:18:1961)
I believe that’s from an extension and not related to your problem but I think we need to wait for help from @m_hutley or one of the other JS gurus here.
What happens if you add the code for webcam start that was suggested above.
Console says:
Cache set failed: ReferenceError: caches is not defined
at CacheStore.set (CacheStore.js:18:93)
at CacheStore.setWithTTL (CacheStore.js:18:351)
at async GenAIWebpageEligibilityService.getExplicitBlockList (GenAIWebpageEligibilityService.js:18:1290)
at async GenAIWebpageEligibilityService._shouldShowTouchpoints (GenAIWebpageEligibilityService.js:18:1595)
at async GenAIWebpageEligibilityService.shouldShowTouchpoints (GenAIWebpageEligibilityService.js:18:3140)
at async ActionableCoachmark.isEligible (ActionableCoachmark.js:18:2503)
at async ShowOneChild.getRenderPrompt (ShowOneChild.js:18:1750)
at async ShowOneChild.render (ShowOneChild.js:18:1961)