1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
---
title: Obserwator pobierania danych
slug: Fragmenty_kodu/Obserwator_pobierania_danych
translation_of: Archive/Add-ons/Code_snippets/Downloading_Files
---
<p>Przykład obserwatora pobierania danych dla menedżera pobierania plików Firefoksa:</p>
<pre>// ******************************
// DownloadObserver
// ******************************
function sampleDownload_init(){
//**** Add download observer
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);
//**** Get Download file object
var oFile = null;
try{
oFile = oDownload.targetFile; // New firefox 0.9+
} catch (e){
oFile = oDownload.target; // Old firefox 0.8
}
//**** Download Start
if (topic == "dl-start"){
alert('Start download to - '+oFile.path);
}
//**** Download Cancel
if(topic == "dl-cancel"){
alert('Canceled download to - '+oFile.path);
}
//**** Download Failed
else if(topic == "dl-failed"){
alert('Failed download to - '+oFile.path);
}
//**** Download Successs
else if(topic == "dl-done"){
alert('Done download to - '+oFile.path);
}
}
}
window.addEventListener("load", sampleDownload_init, false);
</pre>
<p> </p>
<div class="noinclude">
</div>
<p>{{ languages( { "en": "en/Code_snippets/Downloading_Files", "fr": "fr/Extraits_de_code/Observateur_de_t\u00e9l\u00e9chargement", "ja": "ja/Code_snippets/Download_Observer" } ) }}</p>
|