-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchlinks.js
More file actions
122 lines (106 loc) · 4.55 KB
/
fetchlinks.js
File metadata and controls
122 lines (106 loc) · 4.55 KB
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
(function () {
'use strict';
// Function to get the post ID from the DOM
function getPostID() {
const postIDElement = document.querySelector('#mypostid');
console.log(postIDElement);
return postIDElement ? postIDElement.value : null;
}
// Function to fetch the real URL for a masked link
async function fetchRealURL(maskedURL) {
try {
const response = await fetch(maskedURL, {
method: 'HEAD',
mode: 'cors',
headers: {
'Referrer': document.referrer // Set the Referrer header to the current document's referrer
}
});
return response.url; // The actual URL after redirection
} catch (error) {
console.error('Error fetching real URL:', error);
return null;
}
}
// Function to replace the masked links with the real ones
async function replaceMaskedLinks() {
const mypostid = getPostID();
console.log(mypostid);
if (!mypostid) {
console.error('Post ID not found');
return;
}
try {
const response = await fetch("https://www.novelupdates.com/wp-admin/admin-ajax.php", {
"credentials": "include",
"headers": {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:131.0) Gecko/20100101 Firefox/131.0",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With": "XMLHttpRequest",
"Sec-GPC": "1",
"Sec-Fetch-Dest": "empty",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Site": "same-origin",
},
"referrer": window.location.href, // Set referrer to the current page URL
"body": `action=nd_getchapters&mygrr=0&mypostid=${mypostid}`,
"method": "POST",
"mode": "cors"
});
const responseText = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(responseText, 'text/html');
const links = doc.querySelectorAll('a[data-id]'); // Select masked links within the response document
console.log(links);
// Replace the masked links in the original document
for (let link of links) {
const maskedURL = link.getAttribute('href');
const realURL = await fetchRealURL(maskedURL);
if (realURL) {
const originalLink = document.querySelector(`a[data-id="${link.getAttribute('data-id')}"]`);
if (originalLink) {
originalLink.setAttribute('href', realURL); // Replace with real URL
}
}
// Delay to avoid overloading the server
await new Promise(resolve => setTimeout(resolve, 100)); // Adjust the delay as needed
}
} catch (error) {
console.error('Error fetching or processing data:', error);
}
}
// Async function to observe the DOM for the target element
async function observeDOM(targetId, callback, parentClass) {
const parentElement = document.querySelector(parentClass);
if (!parentElement) return;
let targetElement = document.getElementById(targetId);
if (targetElement) {
callback(targetElement);
return;
}
const observer = new MutationObserver((mutations) => {
for (const mutation of mutations) {
if (mutation.addedNodes.length) {
targetElement = document.getElementById(targetId);
if (targetElement) {
callback(targetElement);
observer.disconnect(); // Stop observing once the element is found
break;
}
}
}
});
observer.observe(parentElement, { childList: true, subtree: true });
// Wait until the target element is found
while (!targetElement) {
await new Promise(resolve => setTimeout(resolve, 100)); // Polling interval
targetElement = document.getElementById(targetId);
}
}
// Main function to initialize the script
function init() {
observeDOM('mypostid', replaceMaskedLinks, '.l-main'); // Using the parent class
}
// Run the main function
init();
})();