diff --git a/app/_locales/en/messages.json b/app/_locales/en/messages.json index 8b7fcde..9a588d2 100644 --- a/app/_locales/en/messages.json +++ b/app/_locales/en/messages.json @@ -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" } } diff --git a/app/_locales/zh/messages.json b/app/_locales/zh/messages.json index e605df1..b29556e 100644 --- a/app/_locales/zh/messages.json +++ b/app/_locales/zh/messages.json @@ -58,5 +58,9 @@ "setPort": { "message": "设定埠", "description": "设定埠" + }, + "downloadFallback": { + "message": "如果在 motrix 中启动失败,请使用浏览器的默认下载管理器", + "description": "如果在 motrix 中启动失败,请使用浏览器的默认下载管理器" } } diff --git a/app/scripts/background.js b/app/scripts/background.js index 57c1fce..c3b8a0c 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -49,6 +49,7 @@ async function downloadAgent() { 'minFileSize', 'blacklist', 'motrixPort', + 'downloadFallback', ]); const getAriaDownloader = async (options) => { @@ -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; @@ -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); diff --git a/app/scripts/config.js b/app/scripts/config.js index b1209fa..6bb07ca 100644 --- a/app/scripts/config.js +++ b/app/scripts/config.js @@ -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([]); @@ -40,6 +41,7 @@ function ConfigView() { 'hideChromeBar', 'showContextOption', 'motrixPort', + 'downloadFallback', ]) .then( (result) => { @@ -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 }); @@ -209,6 +217,24 @@ function ConfigView() { + {/* Fallback status switch */} + + __MSG_downloadFallback__ + + + + { + browser.storage.sync.set({ + downloadFallback: !downloadFallback, + }); + setDownloadFallback((x) => !x); + }} + /> + + + {/* Notifications status switch */} __MSG_enableNotifications__