-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
59 lines (52 loc) · 1.77 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
// const requestURL = 'http://127.0.0.1:3100/api/downloads?get_peers=false&get_pieces=false&get_files=true';
const api_endpoint = 'http://127.0.0.1:3100/api/downloads?get_peers=false&get_pieces=false&get_files=true';
function log(msg, force) {
if (log.enabled || force) {
console.log('Tribler downloader: ' + msg);
}
}
log.enabled = true;
browser.contextMenus.create({
id: "selected-link-download",
title: "Download Magnet link with Tribler",
});
browser.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === "selected-link-download") {
const selectedText = info.selectionText;
if (selectedText && selectedText.startsWith("magnet:")) {
download(selectedText);
} else {
log('No valid magnet link selected', true);
}
}
});
/**
* Downloads a file from the given URL by sending a PUT request to the local API.
*
* @param {string} url - The magnet URL of the file to be downloaded.
* @returns {void}
*/
function download(selectedText) {
let api_payload = { "anon_hops": 2, "safe_seeding": true, "destination": "/tmp/", "uri": selectedText };
let json_payload = JSON.stringify(api_payload);
// check if json_payload is valid JSON
try {
JSON.parse(json_payload);
} catch (error) {
console.error('Invalid JSON payload: ', error);
return;
}
const options = {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'accept': 'application/json',
'X-Api-Key': 'changeme'
},
body: json_payload
};
fetch(api_endpoint, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
};