Discussions » Development

How to apply this script to content subsequently loaded within the page?

§
Posted: 2017-02-09
Edited: 2017-02-09

How to apply this script to content subsequently loaded within the page?

I had a little success modifying a script meant to pause videos, to autoplay them instead. It works on pages where the video loads with the page, but fails on vk.com (where I want it to work) 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;
    }
}
§
Posted: 2017-04-02

Take a look at the Mutation Observer API, or use this jQuery plugin (provides fallbacks for unsupported browsers): https://github.com/kapetan/jquery-observe

Post reply

Sign in to post a reply.