Nicovideo:GINZA-HTML5 autoset 1080p

If 1080p video exists, set video quality to 1080p automatically.

As of 2018-01-14. See the latest version.

// ==UserScript==
// @name         Nicovideo:GINZA-HTML5 autoset 1080p
// @namespace    https://twitter.com/tigerauge0
// @version      1.03
// @description  If 1080p video exists, set video quality to 1080p automatically.
// @author       HAC
// @match        http://www.nicovideo.jp/watch/*
// @grant        none
// @require      http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// ==/UserScript==

(function() {
    'use strict';

    let documentObserver = new MutationObserver(function (mutationRecords, observer) {
        //console.log('Obserbing');
        for(let mutationRecord of mutationRecords) {
            if(mutationRecord.target === $('.PlayerOptionDropdown-menu')[0]) {
                let resolutionListTopDomObj = $(mutationRecord.target).children('.PlayerOptionDropdownItem')[0];
                let resolutionListTopInnerDomObj = $(resolutionListTopDomObj).children('.PlayerOptionDropdownItem-inner')[0];
                if(/1080p/.test(resolutionListTopInnerDomObj.textContent)) {
                    if(!$(resolutionListTopDomObj).hasClass('is-disabled')) {
                        console.log("1080p is available.");
                        resolutionListTopInnerDomObj.click();
                        if($('#AutoPlayMenuItem-on').val() === 'true'){
                            $('.PlayerPlayButton > .ControllerButton-inner')[0].click();
                        }
                    } else {
                        console.log("1080p is not available because of low-quality mode.");
                    }
                } else {
                    console.log("1080p doesn't exist.");
                }
                // 問題点: .PlayerOptionDropdownItem[0]が検出できなかった場合、Observeし続ける
                observer.disconnect();
                break;
            }
        }
    });
    documentObserver.observe(document, { childList: true, subtree: true});
})();