niconico myvideo search

投稿動画から作者のMyvideoを探す

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           niconico myvideo search
// @namespace      http://web.zgo.jp/
// @description    投稿動画から作者のMyvideoを探す
// @include        http://www.nicovideo.jp/watch/*
// @version 0.0.1.20140518104259
// ==/UserScript==
(function() {
    var videoTitle = document.getElementsByClassName('video_title')[0];
    if (!videoTitle) return;
    var videoUserId = unsafeWindow.so.variables.videoUserId;// 投稿者のID
    var userVideoURL = "http://www.nicovideo.jp/user/" + videoUserId + "/video";
    window.gm_nicoUserId = videoUserId;
    // get→inssert
    getUserVideo(userVideoURL, insertLink);

    // ユーザー投稿動画一覧が公開されているか
    // 公開されているならcallbackを実行
    function getUserVideo(userVideoURL, callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: userVideoURL,
            headers: {
                'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey'
            },
            onload: function(responseDetails) {
                var DOMHTML = createDocumentFromString(responseDetails.responseText);
                if (isOpendVideo(DOMHTML)) {
                    callback();
                    return true;
                } else {
                    return false;
                }
            },
            onerror :function() {
                GM_log("通信エラー myvideo")
            }
        });

    }

    // class="noList" をみて公開か判定
    // 公開ならtrue,非公開ならfalse
    function isOpendVideo(doc) {
        var cntBody = doc.getElementById("myContBody");
        return !cntBody.classList.contains("noList");
    }

    // タイトルの横へ挿入
    function insertLink() {
        var span = document.createElement('span');
        span.style.cssText = 'font-size:small; margin-top:4px; line-height:1.25;';
        span.innerHTML = ' <a href="http://www.nicovideo.jp/user/' + videoUserId + '/video">公開投稿動画</a>';
        videoTitle.appendChild(span); // insert position
    }

    function createDocumentFromString(source) {
        var doc;
        try {
            doc = document.cloneNode(false);
            doc.appendChild(doc.importNode(document.documentElement, false));
        } catch(e) {
            doc = document.implementation.createHTMLDocument ?
                    document.implementation.createHTMLDocument('hogehoge') :
                    document.implementation.createDocument(null, 'html', null);
        }
        var range = document.createRange();
        range.selectNodeContents(document.documentElement);
        var fragment = range.createContextualFragment(source);
        var headChildNames = {title: true, meta: true, link: true, script: true, style: true, /*object: true,*/ base: true/*, isindex: true,*/};
        var child, head = doc.getElementsByTagName('head')[0] || doc.createElement('head'),
                body = doc.getElementsByTagName('body')[0] || doc.createElement('body');
        while ((child = fragment.firstChild)) {
            if (
                    (child.nodeType === doc.ELEMENT_NODE && !(child.nodeName.toLowerCase() in headChildNames)) ||
                            (child.nodeType === doc.TEXT_NODE && /\S/.test(child.nodeValue))
                    )
                break;
            head.appendChild(child);
        }
        body.appendChild(fragment);
        doc.documentElement.appendChild(head);
        doc.documentElement.appendChild(body);
        return doc;
    }
})();