Discussões » Desenvolvimento

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

§
Publicado: 09/02/2017
Editado: 09/02/2017

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;
    }
}
§
Publicado: 02/04/2017

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

Publicar resposta

Faça o login para publicar uma resposta.