Last active
February 16, 2024 10:47
-
-
Save Sn0wCrack/a0b0872016457433cc4354fe0c2fe036 to your computer and use it in GitHub Desktop.
CDR Insert Links 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name CDR insert links 2 | |
// @namespace http://tampermonkey.net/ | |
// @version 2024-02-12_v3 | |
// @description inject external download links on cdromance | |
// @author iamevn | |
// @match https://cdromance.com/* | |
// @grant GM.xmlHttpRequest | |
// @connect cdromance.org | |
// @connect cdromance.com | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
const automaticlyFetchDownlaods = false; | |
const ids = { | |
style: 'downloadStyle', | |
details: 'downloadDetails', | |
queryButton: 'downloadQuery', | |
}; | |
const addStyle = () => { | |
const styleText = ` | |
<style id="${ids.style}"> | |
#${ids.details} { | |
margin-bottom: 20px; | |
} | |
#${ids.details} > summary { | |
display: list-item; | |
} | |
#${ids.queryButton} { | |
margin-bottom: 20px; | |
} | |
/* taken from cdromance.org's style */ | |
.download-links.table { | |
display: table; | |
width: 100%; | |
border-collapse: collapse; | |
background-color: #2a2a2a; /* Dark shade for tables */ | |
color: #c0c0c0; /* Light grey text for readability */ | |
font-family: sans-serif; | |
} | |
.download-links .tr { | |
display: table-row | |
} | |
.download-links .th, .td { | |
display: table-cell; | |
border: 1px solid #3f3f3f; | |
padding: 5px; | |
height: 25px; | |
vertical-align: middle; | |
text-align: center; | |
color: #d9d9d9; | |
} | |
.download-links a { | |
color: #4fc3f7; /* Bright color for links to stand out */ | |
} | |
.download-links a:hover { | |
color: #4fc3f7; /* Distinguish visited links with a different shade */ | |
} | |
.download-links a:visited { | |
color: #b085f5; /* Distinguish visited links with a different shade */ | |
} | |
</style>`; | |
if (!document.getElementById(ids.style)) { | |
document.head.insertAdjacentHTML('beforeend', styleText); | |
}; | |
}; | |
const getTicketIdFromPostId = (postId) => { | |
return new Promise((resolve, reject) => { | |
GM.xmlHttpRequest({ | |
data: `action=obfuscate_post_id&post_id=${postId}`, | |
method: 'POST', | |
responseType: 'document', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
url: 'https://cdromance.com/wp-admin/admin-ajax.php', | |
onload: (response) => { | |
resolve(response.responseText.trim()); | |
}, | |
onerror: (response) => { | |
reject(); | |
} | |
}); | |
}) | |
} | |
const getDownloads = (parentElement, ticketId) => { | |
const onTicketResponse = (response) => { | |
const parser = new DOMParser(); | |
const resHtml = parser.parseFromString(response.responseText, 'text/html'); | |
const resContent = resHtml.querySelector('#contentArea'); | |
if (resContent) { | |
if (document.getElementById(ids.details)) { | |
const details = document.getElementById(ids.details); | |
details.innerHTML = resContent.outerHTML; | |
details.insertAdjacentHTML('afterbegin', '<summary>External Links</summary>'); | |
} else { | |
// add open details with the table and add the style | |
const details = document.createElement('details') | |
details.id = ids.details; | |
details.toggleAttribute('open', true); | |
details.innerHTML = resContent.outerHTML; | |
details.insertAdjacentHTML('afterbegin', '<summary>External Links</summary>'); | |
parentElement.appendChild(details); | |
} | |
} | |
}; | |
console.debug('ticket', ticketId); | |
GM.xmlHttpRequest({ | |
data: `cdrTicketInput=${ticketId}`, | |
method: 'POST', | |
responseType: 'document', | |
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }, | |
url: 'https://cdromance.org', | |
onload: onTicketResponse, | |
}); | |
}; | |
const addButton = (parentElement, ticketId) => { | |
if (document.getElementById(ids.queryButton)) { | |
return document.getElementById(ids.queryButton); | |
} else { | |
const input = document.createElement('input'); | |
input.id = ids.queryButton; | |
input.type = 'button'; | |
input.value = 'Find downloads'; | |
input.onclick = () => getDownloads(parentElement, ticketId); | |
parentElement.appendChild(input); | |
return input; | |
} | |
}; | |
const downloadsElement = document.getElementById('download'); | |
if (!downloadsElement) { | |
return; | |
} | |
downloadsElement.querySelectorAll('br') | |
.forEach((element) => element.remove()); | |
const scriptTag = document.getElementById('cdr-ticket-js-extra'); | |
const matches = scriptTag.innerText.match(/var ajax_params = (?<json>{.*});/); | |
if (!matches.groups?.json) { | |
return; | |
} | |
const data = JSON.parse(matches.groups.json); | |
getTicketIdFromPostId(data.post_id) | |
.then((ticketId) => { | |
addStyle(); | |
if (automaticlyFetchDownlaods) { | |
getDownloads(downloadsElement, ticketId); | |
} else { | |
addButton(downloadsElement, ticketId); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment