Skip to content

Commit 4855fec

Browse files
committed
🐛 修复并发创建菜单的报错 #580
1 parent 9ced958 commit 4855fec

1 file changed

Lines changed: 12 additions & 17 deletions

File tree

src/app/service/service_worker/popup.ts

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export class PopupService {
110110
menu.options = message.options;
111111
}
112112
}
113-
this.updateScriptMenu();
113+
await this.updateScriptMenu();
114114
return data;
115115
});
116116
}
@@ -122,27 +122,22 @@ export class PopupService {
122122
if (script) {
123123
script.menus = script.menus.filter((item) => item.id !== id);
124124
}
125-
this.updateScriptMenu();
125+
await this.updateScriptMenu();
126126
return data;
127127
});
128128
}
129129

130-
updateScriptMenu() {
130+
async updateScriptMenu() {
131131
// 获取当前页面并更新菜单
132-
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
133-
const lastError = chrome.runtime.lastError;
134-
if (lastError) {
135-
console.error("chrome.runtime.lastError in chrome.tabs.query:", lastError);
136-
// 无法获取当前页面
137-
return;
138-
}
139-
if (!tabs.length) {
140-
return;
141-
}
142-
const tab = tabs[0];
143-
// 生成菜单
144-
tab.id && this.genScriptMenu(tab.id);
145-
});
132+
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
133+
if (!tabs.length) {
134+
return;
135+
}
136+
const tab = tabs[0];
137+
// 生成菜单
138+
if (tab.id) {
139+
await this.genScriptMenu(tab.id);
140+
}
146141
}
147142

148143
scriptToMenu(script: Script): ScriptMenu {

0 commit comments

Comments
 (0)