Hatena::ブログ(Diary)

サバニFRP

2006-11-13

[]動画挿入スクリプト

なんか最近の流行のようなので、bbs2chreaderYoutubeStage6 のリンクから動画を挿入するスクリプトを書いてみた。

お笑い動画@Youtube板 のようなたくさん動画リンクがあるスレだとすごく重い。挿入じゃなくてポップアップのほうがいいかもしれない。

// <script type="text/javascript; e4x=1" src="<SKINPATH/>insertvideo.js"></script>

var insertVideo = {

  appendYoutubeObject: function(aRefNode, aVideoID){
    var dataURL = "http://www.youtube.com/v/" + aVideoID;
    var xml = <span>
      <br/>
      <object data={dataURL} type="application/x-shockwave-flash"
          width="340" height="288">
        <param name="wmode" value="transparent"></param>
      </object>
    </span>;

    var range = document.createRange();
    range.selectNode(aRefNode.parentNode);
    var fragment = range.createContextualFragment(xml.toXMLString());
    aRefNode.parentNode.insertBefore(fragment, aRefNode.nextSibling);
  },

  appendStage8Object: function(aRefNode, aVideoID){
    var dataURL = "http://video-akamai.stage6.com/" + aVideoID + ".divx";
    var thumbnailURL = "http://images.stage6.com/videos/" + aVideoID + ".jpg";
    var xml = <span>
      <br/>
      <object data={dataURL} type="video/divx"
          width="340" height="288">
        <param name="autoPlay" value="false"></param>
      </object>
      <img src={thumbnailURL} width="200"/>
    </span>;

    var range = document.createRange();
    range.selectNode(aRefNode.parentNode);
    var fragment = range.createContextualFragment(xml.toXMLString());
    aRefNode.parentNode.insertBefore(fragment, aRefNode.nextSibling);
  },

  insertVideo: function(){
    var xpathYoutube = "descendant::a[contains(@href, 'http://www.youtube.com/watch')]";
    var xpathStage8 = "descendant::a[contains(@href, 'http://stage6.divx.com/content/show')]";
    var xpathStage8m = "descendant::a[contains(@href, 'http://stage6.divx.com/members')]";

    var xpathResult = document.evaluate(xpathYoutube +"|"+ xpathStage8  +"|"+ xpathStage8m,
          document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);    

    for(var i = 0; i < xpathResult.snapshotLength; i++){
      var node = xpathResult.snapshotItem(i);
      if(node.href.indexOf("www.youtube.com")!=-1 &&
              node.href.match(/v=([^\&]+)/)){
        insertVideo.appendYoutubeObject(node, RegExp.$1);
      }else if(node.href.indexOf("stage6.divx.com")!=-1 &&
               node.href.match(/content_id=([^\&]+)/)){
        insertVideo.appendStage8Object(node, RegExp.$1);
      }else if(node.href.indexOf("stage6.divx.com")!=-1 &&
               node.href.match(/videos\/([^\&]+)/)){
        insertVideo.appendStage8Object(node, RegExp.$1);
      }
    }  
  }

}
window.addEventListener("load", insertVideo.insertVideo, false);