Skip to content

Commit

Permalink
Fix: "Save image" Action Key does not work (#1437)
Browse files Browse the repository at this point in the history
* Temp workaround

I don't like it, but it works

* adds fix to 'downloadFileBlob'

* Change control statements so persmissions.request fires after permissions.contains returns false

* Better variables
  • Loading branch information
LiliaDoe authored Oct 28, 2024
1 parent cba2c17 commit 02bb442
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,52 @@ function downloadFile(url, filename, conflictAction, callback) {
function onMessage(message, sender, callback) {
switch (message.action) {
case 'downloadFileBlob':
// direct URL download through Chrome API might be prohibited (e.g: Pixiv)
// workaround:
// 1. obtain ArrayBuffer from XHR request (GET URL)
// 2. create Blob from ArrayBuffer
// 3. download Blob URL through Chrome API
/**
* direct URL download through Chrome API might be prohibited (e.g: Pixiv)
* workaround:
* 1. obtain ArrayBuffer from XHR request (GET URL)
* 2. create Blob from ArrayBuffer
* 3. download Blob URL through Chrome API
*/

/*
* Workaround for permissions.request not returning a promise in Firefox
* First checks if permissions are availble. If true, downloads file. If not, requests them.
* Not as clean or effecient as just using 'permissions.request'.
*/
cLog('downloadFileBlob: ' + message);
chrome.permissions.request({permissions: ['downloads']}, function (granted) {
cLog('downloadFile granted: ' + granted);
if (granted) {
chrome.permissions.contains({permissions: ['downloads']}, (contained) => {
cLog('downloadFile contains: ' + contained);
if (contained) {
ajaxRequest({method:'GET', response:'DOWNLOAD', url:message.url, filename:message.filename, conflictAction:message.conflictAction, headers:message.headers}, callback);
} else {
chrome.permissions.request({permissions: ['downloads']}, (granted) => {
cLog('downloadFile granted: ' + granted);
if (granted) {
ajaxRequest({method:'GET', response:'DOWNLOAD', url:message.url, filename:message.filename, conflictAction:message.conflictAction, headers:message.headers}, callback);
}
})
}
});
return true;
case 'downloadFile':
cLog('downloadFile: ' + message);
chrome.permissions.request({permissions: ['downloads']}, function (granted) {
cLog('downloadFile granted: ' + granted);
if (granted) {
/*
* Workaround for permissions.request not returning a promise in Firefox
* First checks if permissions are availble. If true, downloads file. If not, requests them.
* Not as clean or effecient as just using 'permissions.request'.
*/
chrome.permissions.contains({permissions: ['downloads']}, (contained) => {
cLog('downloadFile contains: ' + contained);
if (contained) {
downloadFile(message.url, message.filename, message.conflictAction, callback);
} else {
chrome.permissions.request({permissions: ['downloads']}, (granted) => {
cLog('downloadFile granted: ' + granted);
if (granted) {
downloadFile(message.url, message.filename, message.conflictAction, callback);
}
})
}
});
return true;
Expand Down

0 comments on commit 02bb442

Please sign in to comment.