Skip to content

Commit

Permalink
Merge pull request #1125 from GrosPoulet/master
Browse files Browse the repository at this point in the history
Fix for missing file extension after download (#1116)
  • Loading branch information
GrosPoulet authored Apr 8, 2023
2 parents 461a4fb + 624738a commit 5516c83
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
37 changes: 35 additions & 2 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ function downloadFile(url, filename, conflictAction, callback) {
}
function onMessage(message, sender, callback) {
switch (message.action) {
case 'downloadFile':
case 'downloadFileBlob':
// direct URL download through Chrome API might be prohibited (e.g: Pixiv)
// workaround:
// 1. obtain ArrayBuffer from XHR request (GET URL)
// 1. obtain ArrayBuffer from XHR request (GET URL)
// 2. create Blob from ArrayBuffer
// 3. download Blob URL through Chrome API
if (options.enableDownloads) {
Expand All @@ -84,6 +84,39 @@ function onMessage(message, sender, callback) {
}
});
}
case 'downloadFile':

function onChanged(delta) {
if (!delta) return;
if (delta.id != currentId) return;
if (delta.state && delta.state.current !== 'in_progress') {
chrome.downloads.onChanged.removeListener(onChanged);
// call callback only if download failed
if (delta.state.current !== 'complete') {
callback();
}
}
}

if (options.enableDownloads) {
var currentId;
chrome.downloads.onChanged.addListener(onChanged);
chrome.downloads.download({url: message.url, filename: message.filename, conflictAction: message.conflictAction, saveAs: false}, id => { currentId = id });
return true;
} else {
chrome.permissions.request({
permissions: ['downloads']
}, function (granted) {
if (granted) {
var currentId;
chrome.downloads.onChanged.addListener(onChanged);
chrome.downloads.download({url: message.url, filename: message.filename, conflictAction: message.conflictAction, saveAs: false}, id => { currentId = id });
return true;
} else {
return false;
}
});
}
case 'ajaxGet':
ajaxRequest({url:message.url, response:message.response, method:'GET', headers:message.headers}, callback);
return true;
Expand Down
18 changes: 12 additions & 6 deletions js/hoverzoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -2799,12 +2799,18 @@ var hoverZoom = {
cLog('filename: ' + filename);
}

chrome.runtime.sendMessage({
action: 'downloadFile',
url: url,
filename: filename,
conflictAction: 'uniquify'
}, callback);
// 1st attempt to download file (Chrome API)
chrome.runtime.sendMessage({action:'downloadFile',
url:url,
filename:filename,
conflictAction:'uniquify'},
function() {
// 2nd attempt (blob + Chrome API)
chrome.runtime.sendMessage({action:'downloadFileBlob',
url:url,
filename:filename,
conflictAction:'uniquify'});
});
}

// 4 types of media can be saved to disk: image, video, audio, playlist
Expand Down

0 comments on commit 5516c83

Please sign in to comment.