Skip to content

Commit 7d4ad2d

Browse files
committed
修复chrome.downloads.download错误捕获
chrome扩展api发生错误无法通过try/catch捕获,必须在api回调函数中访问chrome.runtime.lastError进行获取
1 parent 7790137 commit 7d4ad2d

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/pages/options/routes/script/ScriptEditor.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -242,23 +242,30 @@ function ScriptEditor() {
242242
};
243243
const saveAs = (script: Script, e: editor.IStandaloneCodeEditor) => {
244244
return new Promise<void>((resolve) => {
245-
try {
246-
chrome.downloads.download(
247-
{
248-
url: URL.createObjectURL(
249-
new Blob([e.getValue()], { type: "text/javascript" })
250-
),
251-
saveAs: true, // true直接弹出对话框;false弹出下载选项
252-
filename: `${script.name}.user.js`,
253-
},
254-
() => {
245+
chrome.downloads.download(
246+
{
247+
url: URL.createObjectURL(
248+
new Blob([e.getValue()], { type: "text/javascript" })
249+
),
250+
saveAs: true, // true直接弹出对话框;false弹出下载选项
251+
filename: `${script.name}.user.js`,
252+
},
253+
() => {
254+
/*
255+
chrome扩展api发生错误无法通过try/catch捕获,必须在api回调函数中访问chrome.runtime.lastError进行获取
256+
var chrome.runtime.lastError: chrome.runtime.LastError | undefined
257+
This will be defined during an API method callback if there was an error
258+
*/
259+
if (chrome.runtime.lastError) {
260+
// eslint-disable-next-line no-console
261+
console.log("另存为失败: ", chrome.runtime.lastError);
262+
Message.error(`另存为失败: ${chrome.runtime.lastError.message}`);
263+
} else {
255264
Message.success("另存为成功");
256-
resolve();
257265
}
258-
);
259-
} catch (err) {
260-
Message.error(`另存为失败: ${err}`);
261-
}
266+
resolve();
267+
}
268+
);
262269
});
263270
};
264271
const menu: EditorMenu[] = [

0 commit comments

Comments
 (0)