Home > Blockchain >  How to find class by property
How to find class by property

Time:09-20

I'm doing some manipulations at the 'video' class at youtube, like change the currentTime property and read duration property of that class and I had success doing it. But when I go to other sites, sometimes they change the name of 'video' to 'video2' for example, and my code doesn't work in that site. I want to know if there is a easy way to make my code look for classes that have the currentTime property, and than set this as my variable, for example.

what I can do:

    var videoClass = document.getElementsByTagName('video');

what I want to do:

    var videoClass = document.getClassesByProperty('currentTime');

CodePudding user response:

I'm guessing you mean attribute, not property, e.g. something like <video2 currentTime="xxx">. If so, you can use a query selector to match the attribute.

var videoClass = document.querySelectorAll('[currentTime]');

CodePudding user response:

My temporary and probably permanent solution:

try{
    var videoClass = document.querySelectorAll('[class^=video-stream');
}catch{}
function setVideoClass(){
    try{
        var videoList = document.querySelectorAll('*');
        for (var i = 0; i < videoList.length; i  ){
            try{
                var videoClassLocal = videoList[i].currentTime;
                if ((videoClassLocal != undefined ) & (videoClassLocal != 0) ){
                    videoClass = videoList[i];
                }
            }catch{}
        }
    }catch{}
}
setInterval(setVideoClass,1000);
  • Related