I am trying to develop some JavaScript code to try and play audio automatically when a page loads. In order to do this successfully, I went into chrome://flags and set the #autoplay-policy to "no gesture is "required". When I did this, I was able to successfully play audio automatically from a regular JavaScript + HTML files. However, when I put this same code into my custom Chrome extension's content.js file, I get this error "Uncaught (in promise) DOMException", which is the same error I was receiving back before I disabled the Chrome flag in my regular JavaScript + HTML files.
const sound = new Audio()
function playSound() {
sound.src = 'audio/dragon.mp3';
sound.play();
}
setTimeout(function () {
playSound();
}, 2000)
This is the code that works on my regular JavaScript + HTML web page after I changed the #autoplay-policy to no gesture required. When I use this same code in my Chrome extension, it will not play audio automatically.
The overall question is whether or not Chrome flags have an effect on the policies of Chrome extensions? Or if there is something else that is not allowing my audio to play. Because I do not understand why disabling the #autoplay-policy allows my one website to autoplay an MP3 file, but the Chrome extension cannot.