1

I want to make a Firefox add-on that would loop playing a sound when the user opens a certain website (e.g. mozilla.org). I've read that Firefox blocks autoplay until user interacts with a site (clicks on sth etc) so I decided to make a loop that would try to play the sound over and over again so eventually when user activates the site the sound starts playing. It doesn't work and i have no idea why. Nothing shows up in the console (theoretically there should be information about blocked autoplay).

json:

{
  "manifest_version": 2,
  "name": "Borderify",
  "version": "1.0",

  "icons": {
    "48": "icons/border-48.png"
  },

  "content_scripts": [
    {
      "matches": [ "*://*.mozilla.org/*" ],
      "js": [ "borderify.js" ]
    }
  ]
}

js:

function sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

var audio = new Audio('airhorn.mp3');

while(true){
audio.play();
sleep(1000);
}
Leprawel
  • 21
  • 4
  • that while loop is just going to lock up the browser, that sleep is not going to do anything – epascarello Nov 04 '19 at 18:00
  • Does this answer your question? [HTML5 Audio Looping](https://stackoverflow.com/questions/3273552/html5-audio-looping) – Vl4dimyr Nov 04 '19 at 18:16
  • Why is sleep not doing anything, i created it based on https://stackoverflow.com/questions/951021/what-is-the-javascript-version-of-sleep – Leprawel Nov 04 '19 at 18:20
  • sleep is just pausing everything for the purpose of doing nothing. For example, if you want to simulate a typewriter, you can write every letters of a string with a sleep inbetween each prints. I'd try to simulate a click on the window to trigger your function instead of looping until user interacts. – pensum Nov 04 '19 at 18:22
  • if its pausing in the loop then why wouldnt the loop work – Leprawel Nov 04 '19 at 18:24
  • It is a promise, it is not doing anything. If you used `await` probably what answer has it would do something, but it still is not really sleeping. – epascarello Nov 04 '19 at 18:26
  • i guess i dont unterstand at all how this works – Leprawel Nov 04 '19 at 18:27

0 Answers0