Nicovideo:GINZA-HTML5 autoset 1080p

If 1080p is available, set to 1080p automatically.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Nicovideo:GINZA-HTML5 autoset 1080p
// @namespace    https://twitter.com/tigerauge0
// @version      1.12
// @description  If 1080p is available, set to 1080p automatically.
// @author       HAC
// @match        http://www.nicovideo.jp/watch/*
// @match        https://www.nicovideo.jp/watch/*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let observer = new MutationObserver(function (mutationRecords, thisObserver) {
        // 最初に自動と低が表示されてから、後で.PlayerOptionDropdown-menuノード指定で変更が入り、細かい画質が追加される
        for(let mutationRecord of mutationRecords) {
            let dropdownMenuDomObj;
            // .PlayerOptionDropdown-menu
            dropdownMenuDomObj = mutationRecord.target;
            if(dropdownMenuDomObj.classList.contains('PlayerOptionDropdown-menu')) {
                if(dropdownMenuDomObj.childNodes.length) {
                    let dropdownItemTopDomObj = dropdownMenuDomObj.childNodes[0];
                    // .PlayerOptionDropdown-menu > .PlayerOptionDropdownItem
                    if(dropdownItemTopDomObj.classList.contains('PlayerOptionDropdownItem')) {
                        if(dropdownItemTopDomObj.childNodes.length) {
                            let dropdownItemTopInnerDomObj = dropdownItemTopDomObj.childNodes[0];
                            // .PlayerOptionDropdown-menu > .PlayerOptionDropdownItem > .PlayerOptionDropdownItem-inner
                            if(dropdownItemTopInnerDomObj.classList.contains('PlayerOptionDropdownItem-inner')) {
                                if(/1080p/.test(dropdownItemTopInnerDomObj.textContent)) {
                                    if(!dropdownItemTopDomObj.classList.contains('is-disabled')) {
                                        console.log("1080p is available.");
                                        dropdownItemTopInnerDomObj.click();
                                        let autoPlayDomObj = document.getElementById('AutoPlayMenuItem-on');
                                        if( autoPlayDomObj && autoPlayDomObj.checked === true){
                                            let videoContainerDomObj = document.getElementById('MainVideoPlayer');
                                            if(videoContainerDomObj.getElementsByTagName('video').length) {
                                                let videoDomObj = videoContainerDomObj.getElementsByTagName('video')[0];
                                                videoDomObj.addEventListener('loadedmetadata', function() {
                                                    console.log("Autoplay start.");
                                                    videoDomObj.play();
                                                });
                                            }
                                        }
                                    } else {
                                        console.log("1080p is not available because of low-quality mode.");
                                    }
                                } else {
                                    console.log("1080p doesn't exist.");
                                }
                                // 問題点: .PlayerOptionDropdownItem[0]が検出できなかった場合、Observeし続ける
                                thisObserver.disconnect();
                                break;
                            }
                        }
                    }
                }
            }
        }
    });
    observer.observe(document.getElementById('js-app'), { childList: true, subtree: true});
})();