Skip to content

Commit

Permalink
add/remove onDeterminingFilename listener
Browse files Browse the repository at this point in the history
  • Loading branch information
hpat0003 committed Nov 8, 2018
1 parent 7ee3299 commit f116621
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function main() {

// array of titles of resources in lower case
let resourceTitle = []
let resourcesList = []

// downloadResources on button press
let button = document.getElementById("downloadResources");
Expand Down Expand Up @@ -58,18 +57,13 @@ function main() {
}
});
initStorage();
chrome.downloads.onDeterminingFilename.addListener((downloadItem, suggest) => (
item = resourcesList.filter(r=>r.url==downloadItem.url)[0],
console.log(downloadItem),
console.log(resourcesList),
suggest({filename: item.course + '/' + item.section + '/' + downloadItem.filename})))
}

function initStorage() {
chrome.storage.sync.get(['downloads', 'alreadyRequested'], result => {
let value = result.downloads ? result.downloads : 0;
let boolean = result.alreadyRequested ? result.alreadyRequested : false;
chrome.storage.sync.set({'downloads': value, 'alreadyRequested': boolean}, function() {
let downloads = result.downloads ? result.downloads : 0;
let alreadyRequested = result.alreadyRequested ? result.alreadyRequested : false;
chrome.storage.sync.set({'downloads': downloads, 'alreadyRequested': alreadyRequested}, function() {
console.log('initialised storage variables');
});
})
Expand Down Expand Up @@ -140,12 +134,27 @@ function updateDownloads(newDownloads) {
});
}

function suggestFilename(downloadItem, suggest) {
item = resourcesList.filter(r => r.url==downloadItem.url)[0],
console.log(downloadItem),
console.log(resourcesList),
suggest({filename: item.course + '/' + item.section + '/' + downloadItem.filename})
}

function downloadResources() {
const INTERVAL = 500;
let footer = document.getElementById("footer");
let button = document.getElementById("downloadResources");
let resourceSelector = document.getElementById("resourceSelector");
let selectedOptions = Array.from(resourceSelector.selectedOptions);
let organizeChecked = document.getElementById('organize').checked;
let hasDownloadsListener = chrome.downloads.onDeterminingFilename.hasListener(suggestFilename);

// add/remove listener to organize files
if (organizeChecked && !hasDownloadsListener)
chrome.downloads.onDeterminingFilename.addListener(suggestFilename);
else if (!organizeChecked && hasDownloadsListener)
chrome.downloads.onDeterminingFilename.removeListener(suggestFilename);

// hidding the button and showing warning text
button.setAttribute('hidden', 'hidden');
Expand Down Expand Up @@ -180,4 +189,5 @@ function downloadResources() {

document.addEventListener('DOMContentLoaded', () => {
main();
var resourcesList = []
});

0 comments on commit f116621

Please sign in to comment.