地球ウォーカー2

Scala, Python の勉強日記

YouTubeをリピート再生するGreasemonkey (&Bookmarklet)

既にあるかもしれないけど作ってみた。
とりあえずdocument.getElementById('movie_player')さえすればYouTube JavaScript Player APIを使えるということがわかった。

機能

  • ビデオの再生が終わったら勝手にリピート再生する。
  • 右上のボタン(AutoPagerizeのパクリ)でリピート再生のON/OFFを切り替えられる。

Greasemonkey

スクリプトをサーバーにおいた。
Youtube Repeat
ソースは以下の通り。

// ==UserScript==
// @name Youtube Repeat
// @description Repeat Youtube playing
// @namespace http://d.hatena.ne.jp/hysa/
// @include http://www.youtube.com/*
// ==/UserScript==
(function() {
  var p = document.getElementById('movie_player').wrappedJSObject;
  if (p === null) return;

  var on = GM_getValue('repeat', true);

  setInterval(
    /**
     * Repeat the video.
     * if the video is ended, play the video again.
     *
     * @param {number} state the state of the player.
     */
    function repeat() {
      if (p.getPlayerState() === State.ENDED && on) {
        p.playVideo();
      }
    }

  , 1000);

  /**
   * Enum for the state of the player 
   *
   * @enum {number}
   */
  var State = {

    UNSTARTED: -1,

    ENDED: 0,

    PLAYING: 1,

    PAUSED: 2,

    BUFFERING: 3,

    VIDEO_CUED: 5
  };


  /**
   * Create a button.
   *
   */
  (function createButton() {
    var button = document.createElement('div');
    var style = button.style;
    
    /**
     * Set style on the button.
     */
    function setStyle() {
      style.background = 'none repeat scroll 0 0 #0F0';
      style.color = '#FFF';
      style.fontSize = '12px';
      style.height = '10px';
      style.position = 'fixed';
      style.right = '3px';
      style.top = '3px';
      style.width = '10px';
      style.zIndex = '255';
      if (!on) {
        style.backgroundColor = '#CCC';
      }
    }

    button.addEventListener(
      'click',

      /**
       * Toggle switch.
       *
       */
      function() {
        if (on) {
          style.backgroundColor = '#CCC';
        } else {
          style.backgroundColor = '#0F0';
        }
        on = !on;
        GM_setValue('repeat', on);
      },
      false
    );

    setStyle();
    document.body.appendChild(button);
  })();

})();

Bookmarklet

IE8、FirefoxGoogle Chromeのみ確認。

javascript:(function(s){ s.src = 'http://hysa.main.jp/public/gm/YoutubeRepeat.js';document.body.appendChild(s);})(document.createElement('script'));