Skip to content

Commit

Permalink
Add Chrome support for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
gyng committed Oct 17, 2017
1 parent 4f1fa95 commit 56a8dd1
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src/notification.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/notifications

const ICON_URL = "icons/ic_archive_black_128px.png";

const downloadsList = {}; // global

const addNotifications = options => {
Expand All @@ -17,19 +19,39 @@ const addNotifications = options => {

browser.downloads.onChanged.addListener(downloadDelta => {
const item = downloadsList[downloadDelta.id];
const slashIdx = item.filename && item.filename.lastIndexOf("/");
const filename = item.filename && item.filename.substring(slashIdx + 1);

if (
notifyOnFailure &&
(!downloadDelta ||
// CHROME
// Chrome does not have the filename in the initial DownloadItem,
// so extract it from the DownloadDelta
if (chrome) {
if (
downloadDelta &&
downloadDelta.filename &&
downloadDelta.filename.current
) {
downloadsList[downloadDelta.id].filename =
downloadDelta.filename.current;
}
}

const fullFilename = item && item.filename;
const slashIdx = fullFilename && fullFilename.lastIndexOf("/");
const filename = fullFilename.substring(slashIdx + 1);

// CHROME
// Chrome's DownloadDelta contains different information from Firefox's
const failed = chrome
? downloadDelta.error
: !downloadDelta ||
!downloadDelta.state ||
downloadDelta.state.current === "interrupted")
) {
downloadDelta.state.current === "interrupted";

if (notifyOnFailure && failed) {
browser.notifications.create(String(downloadDelta.id), {
type: "basic",
title: "Failed to save",
message: `${filename}`
iconUrl: ICON_URL,
message: filename
});
} else if (
notifyOnSuccess &&
Expand All @@ -39,7 +61,8 @@ const addNotifications = options => {
browser.notifications.create(String(downloadDelta.id), {
type: "basic",
title: "Saved",
message: `${filename}`
iconUrl: ICON_URL,
message: filename
});
}
});
Expand Down

0 comments on commit 56a8dd1

Please sign in to comment.