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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);

})();