Skip to content

Commit

Permalink
added option to adjust the fallback behaviour, ref: gautamkrishnar#123
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamkrishnar committed May 6, 2023
1 parent 235f4f5 commit 468bbfd
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
4 changes: 4 additions & 0 deletions app/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,9 @@
"setPort": {
"message": "Set Port",
"description": "Set Port"
},
"downloadFallback": {
"message": "If launching in Motrix fails, use the browser's default download manager",
"description": "If launching in Motrix fails, use the browser's default download manager"
}
}
4 changes: 4 additions & 0 deletions app/_locales/zh/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,9 @@
"setPort": {
"message": "设定埠",
"description": "设定埠"
},
"downloadFallback": {
"message": "如果在 motrix 中启动失败,请使用浏览器的默认下载管理器",
"description": "如果在 motrix 中启动失败,请使用浏览器的默认下载管理器"
}
}
38 changes: 34 additions & 4 deletions app/scripts/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ async function downloadAgent() {
'minFileSize',
'blacklist',
'motrixPort',
'downloadFallback',
]);

const getAriaDownloader = async (options) => {
Expand All @@ -59,7 +60,7 @@ async function downloadAgent() {
});

const shouldCheck =
statuses[0]?.byExtensionName !== 'Motrix WebExtension';
statuses[0]?.byExtensionName !== browser.i18n.getMessage('appName');

// Extension is disabled
if (shouldCheck && !result.extensionStatus) return;
Expand Down Expand Up @@ -128,9 +129,38 @@ async function downloadAgent() {
await downloader.handleStart(result, downloadItem, history);
} catch {
if (downloader instanceof AriaDownloader) {
await browser.downloads.resume(downloadItem.id);
downloader = new BrowserDownloader();
await downloader.handleStart(result, downloadItem, history);
if (
typeof result.downloadFallback === 'undefined' ||
result?.downloadFallback
) {
await browser.downloads.resume(downloadItem.id);
downloader = new BrowserDownloader();
await downloader.handleStart(result, downloadItem, history);
} else {
await browser?.downloads
?.removeFile(downloadItem.id)
.then()
.catch(onError);
await browser?.downloads
?.cancel(downloadItem.id)
.then()
.catch(onError);
await browser?.downloads
?.erase({ id: downloadItem.id })
.then()
.catch(onError);
const notificationOptions = {
type: 'basic',
iconUrl: '../images/icon-large.png',
title: 'Connection to motrix is not working',
message:
'Browser download fallback is also not enabled. Your download will be cancelled.',
};
const notificationId = Math.round(
new Date().getTime() / 1000
).toString();
browser.notifications.create(notificationId, notificationOptions);
}
}
}
}, onError);
Expand Down
26 changes: 26 additions & 0 deletions app/scripts/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function ConfigView() {
const [motrixAPIkey, setMotrixAPIkey] = useState('');
const [extensionStatus, setExtensionStatus] = useState(false);
const [enableNotifications, setEnableNotifications] = useState(false);
const [downloadFallback, setDownloadFallback] = useState(true);
const [enableDownloadPrompt, setEnableDownloadPrompt] = useState(false);
const [minFileSize, setMinFileSize] = useState('');
const [blacklist, setBlacklist] = useState([]);
Expand All @@ -40,6 +41,7 @@ function ConfigView() {
'hideChromeBar',
'showContextOption',
'motrixPort',
'downloadFallback',
])
.then(
(result) => {
Expand All @@ -65,6 +67,12 @@ function ConfigView() {
} else {
setExtensionStatus(result.extensionStatus);
}
if (typeof result.downloadFallback === 'undefined') {
browser.storage.sync.set({ downloadFallback: true });
setDownloadFallback(true);
} else {
setDownloadFallback(result.downloadFallback);
}

if (typeof result.enableNotifications === 'undefined') {
browser.storage.sync.set({ enableNotifications: true });
Expand Down Expand Up @@ -209,6 +217,24 @@ function ConfigView() {
</Box>
</Grid>

{/* Fallback status switch */}
<Grid item xs={6}>
<FormLabel>__MSG_downloadFallback__</FormLabel>
</Grid>
<Grid item xs={2}>
<Box display="flex" justifyContent="center">
<Switch
checked={downloadFallback}
onClick={() => {
browser.storage.sync.set({
downloadFallback: !downloadFallback,
});
setDownloadFallback((x) => !x);
}}
/>
</Box>
</Grid>

{/* Notifications status switch */}
<Grid item xs={6}>
<FormLabel>__MSG_enableNotifications__</FormLabel>
Expand Down

0 comments on commit 468bbfd

Please sign in to comment.