Join MDN and developers like you at Mozilla's View Source conference, 12-14 September in Berlin, Germany. Learn more at https://viewsourceconf.org

Download Observer

Firefox ダウンロードマネージャー用のダウンロード監視サンプル

// ******************************
// DownloadObserver
// ******************************
function sampleDownload_init(){
  //**** ダウンロード監視の追加
  var observerService = Components.classes["@mozilla.org/observer-service;1"]
                                  .getService(Components.interfaces.nsIObserverService);
  observerService.addObserver(sampleDownloadObserver, "dl-start", false);
  observerService.addObserver(sampleDownloadObserver, "dl-done", false);
  observerService.addObserver(sampleDownloadObserver, "dl-cancel", false);
  observerService.addObserver(sampleDownloadObserver, "dl-failed", false);

  window.addEventListener("unload", function() {
    observerService.removeObserver(sampleDownloadObserver, "dl-start");
    observerService.removeObserver(sampleDownloadObserver, "dl-done");
    observerService.removeObserver(sampleDownloadObserver, "dl-cancel");
    observerService.removeObserver(sampleDownloadObserver, "dl-failed");
  }, false);
}
var sampleDownloadObserver = {
  observe: function (subject, topic, state) {
    var oDownload = subject.QueryInterface(Components.interfaces.nsIDownload);
    //**** ダウンロードファイルを持つオブジェクトを取得
    var oFile = null;
    try{
      oFile = oDownload.targetFile;  // New firefox 0.9+
    } catch (e){
      oFile = oDownload.target;      // Old firefox 0.8
    }
    //**** ダウンロード開始イベント
    if (topic == "dl-start"){
      alert('Start download to - '+oFile.path); 
    }
    //**** ダウンロードキャンセルイベント
    if(topic == "dl-cancel"){ 
      alert('Canceled download to - '+oFile.path); 
    }
    //**** ダウンロード失敗
    else if(topic == "dl-failed"){ 
      alert('Failed download to - '+oFile.path);
    }
    //**** ダウンロード完了
    else if(topic == "dl-done"){
      alert('Done download to - '+oFile.path);
    }    
  }
}
window.addEventListener("load", sampleDownload_init, false);

 

ドキュメントのタグと貢献者

タグ: 
 このページの貢献者: tregagnon, Mgjbot, Shimono
 最終更新者: tregagnon,