X-Git-Url: https://git.josue.xyz/?a=blobdiff_plain;f=.config%2FBraveSoftware%2FBrave-Browser%2FDefault%2FExtensions%2Fcimiefiiaegbelhefglklhhakcgmhkai%2F1.7.6_0%2Fextension-downloads.js;fp=.config%2FBraveSoftware%2FBrave-Browser%2FDefault%2FExtensions%2Fcimiefiiaegbelhefglklhhakcgmhkai%2F1.7.6_0%2Fextension-downloads.js;h=031e99b9f20edc0ef3998f4e24d7cb3a4e314afe;hb=3aba54c891969552833dbc350b3139e944e17a97;hp=0000000000000000000000000000000000000000;hpb=1def8ecce8e6f3aa32e6978d0ba7846a99b8de34;p=dotfiles%2F.git diff --git a/.config/BraveSoftware/Brave-Browser/Default/Extensions/cimiefiiaegbelhefglklhhakcgmhkai/1.7.6_0/extension-downloads.js b/.config/BraveSoftware/Brave-Browser/Default/Extensions/cimiefiiaegbelhefglklhhakcgmhkai/1.7.6_0/extension-downloads.js new file mode 100644 index 00000000..031e99b9 --- /dev/null +++ b/.config/BraveSoftware/Brave-Browser/Default/Extensions/cimiefiiaegbelhefglklhhakcgmhkai/1.7.6_0/extension-downloads.js @@ -0,0 +1,152 @@ +/* + Copyright (C) 2017-2019 Kai Uwe Broulik + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 3 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +var activeDownloads = [] +var downloadUpdateInterval = 0; + +function startSendingDownloadUpdates() { + if (!downloadUpdateInterval) { + downloadUpdateInterval = setInterval(sendDownloadUpdates, 1000); + } +} + +function stopSendingDownloadUpdates() { + if (downloadUpdateInterval) { + clearInterval(downloadUpdateInterval); + downloadUpdateInterval = 0; + } +} + +function sendDownloadUpdates() { + chrome.downloads.search({ + state: 'in_progress', + paused: false + }, function (results) { + if (!results.length) { + stopSendingDownloadUpdates(); + return; + } + + results.forEach(function (download) { + if (activeDownloads.indexOf(download.id) === -1) { + return; + } + + var payload = { + id: download.id, + bytesReceived: download.bytesReceived, + estimatedEndTime: download.estimatedEndTime, + // Firefox ends along "-1" as totalBytes on download creation + // but then never updates it, so we send this along periodically, too + totalBytes: download.totalBytes + }; + + port.postMessage({subsystem: "downloads", event: "update", download: payload}); + }); + }); +} + +function createDownload(download) { + // don't bother telling us about completed downloads... + // otherwise on browser startup we'll spawn a gazillion download progress notification + if (download.state === "complete" || download.state === "interrupted") { + return; + } + + activeDownloads.push(download.id); + startSendingDownloadUpdates(); + + port.postMessage({subsystem: "downloads", event: "created", download: download}); +} + +function sendDownloads() { + // When extension is (re)loaded, create each download initially + chrome.downloads.search({ + state: 'in_progress', + }, function (results) { + results.forEach(createDownload); + }); +} + +chrome.downloads.onCreated.addListener(createDownload); + +chrome.downloads.onChanged.addListener(function (delta) { + if (activeDownloads.indexOf(delta.id) === -1) { + return; + } + + // An interrupted download was resumed. When a download is interrupted, we finish (and delete) + // the job but the browser re-uses the existing download, so when this happen, + // pretend a new download was created. + if (delta.state) { + if (delta.state.previous === "interrupted" && delta.state.current === "in_progress") { + console.log("Resuming previously interrupted download, pretending a new download was created"); + chrome.downloads.search({ + id: delta.id + }, function (downloads) { + createDownload(downloads[0]); + }); + return; + } + } + + // The update timer stops automatically when there are no running downloads + // so make sure to restart it when a download is unpaused + if (delta.paused) { + if (delta.paused.previous && !delta.paused.current) { + startSendingDownloadUpdates(); + } + } + + var payload = {}; + + Object.keys(delta).forEach((key) => { + payload[key] = delta[key].current; + }); + + payload.id = delta.id; // not a delta, ie. has no current and thus isn't added by the loop below + + port.postMessage({subsystem: "downloads", event: "update", download: payload}); +}); + +addCallback("downloads", "cancel", function (message) { + var downloadId = message.downloadId; + + console.log("Requested to cancel download", downloadId); + + chrome.downloads.cancel(downloadId); +}); + +addCallback("downloads", "suspend", function (message) { + var downloadId = message.downloadId; + + console.log("Requested to suspend download", downloadId); + + chrome.downloads.pause(downloadId); +}); + +addCallback("downloads", "resume", function (message) { + var downloadId = message.downloadId; + + console.log("Requested to resume download", downloadId); + + chrome.downloads.resume(downloadId); +}); + +addCallback("downloads", "createAll", () => { + sendDownloads(); +});