-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMaktabkhooneh_Course_Downloader.user.js
122 lines (104 loc) · 4.32 KB
/
Maktabkhooneh_Course_Downloader.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
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
// ==UserScript==
// @name Maktabkhooneh Course Downloader
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Automate the process of grabbing download links from Maktabkhooneh course pages
// @author EVOL-ution
// @match https://maktabkhooneh.org/course/*
// @grant GM_xmlhttpRequest
// ==/UserScript==
(function() {
'use strict';
// Function to convert relative href links to absolute URLs
function toAbsoluteUrl(relativeUrl) {
return "https://maktabkhooneh.org" + relativeUrl;
}
// Function to grab href links and send GET requests
function grabAndSendLinks() {
// Find all elements with the specified class name
var sectionElement = document.querySelector('section.base-chapter__section');
var hrefLinks = [];
if (sectionElement) {
// Find all anchor elements within the section
var anchorElements = sectionElement.querySelectorAll('a');
// Iterate through the anchor elements and collect their href links
anchorElements.forEach(function (element) {
var href = element.getAttribute('href');
if (href) {
var absoluteUrl = toAbsoluteUrl(href);
hrefLinks.push(absoluteUrl);
}
});
}
// Send GET requests for all href links
var downloadLinks = [];
Promise.all(
hrefLinks.map(function(href) {
return new Promise(function(resolve, reject) {
GM_xmlhttpRequest({
method: 'GET',
url: href,
onload: function(response) {
var downloadLink = extractDownloadLink(response.responseText);
if (downloadLink) {
downloadLinks.push(downloadLink);
}
resolve();
},
onerror: function(error) {
reject(error);
}
});
});
})
).then(function() {
displayDownloadLinks(downloadLinks);
});
}
// Function to extract download link from response HTML
function extractDownloadLink(html) {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var downloadDiv = doc.querySelector('div.unit-content--download');
if (downloadDiv) {
var downloadLink = downloadDiv.querySelector('a.button--round');
if (downloadLink) {
return downloadLink.getAttribute('href');
}
}
return null;
}
// Function to display download links in a new page
function displayDownloadLinks(downloadLinks) {
var newPage = window.open('', '_blank');
newPage.document.write('<html><head><title>Download Links</title></head><body><h1>Download Links</h1><ul>');
downloadLinks.forEach(function(link) {
newPage.document.write('<li><a href="' + link + '">' + link + '</a></li>');
});
newPage.document.write('</ul></body></html>');
newPage.document.close();
}
// Function to add the download button
function addDownloadButton() {
var buttonDiv = document.createElement('div');
buttonDiv.classList.add('ml-20', 'cursor-pointer', 'md:text-xl');
var downloadButton = document.createElement('button');
downloadButton.id = 'downloadButton';
downloadButton.classList.add('button--full');
downloadButton.innerText = 'دانلود'; // Change button text here
downloadButton.addEventListener('click', function() {
grabAndSendLinks();
});
buttonDiv.appendChild(downloadButton);
var navbar = document.querySelector('.course-page__navbar-items');
// Insert the button as the first child
navbar.insertBefore(buttonDiv, navbar.firstChild);
}
// Check if the URL matches the course page pattern
if (window.location.pathname.startsWith('/course/')) {
// Wait for the page to be fully loaded using window.onload
window.onload = function() {
addDownloadButton();
};
}
})();