Skip to content

Commit

Permalink
feat: Recommend Aria2 Manager if start Aria2 failed
Browse files Browse the repository at this point in the history
  • Loading branch information
alexhua committed Dec 16, 2023
1 parent 0673d36 commit f42b8b2
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 17 deletions.
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "You should determine whether to delete the related website cookies when asking download settings."
},
"Aria2ManagerPromotionDes":{
"message": "It seems Aria2 Manager is not installed, access \"Alex Recommends\" channel for more information?"
}
}
3 changes: 3 additions & 0 deletions _locales/ja/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "ダウンロード設定を尋ねる際に、関連するウェブサイトのクッキーを削除するかどうかを決定する必要があります。"
},
"Aria2ManagerPromotionDes":{
"message": "Aria2 Managerはインストールされていないようですが、詳細については、Alex推奨チャネルにアクセスしてください?"
}
}
3 changes: 3 additions & 0 deletions _locales/ko/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "다운로드 설정을 묻을 때 관련 웹 사이트 쿠키를 삭제할지 여부를 결정해야 합니다."
},
"Aria2ManagerPromotionDes":{
"message": "Aria2 Manager가 설치되어 있지 않은 것 같습니다.\"Alex 추천\"채널을 방문하여 자세한 내용을 확인하시겠습니까?"
}
}
3 changes: 3 additions & 0 deletions _locales/uk/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "Вам слід визначити, чи ви хочете видалити файли cookie пов'язаного веб-сайту при налаштуванні завантаження."
},
"Aria2ManagerPromotionDes":{
"message": "Здається, Aria2 Manager не встановлено, звертайтеся до каналу \"Alex Recommends\" для отримання додаткової інформації?"
}
}
3 changes: 3 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "在询问下载设置时,你需要决定是否删除相关网站的Cookies信息。"
},
"Aria2ManagerPromotionDes":{
"message": "好像没有安装Aria2 Manager,访问“Alex推荐”频道了解更多信息?"
}
}
3 changes: 3 additions & 0 deletions _locales/zh_TW/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -250,5 +250,8 @@
},
"ManualDownloadCookiesTooltipDes":{
"message": "在詢問下載設定時,你需要决定是否删除相關網站的Cookies資訊。"
},
"Aria2ManagerPromotionDes":{
"message": "好像沒有安裝Aria2 Manager,訪問“Alex推薦”頻道瞭解更多資訊?"
}
}
9 changes: 9 additions & 0 deletions aria2.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html lang="en">

<head>
<title>Start Aria2</title>
<script src="js/startAria2.js" type="module"></script>
</head>

</html>
40 changes: 23 additions & 17 deletions background.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import Configs from "./js/config.js";
import Aria2 from "./js/aria2.js";
import Aria2Options from "./js/aria2Options.js";

const NID_DEFAULT = "NID_DEFAULT";
const NID_TASK_NEW = "NID_TASK_NEW";
const NID_TASK_STOPPED = "NID_TASK_STOPPED";

var CurrentTabUrl = "about:blank";
var MonitorId = -1;
var MonitorRate = 3000; // Aria2 monitor interval 3000ms
var RemoteAria2List = [];

const NID_DEFAULT = "NID_DEFAULT";
const NID_TASK_NEW = "NID_TASK_NEW";
const NID_TASK_STOPPED = "NID_TASK_STOPPED";

const isDownloadListened = () => chrome.downloads.onDeterminingFilename.hasListener(captureDownload);

Expand Down Expand Up @@ -300,12 +301,12 @@ async function captureDownload(downloadItem, suggest) {

async function launchUI(info) {
const index = chrome.runtime.getURL('ui/ariang/index.html');
let webUiUrl = index; // launched from notification, option menu or browser toolbar icon
let webUiUrl = index + '#!'; // launched from notification, option menu or browser toolbar icon

/* assemble the final web ui url */
if (info?.hasOwnProperty("filename") && info.url) { // launched for new task
const downloadItem = info;
webUiUrl = index + "#!/new?url=" + encodeURIComponent(btoa(encodeURI(downloadItem.url)));
webUiUrl = webUiUrl + "/new?url=" + encodeURIComponent(btoa(encodeURI(downloadItem.url)));
if (downloadItem.referrer && downloadItem.referrer != "" && downloadItem.referrer != "about:blank") {
webUiUrl = webUiUrl + "&referer=" + encodeURIComponent(downloadItem.referrer);
}
Expand All @@ -320,7 +321,7 @@ async function launchUI(info) {
}
} else if (typeof info === "string" && info.startsWith(NID_TASK_STOPPED)) { // launched from task done notification click
const gid = info.slice(NID_TASK_STOPPED.length) || '';
webUiUrl += gid ? "#!/task/detail/" + gid : "#!/stopped";
webUiUrl += gid ? "/task/detail/" + gid : "/stopped";
}

chrome.tabs.query({ "url": index }).then(function (tabs) {
Expand Down Expand Up @@ -511,7 +512,8 @@ function onMenuClick(info, tab) {
if (info.menuItemId == "MENU_OPEN_WEB_UI") {
launchUI();
} else if (info.menuItemId == "MENU_START_ARIA2") {
chrome.tabs.create({ url: "aria2://start/" });
const url = chrome.runtime.getURL('aria2.html')
chrome.tabs.create({ url });
} else if (info.menuItemId == "MENU_CAPTURE_DOWNLOAD") {
chrome.storage.local.set({ integration: info.checked });
} else if (info.menuItemId == "MENU_MONITOR_ARIA2") {
Expand Down Expand Up @@ -737,7 +739,6 @@ function registerAllListeners() {
CurrentTabUrl = tab?.url || "about:blank";
updateOptionMenu(tab);
});

});

chrome.windows.onFocusChanged.addListener(function (windowId) {
Expand Down Expand Up @@ -770,14 +771,14 @@ function registerAllListeners() {
url: optionsUrl
});
} else if (details.reason == "update") {
chrome.storage.local.get("rpcList").then(configs => {
if (!configs.rpcList) {
/* Old local storage should be upgraded to Chrome storage */
chrome.tabs.create({
url: optionsUrl + "?action=upgrade-storage"
});
}
})
// chrome.storage.local.get("rpcList").then(configs => {
// if (!configs.rpcList) {
// /* Old local storage should be upgraded to Chrome storage */
// chrome.tabs.create({
// url: optionsUrl + "?action=upgrade-storage"
// });
// }
// })
/* new version update notification */
let title = `Version ${manifest.version} 🚀`;
let message = `${manifest.name} has been updated.`;
Expand All @@ -799,7 +800,7 @@ function registerAllListeners() {

/* receive download request from magnet page, export all, ariaNG */
chrome.runtime.onMessage.addListener(
function (message) {
function (message, sender, sendResponse) {
switch (message.type) {
case "DOWNLOAD":
case "EXPORT_ALL":
Expand All @@ -810,6 +811,11 @@ function registerAllListeners() {
downloadItem.type = "DOWNLOAD_VIA_BROWSER";
download(downloadItem);
break;
case "QUERY_WINDOW_STATE":
chrome.windows.get(message.data).then(
(window) => { sendResponse({ data: window }) }
);
return true;
}
}
);
Expand Down
44 changes: 44 additions & 0 deletions js/startAria2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const SCHEME_ARIA2_START = "aria2://start/";
const PROMOTION_PAGE = chrome.runtime.getURL('ui/ariang/index.html') + "#!recommend?path=/posts/3#usage";

var focused = true;
var confirmed = false;

window.addEventListener('blur', function () {
focused = false;
console.log('Web page is blurred');
chrome.runtime.sendMessage({ type: 'QUERY_WINDOW_STATE', data: chrome.windows.WINDOW_ID_CURRENT }).then(
(response) => {
if (response && response.data) {
let currentWindow = response.data;
console.log('Current window = %d, focus =', currentWindow.id, currentWindow.focused);
if (!currentWindow.focused) {
console.log('Chrome is blurred, Web page will close.');
window.close();
}
}
}
)
});

window.addEventListener('focus', function () {
focused = true;
console.log('Web page is focused, if not confirmed, it will close. confirmed = ', confirmed);
!confirmed && window.close();
});

window.location.href = SCHEME_ARIA2_START;

setTimeout(() => {
if (focused) {
let des = chrome.i18n.getMessage("Aria2ManagerPromotionDes");
if (confirm(des)) {
confirmed = true;
window.location.href = PROMOTION_PAGE;
} else {
console.log('Promotion is canceled, web page will close.')
window.close();
}
}
}, 300);

0 comments on commit f42b8b2

Please sign in to comment.