LDRのsubs containerで最速動画ウォッチング

二つのスクリプトをあわせたら面白いんじゃないかと思ったので書いてみました。

// ==UserScript==
// @name           watch YouTube in subs container
// @namespace      http://shinten.info/
// @author         watta
// @include        http://reader.livedoor.com/*
// ==/UserScript==
//
// Original codes from id:antipop & id:trickstar_os. 
// - http://d.hatena.ne.jp/antipop/20060603/1149314071
// - http://d.hatena.ne.jp/trickstar_os/20070415/1176590537

(function() {
    var regex = new RegExp('^http://(?:www\\.)?youtube\\.com/(?:watch)?\\?.*v=([^&]+).*$', 'i');

    var frameOffset  = 0;
    var playerWidth  = '250';
    var playerHeight = '210';
    var playerId     = 'youtubeplayer';
    var autoPlay     = true;
    var w = (typeof unsafeWindow == "undefined") ? window : unsafeWindow;
    var _onload = w.onload;

    var onload = function () {
        with (w) {
            State.GM_now_watching = null;

            style_updater("subs_container", function () {
                var h = State.container_height - $("subs_tools").offsetHeight - frameOffset;
                setStyle(this, {
                    display : State.show_left ? "block": "none",
                    width   : State.leftpane_width + "px",
                    height  : h + "px"
                });
            });

            Keybind.add('Y', function () {
                var item    = get_active_item(true);
                var videoId = (item && item.link.match(regex));

                if (State.GM_now_watching) {
                    (videoId && videoId[1] != State.GM_now_watching)
                        ? changeVideo(videoId[1])
                        : hidePlayer();
                }
                else {
                    if (videoId) showPlayer(videoId[1]);
                }
            });

            function showPlayer(videoId) {
                var player = makePlayer(videoId);
                $('left_container').appendChild(player);

                frameOffset = 220;
                style_update('subs_container');

                State.GM_now_watching = videoId;
            }

            function hidePlayer() {
                $('left_container').removeChild($(playerId));

                frameOffset = 0;
                style_update('subs_container');

                State.GM_now_watching = null;
            }

            function changeVideo(videoId) {
                var player = makePlayer(videoId);
                $('left_container').replaceChild(player, $(playerId));

                State.GM_now_watching = videoId;
            }

            function makePlayer(videoId) {
                var videoSrc = [
                    'http://www.youtube.com/v/',
                    videoId,
                    autoPlay ? '&autoplay=1' : ''
                ].join('');

                return $N('div', {
                    'id': playerId
                    }, [
                        $N('object', {
                            'width' : playerWidth,
                            'height': playerHeight
                        }),
                        $N('param', {
                            'name'  : 'movie',
                            'value' : videoSrc
                        }),
                        $N('embed', {
                            'width'  : playerWidth,
                            'height' : playerHeight,
                            'type'   : 'application/x-shockwave-flash',
                            'src'    : videoSrc
                        })
                    ]
                );
            }
        }
    }

    w.onload = function () {
        _onload();
        onload();
    };
})();