--- title: downloads.onChanged slug: Mozilla/Add-ons/WebExtensions/API/downloads/onChanged tags: - API - Add-ons - Event - Extensions - Non-standard - Reference - Téléchargement - WebExtensions - downloads - onChanged translation_of: Mozilla/Add-ons/WebExtensions/API/downloads/onChanged ---
L'événement onChanged() de l'API {{WebExtAPIRef("downloads")}} est déclenché lorsque l'une des propriétés de {{WebExtAPIRef('downloads.DownloadItem')}} change (à l'exception de bytesReceived).
L'écouteur reçoit un fichier downloadDelta en tant que paramètre — un objet contenant le downloadId de l'objet {{WebExtAPIRef('downloads.DownloadItem')}} en question, plus le statut de toutes les propriétés qui ont changé.
browser.downloads.onChanged.addListener(listener) browser.downloads.onChanged.removeListener(listener) browser.downloads.onChanged.hasListener(listener)
Les événements ont trois fonctions :
addListener(callback)removeListener(listener)listener est l'écouteur à supprimer.hasListener(listener)listener donné est enregistré pour cet événement. Renvoie true s'il écoute, sinon false.callbackUne fonction de rappel qui sera appelée lorsque cet événement se produira. Cette fonction recevra les arguments suivants :
downloadDeltaobjet représentant l'objet {{WebExtAPIRef('downloads.DownloadItem')}} qui a été modifié, ainsi que l'état de toutes les propriétés qui y ont été modifiées.L'objet downloadDelta a les propriétés suivantes disponibles :
idinteger représentant l'identifiant de l'id {{WebExtAPIRef('downloads.DownloadItem')}} qui a changé.url{{optional_inline}}url {{WebExtAPIRef('downloads.DownloadItem')}}.filename{{optional_inline}}filename {{WebExtAPIRef('downloads.DownloadItem')}}danger{{optional_inline}}danger {{WebExtAPIRef('downloads.DownloadItem')}}.mime{{optional_inline}}mime {{WebExtAPIRef('downloads.DownloadItem')}}startTime{{optional_inline}}startTime {{WebExtAPIRef('downloads.DownloadItem')}}.endTime{{optional_inline}}endTime {{WebExtAPIRef('downloads.DownloadItem')}}.state{{optional_inline}}état {{WebExtAPIRef('downloads.DownloadItem')}}canResume{{optional_inline}}canResume.paused{{optional_inline}}pause {{WebExtAPIRef('downloads.DownloadItem')}}.error{{optional_inline}}erreur {{WebExtAPIRef('downloads.DownloadItem')}}.totalBytes{{optional_inline}}totalBytes {{WebExtAPIRef('downloads.DownloadItem')}}.fileSize{{optional_inline}}fileSize {{WebExtAPIRef('downloads.DownloadItem')}}.exists{{optional_inline}}{{Compat("webextensions.api.downloads.onChanged")}}
Enregistrez un message lorsque les téléchargements sont terminés :
function handleChanged(delta) {
if (delta.state && delta.state.current === "complete") {
console.log(`Download ${delta.id} has completed.`);
}
}
browser.downloads.onChanged.addListener(handleChanged);
{{WebExtExamples}}
This API is based on Chromium's chrome.downloads API.
Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.
// Copyright 2015 The Chromium Authors. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.