-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
62 lines (58 loc) · 1.44 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { uploadToHalo, fetchImage } from './utils/index.mjs';
let token;
let domain;
let slugify;
async function genericOnClick(info) {
chrome.storage.sync.get(
{
haloToken: '',
haloDomain: '',
haloSlugify: '',
},
async function (items) {
token = items.haloToken;
domain = items.haloDomain;
slugify = items.haloSlugify;
if (!token || !domain) {
chrome.notifications.create({
type: 'basic',
title: 'Halo',
message: '请先填写Halo的Token和域名',
iconUrl: 'icon.png',
});
return;
}
let formData;
try {
formData = await fetchImage(info, slugify);
} catch (e) {
chrome.notifications.create({
type: 'basic',
title: 'Halo',
message: '获取图片失败,fetch不支持改url',
iconUrl: 'icon.png',
});
return;
}
try {
await uploadToHalo(formData, domain, token);
} catch (e) {
chrome.notifications.create({
type: 'basic',
title: 'Halo',
message: '上传图片失败',
iconUrl: 'icon.png',
});
return;
}
}
);
}
chrome.contextMenus.onClicked.addListener(genericOnClick);
chrome.runtime.onInstalled.addListener(async function () {
chrome.contextMenus.create({
title: '上传到我的Halo附件',
contexts: ['image'],
id: '1',
});
});