[niconico video] Add the "Add My List" button to mylist page

Add the "Add My List" button to niconico video mylist page

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         [niconico video] Add the "Add My List" button to mylist page
// @name:ja      [ニコニコ動画] マイリストページからマイリストするボタンを追加
// @description  Add the "Add My List" button to niconico video mylist page
// @description:ja マイリストページからマイリストするボタンを追加
// @namespace    masshiro.blog
// @version      20200412
// @author       masshiro
// @license      MIT License
// @match        http://www.nicovideo.jp/mylist/*
// @match        https://www.nicovideo.jp/mylist/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    var addButtons = function () {
        var span = document.createElement('span');
        span.style='color:#F00;text-decoration:underline;cursor:pointer';
        span.innerHTML='追加';

        var a = document.createElement('a');
        a.className = 'addmylist';
        a.appendChild(span);

        a.addEventListener('click',function () {
            window.open('http://www.nicovideo.jp/mylist_add/video/' + encodeURIComponent(document.querySelectorAll('.SYS_box_item')[0].querySelectorAll('a')[0].getAttribute('href').replace('watch/','')), 'nicomylistadd', 'width=500, height=400, menubar=no, scrollbars=no');
        },false);

        Array.prototype.forEach.call(document.querySelectorAll('.SYS_box_item_buttons p'), function(item,i) {
            if(typeof item.querySelectorAll('a.addmylist')[0] === 'undefined'){
                var as = a.cloneNode(true);
                as.addEventListener('click',function () {
                    window.open('http://www.nicovideo.jp/mylist_add/video/' + encodeURIComponent(document.querySelectorAll('.SYS_box_item')[i].querySelectorAll('a')[0].getAttribute('href').replace('watch/','')), 'nicomylistadd', 'width=500, height=400, menubar=no, scrollbars=no');
                },false);
                item.appendChild(as);
            }
        });
    };
    
    var DOMObserverTimer = false;
    var DOMObserverConfig = {
        attributes: true,
        childList: true,
        subtree: true
    };
    var DOMObserver = new MutationObserver(function () {
        if (DOMObserverTimer !== 'false') {
            clearTimeout(DOMObserverTimer);
        }
        DOMObserverTimer = setTimeout(function () {
            DOMObserver.disconnect();
            addButtons();
            DOMObserver.observe(document.body, DOMObserverConfig);
        }, 100);
    });
    DOMObserver.observe(document.body, DOMObserverConfig);

})();