Skip to content

Commit

Permalink
fix: It will no longer trigger notification from other sources
Browse files Browse the repository at this point in the history
resolve #42
  • Loading branch information
RossWang committed Dec 20, 2018
1 parent 311701f commit cf9660e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
6 changes: 3 additions & 3 deletions App/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function sendTo(url, fileName, filePath, header, server) {
function (res) {
aria2.addUri([url], params).then(
function (res) {
monitor(options);
monitor(options, res);
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
aria2.close();
},
Expand All @@ -66,7 +66,7 @@ function sendTo(url, fileName, filePath, header, server) {
setTimeout( () => {
aria2.addUri([url], params).then(
function (res) {
monitor(options);
monitor(options, res);
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
aria2.close();
},
Expand All @@ -86,7 +86,7 @@ function sendTo(url, fileName, filePath, header, server) {
aria2.open().then( () => {
aria2.addUri([url], params).then(
function (res) {
monitor(options);
monitor(options, res);
notify(browser.i18n.getMessage("success_connect", fileName) + "\n\n" + url);
aria2.close();
},
Expand Down
42 changes: 21 additions & 21 deletions App/lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@ async function correctFileName(name) {
return tmp;
}

function monitor(options) {
function monitor(options, gid) {
if (mon == undefined) {
mon = new Worker("/lib/worker.js");
}
browser.storage.local.get(config.command.guess, function(item) {
mon.postMessage([options]);
mon.onmessage = function(e) {
console.log(e.data);
if (e.data[0] == "complete") {
notify(browser.i18n.getMessage("download_complete", e.data[1] ));
if (item.sound != "0") {
var audio = new Audio('/data/Sound/complete' + item.sound + '.wav');
audio.play();
browser.storage.local.get(config.command.guess, function(item) {
if (e.data[0] == "complete") {
notify(browser.i18n.getMessage("download_complete", e.data[1] ));
if (item.sound != "0") {
var audio = new Audio('/data/Sound/complete' + item.sound + '.wav');
audio.play();
}
}
}
else if (e.data[0] == "badge" && item.badge){
if(e.data[1] == 0){
browser.browserAction.setBadgeText({text: ""});
mon = null;
else if (e.data[0] == "badge" && item.badge){
if(e.data[1] == 0){
browser.browserAction.setBadgeText({text: ""});
mon = null;
}
else {
browser.browserAction.setBadgeText({text: e.data[1].toString()});
}
}
else {
browser.browserAction.setBadgeText({text: e.data[1].toString()});
else if (e.data[0] == "error"){
notify(browser.i18n.getMessage("download_error", e.data[1] ));
}
}
else if (e.data[0] == "error"){
notify(browser.i18n.getMessage("download_error", e.data[1] ));
}
});
}
});
}
mon.postMessage([options, gid]);
}

function notify(message) {
Expand Down
11 changes: 8 additions & 3 deletions App/lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ postMessage("init");
importScripts("aria.js");
importScripts("polygoat.js");
var aria2;
var gids = [];

onmessage = function(e) {
gids.push(e.data[1]);
if (aria2 == undefined){
connect(e.data[0]);
postMessage("connect");
Expand Down Expand Up @@ -36,9 +38,12 @@ function connect(options) {
});
};
aria2.onDownloadComplete = function(d) {
aria2.getFiles(d.gid).then((info) => {
postMessage(["complete", info[0].path]);
});
if (gids.includes(d.gid)) {
aria2.getFiles(d.gid).then((info) => {
postMessage(["complete", info[0].path]);
});
gids = gids.filter(gid => gid != d.gid);
}
aria2.tellActive(["gid"]).then((info) => {
postMessage(["badge", info.length]);
if(info.length == 0) {
Expand Down

0 comments on commit cf9660e

Please sign in to comment.