0

I wrote a simple chrome extension that should display an alert message whenever a html5 video starts playing on a website being visited.

It's not doing so.

Here is the content.JS code:

var vid = document.getElementsByTagName('video');
vid.addEventListener("playing", function() {
    alert("A HTML5 Video is playing");
}

And here is the Manifest.JSON code:

{
  "manifest_version": 2,

  "name": "video playing alerter",
  "version": "0.1.0",
  "description": "video playing alert demo extension",

  "content_scripts": [{
    "js": ["content.js"],
    "matches": ["*://*/*"]
  }]

}
  • Are you sure the event name is `playing` and not `play`? or that it fires an event at all? Or that you have the right element captured? There's no way for us to verify anything for you. – James Gould Aug 02 '18 at 15:38
  • getElementsByTagName returs an array-like collection that doesn't have addEventListener method. Start using browser devtools: you should see an error in its console. As for a practical solution, you'll have to use MutationObserver and attach an event listener on each `video` element added (see a loosely [related example](https://stackoverflow.com/a/39334319)), or just re-run the code for example every 1 second using setInterval. – wOxxOm Aug 02 '18 at 15:42
  • @wOxxOm Like this? `setInterval(function(){ var vid = document.getElementsByTagName('video'); vid.addEventListener("playing", function() { alert("A HTML5 Video is playing"); }}, 1000);` – Virtual Tutor Aug 02 '18 at 20:00

0 Answers0