forked from estellaarrieta/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPL_title_as_torrent_filename.user.js
80 lines (71 loc) · 2.85 KB
/
PL_title_as_torrent_filename.user.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
// ==UserScript==
// @name PL use title as torrent filename
// @namespace http://tampermonkey.net/
// @version 0.3.1
// @description use title as torrent filename
// @updateURL https://github.com/estellaarrieta/userscripts/raw/main/PL_title_as_torrent_filename.user.js
// @author hyper440
// @match https://pornolab.net/forum/viewtopic.php*
// @match https://pornolab.net/forum/viewforum.php*
// @match https://pornolab.net/forum/tracker.php*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const pageType = getPageType();
const links = [document.querySelector('.dl-stub.dl-link'), document.querySelector('.dl-stub img').parentNode];
links.forEach(link => {
// Remove the original click event
link.onclick = null;
// Add a new click event to download the file with the custom filename
link.addEventListener('click', function(e) {
e.preventDefault();
const url = link.href;
const topicTitle = getTopicTitle(link, pageType);
const filteredTopicTitle = topicTitle.replace(/[/\\?%*:|"<>]/g, '-');
const filename = `${filteredTopicTitle}.torrent`;
fetch(url)
.then(response => response.blob())
.then(blob => {
const newLink = document.createElement('a');
newLink.href = URL.createObjectURL(blob);
newLink.download = filename;
newLink.style.display = 'none';
document.body.appendChild(newLink);
newLink.click();
document.body.removeChild(newLink);
})
.catch(err => {
console.error('Error downloading the file:', err);
});
});
});
function getPageType() {
const url = window.location.href;
if (url.includes('viewtopic.php')) {
return 'viewtopic';
} else if (url.includes('viewforum.php')) {
return 'viewforum';
} else if (url.includes('tracker.php')) {
return 'tracker';
}
return '';
}
function getTopicTitle(link, pageType) {
let titleElement;
if (pageType === 'viewtopic') {
titleElement = document.getElementById('topic-title');
} else if (pageType === 'viewforum') {
const rowElement = link.closest('tr');
titleElement = rowElement.querySelector('.torTopic a.torTopic');
} else if (pageType === 'tracker') {
const rowElement = link.closest('tr');
titleElement = rowElement.querySelector('.tLeft a.med.tLink.bold');
}
if (titleElement) {
const titleText = titleElement.textContent;
return titleText;
}
return '';
}
})();