3

I'm having serious trouble doing something very simple. I'd like to use a chrome extension content script to trigger the video in the following url to play:

http://sports24.club/watch?id=espn2

The following code triggers that video in the dev console:

 document.getElementsByClassName('player-poster')[0].click();

Or

$('.player-poster')[0].dispatchEvent(new MouseEvent("click"));

Or

document.getElementsByTagName("video")[0].play();

I've tried some other variations, including using the "trigger" method. They also work.

However, when placed in a content script, every variation simply throws an Uncaught (in promise) DOMException.

I've looked around, and the best advice I received was from this SO question. But even that didn't work.

I would really love if any of you could explain:

  1. Why is this happening? I get that content scripts live in their own little world, but why would that cause a .click() method to not work?

  2. How do I get around that and make this work?

Thanks!

MattyZ
  • 169
  • 1
  • 12
  • When things work in a console but don't in a content script, assuming the node is present at the time the content script runs, the solution is usually to put the code into a [page-level script element](https://stackoverflow.com/a/9517879). – wOxxOm Jan 04 '19 at 20:38
  • Hi @wOxxOm! I was hoping you'd respond (I've seen your responses in other posts). I actually had tried the following earlier, and that also didn't work. Is this what you meant by a page-level script element? $(" – MattyZ Jan 04 '19 at 20:54
  • I'm not sure jQuery-added script actually runs. Add a console.log to verify or use vanilla js. – wOxxOm Jan 04 '19 at 20:58
  • So here's the strange part: I added the log, and then reloaded the extension. When I reloaded the page, I opened up the dev console at the same time. Suddenly the video played, and the log also showed up in the dev console. However, when I reload the page without opening the dev console, the video does not play. When I open the dev console, the log does show, however. If I reload the page with the dev console already open, the video does not play, but the log shows. What the heck...? – MattyZ Jan 04 '19 at 21:04
  • Did you find an answer for this? – Wilson Silva May 25 '20 at 21:46

0 Answers0