forked from dimdenGD/cobalt-extension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
93 lines (80 loc) · 2.31 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
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/*",
"*://*.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(
{ instance: 'cobalt.tools' },
(items) => {
chrome.tabs.create({
url: `https://${items.instance}/?u=${tab.url}`
});
}
);
});
chrome.contextMenus.onClicked.addListener((info, tab) => {
if(info.menuItemId === "download-media-from-link") {
chrome.storage.sync.get(
{ instance: 'cobalt.tools' },
(items) => {
chrome.tabs.create({
url: `https://${items.instance}/?u=${info.linkUrl}`
});
}
);
} else if(info.menuItemId === "download-media-from-page") {
chrome.storage.sync.get(
{ instance: 'cobalt.tools' },
(items) => {
chrome.tabs.create({
url: `https://${items.instance}/?u=${tab.url}`
});
}
);
}
});