Discussions » Creation Requests
Enable html5 autoplay for a site?
Alright, I had a little success modifying a script meant to pause videos. It works on pages where the video loads with the page, but fails on VK because videos are loaded within the page depending on the link clicked.
Any advice to make it work on dynamically added videos would be appreciated.
// ==UserScript== // @name Autoplay Native Chrome html5-player VK // @include https://vk.com/* // @grant none // ==/UserScript== var videos = document.getElementsByTagName('video'); window.addEventListener('load', startVideo, false); function startVideo() { for (var i=0; i<videos.length; i++) { videos[i].play(); video.currentTime = 0; } }
Appreciate the response Jason. I was pretty sure the solution would be a MutationObserver. I have quite a few scripts that use them as it is. They're all modified versions of scripts @wOxxOm helped me with using his.
I've had some luck using it for all types of scripts for speed, but I haven't gotten it to work with this one at all. Hopefully someone can clue me in.
A mutation observer shouldn't be necessary here. Your startVideo function is passed the event as an argument, and you can use that to access the event's target, i.e. the video that loaded.
You would be looking at something like the following (but I haven't tested it, so it might need some changes).
// ==UserScript== // @name Autoplay Native Chrome html5-player VK // @include https://vk.com/* // @grant none // ==/UserScript== window.addEventListener('canplay', startVideo, false); function startVideo(ev) { var video = ev.target; video.currentTime = 0; video.play(); }
If the videos still don't play, it is likely that the server isn't trying to load them in the browser at all yet, in which case there isn't really anything you can do.
Appreciate the response. Unfortunately, that script didn't work. I do think a mutation observer is probably the way to go. I have some scripts which use them, and I'm familiar with making small changes to accomplish different objectives, but the modifications to make them work with this script are over my head.
Enable html5 autoplay for a site?
I see a few scripts that do the opposite, but none which enable it. The site is vk.com, but their html5-player sucks so I forced the use of the native Chrome player. How could I get the native Chrome html5-player to autoplay when it loads?