Audio doesn't play in javascript - when alert is triggered

Viewed 27

I am basically trying to edit this extension (AlertForWords-1.0.0) so that if it finds a specific word, it will alert me and play a sound. But the problem is, it doesn't produce any sounds at all. Here is the code:

let text = document.body.innerText
let textToFind = ""
chrome.storage.sync.get({
    text: '',
    time: 1000
  }, (items) => {
    textToFind = items.text;
    let regexStr = "("
    textToFind.split("\n").forEach((word, index) => {
        if (index != textToFind.split("\n").length-1) {
            regexStr += word + "|"
        } else {
            regexStr += word
        }
    });
    regexStr += ")"

    let regex = new RegExp(regexStr, "gi")
    const mp3_url = 'https://media.geeksforgeeks.org/wp-content/uploads/20190531135120/beep.mp3';

    let src = 'https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3';
    let audio = new Audio(src); 
    setTimeout(() => {
        let found = text.match(regex)
        if (found) {
            //new Audio(mp3_url).play();

            audio.play();
            alert(`Found Word --`)
        }
    }, items.time)
    
});

Any suggestions are appreciated. I've been trying a few different things but nothing seems to work.

0 Answers
Related