-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbackground.js
119 lines (112 loc) · 4.26 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
importScripts('js/storage.js');
const notify = function(title, message) {
return chrome.notifications.create('', {
type: 'basic',
title: title || 'Yape',
message: message || '',
iconUrl: './images/icon.png',
});
}
const loadToastr = function(tab, callback) {
chrome.scripting.insertCSS({
target: {tabId: tab.id},
files: ['css/toastr.min.css']
}, function() {
chrome.scripting.executeScript({
target: {tabId: tab.id},
files: ['js/lib/jquery-3.5.1.min.js', 'js/lib/toastr.min.js']
}, function() {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: () => {
toastr.options = {
closeButton: false,
newestOnTop: false,
progressBar: false,
positionClass: 'toastr-top-right',
containerId: 'toastr-container',
toastClass: 'toastr',
iconClasses: {
error: 'toastr-error',
info: 'toastr-info',
success: 'toastr-success',
warning: 'toastr-warning'
},
iconClass: 'toastr-info',
titleClass: 'toastr-title',
messageClass: 'toastr-message',
closeClass: 'toastr-close-button',
timeOut: 8000
};
}
}, function() {
callback();
});
});
});
}
const sendToast = function(tab, type, message) {
chrome.scripting.executeScript({
target: {tabId: tab.id},
func: (type, message) => {
toastr.remove();
toastr[type](message);
},
args: [type, message]
});
}
const downloadLink = function(info, tab) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 5000)
fetch(`${origin}/api/statusServer`, { signal: controller.signal })
.then(response => response.json())
.then(json => {
clearTimeout(timeoutId);
if (json.hasOwnProperty('error')) {
if (json.error === 'Forbidden') sendToast(tab, 'error', `Invalid credentials, make sure you are logged in`);
else sendToast(tab, 'error', `Server unreachable`);
return;
}
fetch(`${origin}/api/checkURLs?urls=["${encodeURIComponent(info.linkUrl)}"]`)
.then(response => response.json())
.then(json => {
if (json.hasOwnProperty('error')) {
sendToast(tab, 'error', `Error checking url: ${json}`);
return;
}
const safeName = encodeURIComponent(info.linkUrl.replace(/[^a-z0-9._\-]/gi, '_'));
fetch(`${origin}/api/addPackage?name="${safeName}"&links=["${encodeURIComponent(info.linkUrl)}"]`)
.then(response => response.json())
.then(json => {
if (json.hasOwnProperty('error')) {
sendToast(tab, 'error', `Error requesting download: ${json}`);
return;
}
sendToast(tab, 'success', 'Download added successfully');
});
});
})
.catch(e => sendToast(tab, 'error', `Server unreachable`));
}
chrome.runtime.onInstalled.addListener( () => {
chrome.contextMenus.create({
id: 'yape',
title: 'Download with Yape',
contexts:['link']
});
});
chrome.runtime.onMessage.addListener( data => {
if (data.type === 'notification') {
notify(data.title, data.message);
}
});
chrome.contextMenus.onClicked.addListener( ( info, tab ) => {
if ('yape' === info.menuItemId) {
loadToastr(tab, function() {
pullStoredData(function() {
sendToast(tab, 'info', 'Requesting download...');
downloadLink(info, tab);
});
});
}
} );