Show
Ignore:
Timestamp:
12/03/08 19:08:23 (5 weeks ago)
Author:
anekos
Message:

フルスクリーン化などの追加

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/stella.js

    r25172 r25781  
    33// @description-ja ステータスラインに動画の再生時間などを表示する。 
    44// @license        Creative Commons Attribution-Share Alike 3.0 Unported 
    5 // @version        0.06 
     5// @version        0.07 
    66// @author         anekos (anekos@snca.net) 
    77// @minVersion     2.0pre 
     
    1313// 
    1414// TODO 
    15 //    user command 
    16 //    :fetchvideo 
    1715//    Icons 
    1816//    Other video hosting websites 
     17//    auto fullscreen 
    1918// 
    20 // Links: 
     19// Link: 
    2120//    http://d.hatena.ne.jp/nokturnalmortum/ 
     21// 
     22// Refs: 
     23//    http://yuichis.homeip.net/nicodai.user.html 
     24//    http://coderepos.org/share/browser/lang/javascript/vimperator-plugins/trunk/nicontroller.js 
     25//    http://coderepos.org/share/browser/lang/javascript/vimperator-plugins/trunk/youtubeamp.js 
     26//    Thanks! 
    2227// 
    2328// License: 
     
    3338  const ID_PREFIX = 'anekos-stela-'; 
    3439  const InVimperator = !!(liberator && modules && modules.liberator); 
     40  const DOUBLE_CLICK_INTERVAL = 300; 
    3541 
    3642  // }}} 
     
    121127              : '??:??'); 
    122128 
    123   function setWithBackup (target, values) { 
    124     let backup = target.__stella_backup = {}; 
     129  function storeStyle (target, values) { 
     130    let [style, cstyle] = [target.style, content.getComputedStyle(target, '')]; 
     131    let backup = style.__stella_backup = {}; 
    125132    for (let [name, value] in Iterator(values)) { 
    126       backup[name] = target[name]; 
    127       target[name] = value; 
     133      backup[name] = cstyle[name]; 
     134      style[name] = value; 
    128135    } 
    129     liberator.log(target.__stella_backup) 
    130   } 
    131  
    132   function restoreFromBackup (target, doDelete) { 
    133     if (!target.__stella_backup) 
     136  } 
     137 
     138  function restoreStyle (target, doDelete) { 
     139    let style = target.style; 
     140    if (!style.__stella_backup) 
    134141      return; 
    135     for (let [name, value] in Iterator(target.__stella_backup)) 
    136       target[name] = value; 
     142    let backup = style.__stella_backup; 
     143    for (let name in Iterator(backup)) 
     144      style[name] = backup[name]; 
    137145    if (doDelete) 
    138       delete target.__stella_backup; 
     146      delete style.__stella_backup; 
     147  } 
     148 
     149  function getElementByIdEx (id) 
     150    let (p = content.document.getElementById(id)) 
     151      (p && (p.wrappedJSObject || p)); 
     152 
     153  function fixDoubleClick (obj, click, dblClick) { 
     154    let clicked = 0; 
     155    let original = {click: obj[click], dblClick: obj[dblClick]}; 
     156    liberator.log(original); 
     157    obj[click] = function () { 
     158      let self = this, args = arguments; 
     159      let _clicked = ++clicked; 
     160      setTimeout(function () { 
     161        if (_clicked == clicked--) 
     162          original.click.apply(self, args); 
     163        else 
     164          clicked = 0; 
     165      }, DOUBLE_CLICK_INTERVAL); 
     166    }; 
     167    obj[dblClick] = function () { 
     168      clicked = 0; 
     169      original.dblClick.apply(this, arguments); 
     170    }; 
    139171  } 
    140172 
     
    194226    initialize: function () void null, 
    195227 
     228    finalize: function () { 
     229      // 念のためフルスクリーンは解除しておく 
     230      if (this.has('fullscreen', 'rwt') && this.isValid && this.fullscreen) 
     231        this.fullscreen = false; 
     232    }, 
     233 
    196234    is: function (state) (this.state == state), 
    197235 
     
    223261    get statusText () this.timeCodes, 
    224262 
     263    get storage () 
     264      (content.document.__stella_storage || (content.document.__stella_storage = {})), 
     265 
    225266    get timeCodes () (toTimeCode(this.currentTime) + '/' + toTimeCode(this.totalTime)), 
    226267 
    227268    get title () undefined, 
     269 
     270    get isValid () (~buffer.URL.indexOf('http://www.nicovideo.jp/watch/')), 
    228271 
    229272    get volume () undefined, 
     
    318361      if (this.fullscreen) { 
    319362        liberator.log('full') 
    320         setWithBackup(this.player.style, { 
     363        storeStyle(this.player, { 
    321364          position: 'fixed', 
    322365          left: '0px', 
     
    327370      } else { 
    328371        liberator.log('normal') 
    329         restoreFromBackup(this.player.style); 
     372        restoreStyle(this.player); 
    330373      } 
    331374    }, 
     
    359402    get totalTime () parseInt(this.player.getDuration()), 
    360403 
     404    get isValid () buffer.URL.match(/^http:\/\/(?:[^.]+\.)?youtube\.com\/watch/), 
     405 
    361406    get volume () parseInt(this.player.getVolume()), 
    362407    set volume (value) (this.player.setVolume(value), value), 
     
    377422  } 
    378423 
     424  // Normal / Fullscreen 
     425  NicoPlayer.Variables = [ 
     426    ['videowindow._xscale',                     100,   null], 
     427    ['videowindow._yscale',                     100,   null], 
     428    ['videowindow._x',                            6,      0], 
     429    ['videowindow._y',                           65,      0], 
     430    ['controller._x',                             6,  -1000], 
     431    ['inputArea._x',                              4,  -1000], 
     432    ['controller._visible',                       1,      1], 
     433    ['inputArea._visible',                        1,      1], 
     434    ['waku._visible',                             1,      0], 
     435    ['tabmenu._visible',                          1,      0], 
     436    ['videowindow.video_mc.video.smoothing',   null,      1], 
     437    ['videowindow.video_mc.video.deblocking',  null,      5] 
     438  ]; 
     439 
    379440  NicoPlayer.prototype = { 
    380441    __proto__: Player.prototype, 
    381442 
    382443    functions: { 
     444      comment: 'rwt', 
    383445      currentTime: 'rw', 
     446      fetch: 'x', 
     447      fileURL: '', 
     448      fullscreen: 'rwt', 
     449      id: 'r', 
     450      muted: 'rwt', 
     451      pause: 'x', 
     452      play: 'x', 
     453      playEx: 'x', 
     454      playOrPause: 'x', 
     455      repeating: 'rwt', 
     456      title: 'r', 
    384457      totalTime: 'r', 
    385       volume: 'rw', 
    386       play: 'x', 
    387       playOrPause: 'x', 
    388       playEx: 'x', 
    389       pause: 'x', 
    390       muted: 'rwt', 
    391       repeating: 'rwt', 
    392       comment: 'rwt', 
    393       title: 'r', 
    394       fileURL: '', 
    395       id: 'r', 
    396       fetch: 'x', 
    397       title: 'r' 
     458      volume: 'rw' 
    398459    }, 
    399460 
     
    403464    set comment (value) (this.player.ext_setCommentVisible(value), value), 
    404465 
     466    get playerContainer () getElementByIdEx('flvplayer_container'), 
     467 
    405468    get currentTime () parseInt(this.player.ext_getPlayheadTime()), 
    406469    set currentTime (value) (this.player.ext_setPlayheadTime(value), value), 
    407470 
    408471    get fileExtension () '.flv', 
     472 
     473    get fullscreen () !!this.storage.fullscreen, 
     474    set fullscreen (value) { 
     475      let self = this; 
     476      value = !!value; 
     477 
     478      if (this.storage.fullscreen === value) 
     479        return; 
     480 
     481      this.storage.fullscreen = value; 
     482 
     483      let variablesSetter = function () { 
     484        NicoPlayer.Variables.forEach(function ([name, normal, full]) { 
     485          let v = value ? full : normal; 
     486          if (v !== null) 
     487            self.player.SetVariable(name, v); 
     488        }); 
     489      }; 
     490 
     491      let doc = content.document.wrappedJSObject; 
     492      let win = content.wrappedJSObject; 
     493      let player = getElementByIdEx('flvplayer'); 
     494 
     495      win.toggleMaximizePlayer(); 
     496 
     497      if(value)  { 
     498        let f = function () { 
     499          let viewer = {w: 544, h: 384}; 
     500          let screen = { 
     501            w: content.innerWidth, 
     502            h: content.innerHeight 
     503          }; 
     504          let scale = { 
     505            w: Math.max(1, screen.w / viewer.w), 
     506            h: Math.max(1, screen.h / viewer.h) 
     507          }; 
     508          scale.v = Math.min(scale.w, scale.h); 
     509          storeStyle(doc.body, { 
     510            backgroundImage: 'url()', 
     511            backgroundRepeat: '', 
     512            backgroundColor: 'black' 
     513          }); 
     514          player.SetVariable('videowindow.video_mc.video.smoothing' , 1); 
     515          player.SetVariable('videowindow.video_mc.video.deblocking', 5); 
     516          storeStyle( 
     517            player, 
     518            (scale.w >= scale.h) ? { 
     519              width:       Math.floor(viewer.w * scale.h) + 'px', 
     520              height:      screen.h + 'px', 
     521              marginLeft:  ((screen.w - viewer.w * scale.h) / 2) + 'px', 
     522              marginTop:   '0px' 
     523            } : { 
     524              width:       screen.w + 'px', 
     525              height:      Math.floor(viewer.h * scale.w) + 'px', 
     526              marginLeft:  '0px', 
     527              marginTop:   ((screen.h - viewer.h * scale.w) / 2) + 'px' 
     528            } 
     529          ); 
     530          player.SetVariable('videowindow._xscale', 100 * scale.v); 
     531          player.SetVariable('videowindow._yscale', 100 * scale.v); 
     532          variablesSetter(); 
     533        }; 
     534        f(); 
     535        win.onresize = function () 
     536          (InVimperator && liberator.mode === modes.COMMAND_LINE) || setTimeout(f, 1000); 
     537      } else { 
     538        restoreStyle(doc.body); 
     539        //restoreStyle(player); 
     540        player.style.marginLeft = ''; 
     541        player.style.marginTop  = ''; 
     542        variablesSetter(); 
     543        delete win.onresize; 
     544      } 
     545      win.scrollTo(0, 0); 
     546    }, 
    409547 
    410548    get id () 
     
    415553    set muted (value) (this.player.ext_setMute(value), value), 
    416554 
    417     get player () 
    418       let (p = content.document.getElementById('flvplayer')) 
    419         (p && (p.wrappedJSObject || p)), 
     555    get player () getElementByIdEx('flvplayer'), 
    420556 
    421557    get repeating () this.player.ext_isRepeat(), 
     
    444580 
    445581    fetch: function (filepath) { 
    446       liberator.log(this.id) 
    447582      let onComplete = function (xhr) { 
    448583          let res = xhr.responseText; 
     
    483618    'comment', 
    484619    'repeat', 
     620    'fullscreen', 
    485621    { 
    486622      name: 'volume-root', 
     
    593729      this.disable(); 
    594730      this.progressListener.uninstall(); 
     731      for each (let player in this.players) 
     732        player.finalize(); 
    595733      window.removeEventListener('resize', this.__onResize, false); 
    596734    }, 
     
    599737    set hidden (v) (this.panel.hidden = v), 
    600738 
    601     get valid () (this.where), 
     739    get isValid () (this.where), 
    602740 
    603741    get player () this.players[this.where], 
     
    608746    set statusBarVisible (value) (this.statusBar.setAttribute('moz-collapsed', !value), value), 
    609747 
    610     get where () ( 
    611       (~buffer.URL.indexOf('http://www.nicovideo.jp/watch/') && 'niconico') 
    612       || 
    613       (buffer.URL.match(/^http:\/\/(?:[^.]+\.)?youtube\.com\/watch/) && 'youtube') 
    614     ), 
    615  
     748    get where () { 
     749      for (let [name, player] in Iterator(this.players)) 
     750        if (player.isValid) 
     751          return name; 
     752    }, 
    616753 
    617754    addUserCommands: function () { 
     
    624761            ? funcS 
    625762            : function (arg, bang) { 
    626                 if (!stella.valid) 
     763                if (!stella.isValid) 
    627764                  raise('Stella: Current page is not supported'); 
    628765                let p = stella.player; 
     
    685822      icon.style.marginRight = '4px'; 
    686823      setClickEvent('icon', icon); 
     824      icon.addEventListener('dblclick', bindr(this, this.onIconDblClick), false); 
    687825 
    688826      let labels = this.labels = {}; 
     
    760898    }, 
    761899 
     900    onFullscreenClick: function () this.player.toggle('fullscreen'), 
     901 
    762902    onIconClick: function () this.player.playOrPause(), 
    763903 
     904    onIconDblClick: function () this.player.toggle('fullscreen'), 
     905 
    764906    onLocationChange: function () { 
    765       if (this.__valid !== this.valid) { 
    766         (this.__valid = this.valid) ? this.enable() : this.disable(); 
     907      if (this.__valid !== this.isValid) { 
     908        (this.__valid = this.isValid) ? this.enable() : this.disable(); 
    767909      } 
    768910    }, 
     
    780922    }, 
    781923 
    782     onMutedClick: function (event) (this.player.toggle('muted')), 
     924    onMutedClick: function (event) this.player.toggle('muted'), 
    783925 
    784926    onPauseClick: function () this.player.pause(), 
     
    786928    onPlayClick: function () this.player.play(), 
    787929 
    788     onRepeatingClick: function () (this.player.toggle('repeating')), 
     930    onRepeatingClick: function () this.player.toggle('repeating'), 
    789931 
    790932    onResize: function () { 
     
    795937    }, 
    796938 
    797     onSetMutedClick: function (event) (this.player.volume = event.target.getAttribute('volume')) 
     939    onSetVolumeClick: function (event) (this.player.volume = event.target.getAttribute('volume')) 
    798940  }; 
     941 
     942  fixDoubleClick(Stella.prototype, 'onIconClick', 'onIconDblClick'); 
    799943 
    800944  // }}} 
     
    804948  *********************************************************************************/ 
    805949 
    806   let (nsl = liberator.plugins.nico_statusline) { 
     950  if (InVimperator) { 
     951    let estella = liberator.plugins.stella; 
     952 
    807953    let install = function () { 
    808       let stella = liberator.plugins.nico_statusline = new Stella(); 
     954      let stella = liberator.plugins.stella = new Stella(); 
    809955      stella.addUserCommands(); 
    810956      liberator.log('Stella: installed.') 
    811957    } 
    812     if (nsl) { 
    813       nsl.finalize(); 
     958 
     959    // すでにインストール済みの場合は、一度ファイナライズする 
     960    // (デバッグ時に前のパネルが残ってしまうため) 
     961    if (estella) { 
     962      estella.finalize(); 
    814963      install(); 
    815964    } else { 
     
    823972      ); 
    824973    } 
     974  } else { 
     975    /* do something */ 
    825976  } 
    826977