Skip to content

Commit

Permalink
fix: duplicate context menus
Browse files Browse the repository at this point in the history
  • Loading branch information
krau committed Jul 19, 2024
1 parent 80a2b4c commit 53a8c1f
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ const initStorage = chrome.storage.local.get().then((items) => {
});
});

const sendSuccessNotification = async (message) => {
await chrome.notifications.create({
const sendSuccessNotification = (message) => {
chrome.notifications.create({
type: 'basic',
iconUrl: "icons/icon_48.png",
title: '已创建下载任务',
message: message,
});
}

const sendErrorNotification = async (message) => {
await chrome.notifications.create({
const sendErrorNotification = (message) => {
chrome.notifications.create({
type: 'basic',
iconUrl: "icons/icon_48.png",
title: '创建下载任务失败',
message: message,
});
}


chrome.storage.onChanged.addListener((changes) => {
if (changes.host) {
Settings.host = changes.host.newValue;
Expand Down Expand Up @@ -81,22 +82,22 @@ chrome.downloads.onDeterminingFilename.addListener(async function (item) {
}
});
if (Settings.enableNotification) {
await sendSuccessNotification('文件大小: ' + (item.fileSize / (1024 * 1024)).toFixed(2) + 'MB');
sendSuccessNotification('文件大小: ' + (item.fileSize / (1024 * 1024)).toFixed(2) + 'MB');
}
} catch (error) {
await sendErrorNotification('错误信息: ' + error.message);
sendErrorNotification('错误信息: ' + error.message);
}
});

await chrome.contextMenus.removeAll();
chrome.contextMenus.create({
id: Math.random().toString(),
id: "createTask",
title: '使用Gopeed下载',
contexts: ['link', 'image', 'video', 'audio'],
});

chrome.contextMenus.onClicked.addListener(async function (info, tab) {
await initStorage;
console.log(info);
try {
let downloadUrl = info.linkUrl || info.srcUrl || info.frameUrl;
if (info.mediaType) {
Expand Down Expand Up @@ -131,9 +132,9 @@ chrome.contextMenus.onClicked.addListener(async function (info, tab) {
text: '',
});
if (Settings.enableNotification) {
await sendSuccessNotification('文件大小: ' + (resolveResult.res.files[0].size / (1024 * 1024)).toFixed(2) + 'MB');
sendSuccessNotification('文件大小: ' + (resolveResult.res.files[0].size / (1024 * 1024)).toFixed(2) + 'MB');
}
} catch (error) {
await sendErrorNotification('错误信息: ' + error.message);
sendErrorNotification('错误信息: ' + error.message);
}
});

0 comments on commit 53a8c1f

Please sign in to comment.