-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
61 lines (55 loc) · 1.88 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
import { isSupportedSite } from './utils.js';
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.get(['apiUrl'], (data) => {
if (!data.apiUrl) {
chrome.runtime.openOptionsPage();
}
});
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status === 'complete' && tab.url) {
if (isSupportedSite(tab.url)) {
chrome.action.setTitle({ title: "视频下载助手 - 支持的网站", tabId: tabId });
chrome.action.setBadgeText({ text: "✓", tabId: tabId });
chrome.action.setBadgeBackgroundColor({ color: "#4CAF50", tabId: tabId });
} else {
chrome.action.setTitle({ title: "视频下载助手 - 不支持的网站", tabId: tabId });
chrome.action.setBadgeText({ text: "", tabId: tabId });
}
}
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.action === 'fetchVideoLink') {
chrome.storage.sync.get(['apiUrl'], async (data) => {
if (!data.apiUrl) {
sendResponse({ error: '请先配置 API 地址' });
return;
}
try {
const response = await fetch(data.apiUrl, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: request.url,
videoQuality: request.format,
audioFormat: request.audioFormat,
audioBitrate: request.audioBitrate,
}),
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
console.log('API result:', result);
sendResponse(result);
} catch (error) {
console.error('Fetch error:', error);
sendResponse({ error: error.message });
}
});
return true;
}
});