Changeset View
Changeset View
Standalone View
Standalone View
extension/extension.js
Show First 20 Lines • Show All 308 Lines • ▼ Show 20 Line(s) | |||||
309 | 309 | | |||
310 | 310 | | |||
311 | 311 | | |||
312 | // Downloads | 312 | // Downloads | ||
313 | // ------------------------------------------------------------------------ | 313 | // ------------------------------------------------------------------------ | ||
314 | // | 314 | // | ||
315 | 315 | | |||
316 | var activeDownloads = [] | 316 | var activeDownloads = [] | ||
317 | var downloadUpdateInterval = 0; | ||||
317 | 318 | | |||
318 | setInterval(function() { | 319 | function startSendingDownloadUpdates() { | ||
320 | if (!downloadUpdateInterval) { | ||||
321 | downloadUpdateInterval = setInterval(sendDownloadUpdates, 1000); | ||||
322 | } | ||||
323 | } | ||||
324 | | ||||
325 | function stopSendingDownloadUpdates() { | ||||
326 | if (downloadUpdateInterval) { | ||||
327 | clearInterval(downloadUpdateInterval); | ||||
328 | downloadUpdateInterval = 0; | ||||
329 | } | ||||
330 | } | ||||
331 | | ||||
332 | function sendDownloadUpdates() { | ||||
319 | chrome.downloads.search({ | 333 | chrome.downloads.search({ | ||
320 | state: 'in_progress', | 334 | state: 'in_progress', | ||
321 | paused: false | 335 | paused: false | ||
322 | }, function (results) { | 336 | }, function (results) { | ||
323 | if (!results.length) { | 337 | if (!results.length) { | ||
338 | stopSendingDownloadUpdates(); | ||||
324 | return; | 339 | return; | ||
325 | } | 340 | } | ||
326 | 341 | | |||
327 | results.forEach(function (download) { | 342 | results.forEach(function (download) { | ||
328 | if (activeDownloads.indexOf(download.id) === -1) { | 343 | if (activeDownloads.indexOf(download.id) === -1) { | ||
329 | return; | 344 | return; | ||
330 | } | 345 | } | ||
331 | 346 | | |||
332 | var payload = { | 347 | var payload = { | ||
333 | id: download.id, | 348 | id: download.id, | ||
334 | bytesReceived: download.bytesReceived, | 349 | bytesReceived: download.bytesReceived, | ||
335 | estimatedEndTime: download.estimatedEndTime, | 350 | estimatedEndTime: download.estimatedEndTime, | ||
336 | // Firefox ends along "-1" as totalBytes on download creation | 351 | // Firefox ends along "-1" as totalBytes on download creation | ||
337 | // but then never updates it, so we send this along periodically, too | 352 | // but then never updates it, so we send this along periodically, too | ||
338 | totalBytes: download.totalBytes | 353 | totalBytes: download.totalBytes | ||
339 | }; | 354 | }; | ||
340 | 355 | | |||
341 | port.postMessage({subsystem: "downloads", event: "update", download: payload}); | 356 | port.postMessage({subsystem: "downloads", event: "update", download: payload}); | ||
342 | }); | 357 | }); | ||
343 | }); | 358 | }); | ||
344 | }, 1000); | 359 | } | ||
345 | 360 | | |||
346 | // only forward certain download properties back to our host | 361 | // only forward certain download properties back to our host | ||
347 | var whitelistedDownloadProperties = [ | 362 | var whitelistedDownloadProperties = [ | ||
348 | "id", "url", "finalUrl", "filename", "startTime", "estimatedEndTime", "totalBytes", "bytesReceived", "state", "error", /*"canResume"*/, "paused" | 363 | "id", "url", "finalUrl", "filename", "startTime", "estimatedEndTime", "totalBytes", "bytesReceived", "state", "error", /*"canResume"*/, "paused" | ||
349 | ]; | 364 | ]; | ||
350 | 365 | | |||
351 | chrome.downloads.onCreated.addListener(function (download) { | 366 | chrome.downloads.onCreated.addListener(function (download) { | ||
352 | // don't bother telling us about completed downloads... | 367 | // don't bother telling us about completed downloads... | ||
353 | // otherwise on browser startup we'll spawn a gazillion download progress notification | 368 | // otherwise on browser startup we'll spawn a gazillion download progress notification | ||
354 | if (download.state === "complete" || download.state === "interrupted") { | 369 | if (download.state === "complete" || download.state === "interrupted") { | ||
355 | return; | 370 | return; | ||
356 | } | 371 | } | ||
357 | 372 | | |||
358 | var filteredDownload = filterObject(download, whitelistedDownloadProperties); | 373 | var filteredDownload = filterObject(download, whitelistedDownloadProperties); | ||
359 | 374 | | |||
360 | activeDownloads.push(download.id); | 375 | activeDownloads.push(download.id); | ||
376 | startSendingDownloadUpdates(); | ||||
361 | 377 | | |||
362 | port.postMessage({subsystem: "downloads", event: "created", download: filteredDownload}); | 378 | port.postMessage({subsystem: "downloads", event: "created", download: filteredDownload}); | ||
363 | }); | 379 | }); | ||
364 | 380 | | |||
365 | chrome.downloads.onChanged.addListener(function (delta) { | 381 | chrome.downloads.onChanged.addListener(function (delta) { | ||
366 | if (activeDownloads.indexOf(delta.id) === -1) { | 382 | if (activeDownloads.indexOf(delta.id) === -1) { | ||
367 | return; | 383 | return; | ||
368 | } | 384 | } | ||
▲ Show 20 Lines • Show All 271 Lines • Show Last 20 Lines |