Change video quality with YouTube IFrame API

Viewed 628

Hello I am trying to make a JS code to insert in my Blog which makes all the videos be loaded in HD automatically.

I've been reviewing YouTube API IFrame but I can't fully understand the use especially for quality change.

So look for a Chrome extension "Improve YouTube" that does the function of changing the quality for all videos, and as it is open source check their github "https://github.com/ImprovedTube/YouTube-Extension" however I still can't get the script to work:

<script>

      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      var node;

playerQuality = function(node) {
    var quality = 'hd720';

    if (!node) {
        node = document.querySelector('.html5-video-player');
    }

    if (node.getAvailableQualityLevels) {
        var available_quality_levels = node.getAvailableQualityLevels();

        if (quality && quality !== 'auto') {
            if (available_quality_levels.indexOf(quality) === -1) {
                quality = available_quality_levels[0];
            }

            node.setPlaybackQualityRange(quality);
            node.setPlaybackQuality(quality);
        }
    }
};
</script>
0 Answers
Related