Skip to content

Commit

Permalink
refactor: Refine the aria2.js
Browse files Browse the repository at this point in the history
close #215
  • Loading branch information
alexhua committed Feb 15, 2025
1 parent 0c16b1a commit 1a7f467
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 104 deletions.
18 changes: 11 additions & 7 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ const NID_TASK_NEW = "NID_TASK_NEW";
const NID_TASK_STOPPED = "NID_TASK_STOPPED";
const NID_CAPTURED_OTHERS = "NID_CAPTURED_OTHERS";

const INTERVAL_SHORT = 1000;
const INTERVAL_LONG = 3000;

var CurrentWindowId = 0;
var CurrentTabUrl = "about:blank";
var MonitorId = -1;
var MonitorInterval = 3000; // Aria2 monitor interval 3000ms
var MonitorInterval = INTERVAL_LONG; // Aria2 monitor interval 3000ms
var RemoteAria2List = [];
var IconAnimController = new AnimationController();

Expand Down Expand Up @@ -645,6 +648,7 @@ function enableMonitor() {
}

function disableMonitor() {
monitorAria2();
clearInterval(MonitorId);
MonitorId = -1;
chrome.action.setBadgeText({ text: "" });
Expand All @@ -664,6 +668,8 @@ async function monitorAria2() {
for (const i in RemoteAria2List) {
const remoteAria2 = RemoteAria2List[i];
try {
if (!remoteAria2.socket) remoteAria2.openSocket();

let response = await remoteAria2.getGlobalStat();
if (response && response.error) {
throw response.error;
Expand All @@ -679,8 +685,6 @@ async function monitorAria2() {
chrome.downloads.onDeterminingFilename.addListener(captureDownload);
}

if (!remoteAria2.socket) remoteAria2.openSocket();

// Only for default aria2, needs Aria2 enhanced version
const percent = response.result.percentActive;
if (i == 0 && percent && !isNaN(percent)) {
Expand All @@ -705,8 +709,8 @@ async function monitorAria2() {
}

if (active > 0) {
if (MonitorInterval == 3000) {
MonitorInterval = 1000;
if (MonitorInterval == INTERVAL_LONG) {
MonitorInterval = INTERVAL_SHORT;
disableMonitor();
enableMonitor();
}
Expand All @@ -715,8 +719,8 @@ async function monitorAria2() {
else
chrome.power.releaseKeepAwake();
} else if (active == 0) {
if (MonitorInterval == 1000) {
MonitorInterval = 3000;
if (MonitorInterval == INTERVAL_SHORT) {
MonitorInterval = INTERVAL_LONG;
disableMonitor();
enableMonitor();
}
Expand Down
Loading

0 comments on commit 1a7f467

Please sign in to comment.