-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbackground.js
147 lines (132 loc) · 3.94 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const patterns = [
"*://*.bilibili.com/video/*",
"*://*.instagram.com/p/*",
"*://*.instagram.com/reels/*",
"*://*.instagram.com/reel/*",
"*://*.twitter.com/*/status/*",
"*://*.twitter.com/*/status/*/video/*",
"*://*.twitter.com/i/spaces/*",
"*://*.x.com/*/status/*",
"*://*.x.com/*/status/*/video/*",
"*://*.x.com/i/spaces/*",
"*://*.reddit.com/r/*/comments/*/*",
"*://*.soundcloud.com/*",
"*://*.soundcloud.app.goo.gl/*",
"*://*.tumblr.com/post/*",
"*://*.tumblr.com/*/*",
"*://*.tumblr.com/*/*/*",
"*://*.tumblr.com/blog/view/*/*",
"*://*.tiktok.com/*",
"*://*.vimeo.com/*",
"*://*.youtube.com/watch?*",
"*://*.youtu.be/*",
"*://*.youtube.com/shorts/*",
"*://*.vk.com/video*",
"*://*.vk.com/*?w=wall*",
"*://*.vk.com/clip*",
"*://*.vine.co/*",
"*://*.streamable.com/*",
"*://*.pinterest.com/pin/*"
];
chrome.contextMenus.create({
title: "download media from link",
contexts: ["link"],
id: "download-media-from-link",
targetUrlPatterns: patterns
});
chrome.contextMenus.create({
title: "download media from this page",
contexts: ["page"],
id: "download-media-from-page",
documentUrlPatterns: patterns
});
chrome.action.onClicked.addListener(tab => {
let matched = false;
for(let pattern of patterns) {
if(tab.url.includes(pattern.split("*://*.")[1].split("/")[0])) {
matched = true;
break;
}
}
if(!matched) {
return;
}
chrome.storage.sync.get(
{ apiurl: 'api.cobalt.tools' },
(items) => {
downloadItem(items.apiurl, tab.url);
}
);
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if(info.menuItemId === "download-media-from-link") {
chrome.storage.sync.get(
{ apiurl: 'api.cobalt.tools' },
(items) => {
downloadItem(items.apiurl, info.linkUrl)
}
);
} else if(info.menuItemId === "download-media-from-page") {
chrome.storage.sync.get(
{ apiurl: 'api.cobalt.tools' },
(items) => {
downloadItem(items.apiurl, tab.url);
}
);
}
});
async function downloadItem(apiUrl, targetUrl) {
try {
const v = await getApiVersion(apiUrl);
const fetchFn = v?.startsWith?.('7.') ? fetchv7 : fetchv10;
const res = await fetchFn(apiUrl, targetUrl);
const json = await res.json();
if(!json.url) throw new Error("no url");
chrome.tabs.create({url: json.url});
} catch(e) {
let au = apiUrl;
if(!au.startsWith("https://") && !au.startsWith("http://")) au = `https://` + au;
au = au.replace("https://api.", "https://").replace("http://api.", "http://");
if(!au.endsWith("/")) au += '/';
au += `?u=${targetUrl}`;
chrome.tabs.create({url: au });
console.error(e);
}
}
async function getApiVersion(url) {
const res = await fetch(`https://${url}/api/serverInfo`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
});
const info = await res.json();
return info.cobalt !== undefined ? info.cobalt.version : info.version;
}
function fetchv10(url, targetUrl) {
return fetch(`https://${url}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
url: targetUrl,
filenameStyle: 'pretty'
})
});
}
function fetchv7(url, targetUrl) {
return fetch(`https://${url}/api/json`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
body: JSON.stringify({
url: targetUrl,
filenamePattern: 'pretty'
})
});
}