Varning för fulkod, men något sådant här gör jobbet (vet ej om tillåtet).
Körs i Console i Browsern ifrån relevant tråd som du vill hämta ut. Varning igen: om tråden är lång finns risk för att scriptet fryser eller kraschar.
Koden öppnar en modal där alla poster inryms i ordning. Utskriftsfunktionen är inte funktionsduglig ännu.
Kod:
function showModal(htmlContent) {
const overlay = document.createElement('div');
overlay.style.position = 'fixed';
overlay.style.top = '0';
overlay.style.left = '0';
overlay.style.right = '0';
overlay.style.bottom = '0';
overlay.style.backgroundColor = 'rgba(0,0,0,0.5)';
overlay.style.display = 'flex';
overlay.style.alignItems = 'center';
overlay.style.justifyContent = 'center';
overlay.style.zIndex = '9999';
const modal = document.createElement('div');
modal.style.backgroundColor = 'white';
modal.style.width = '90%';
modal.style.height = '90%';
modal.style.overflow = 'auto';
modal.style.padding = '1em';
modal.style.position = 'relative';
const printButton = document.createElement('button');
printButton.innerHTML = '🖨 Print';
printButton.style.position = 'absolute';
printButton.style.top = '10px';
printButton.style.right = '10px';
printButton.style.padding = '10px';
printButton.style.fontSize = '16px';
printButton.style.backgroundColor = '#4CAF50';
printButton.style.color = 'white';
printButton.style.border = 'none';
printButton.style.borderRadius = '5px';
printButton.style.cursor = 'pointer';
printButton.setAttribute('onclick', 'printModalContent(modal)');
modal.appendChild(printButton);
modal.innerHTML += htmlContent;
overlay.appendChild(modal);
overlay.addEventListener('click', (event) => {
if (event.target === overlay) {
document.body.removeChild(overlay);
}
});
document.body.appendChild(overlay);
}
function printModalContent(modal) {
const printWindow = window.open('', '', 'width=800,height=600');
const modalContent = modal.innerHTML.replace(/<button[^>]*>.*?<\/button>/, '');
printWindow.document.write(`
<html>
<head>
<title>Print</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
}
.modal-content {
padding: 20px;
background-color: white;
}
</style>
</head>
<body>
<div class="modal-content">
${modalContent} <!-- Insert the modal content here -->
</div>
</body>
</html>
`);
printWindow.document.close();
setTimeout(() => {
printWindow.print();
}, 500);
}
async function retreiveURL(url) {
const r = await fetch(url);
if (!r.ok) {
throw new Error(`Network error: ${r.status} ${r.statusText}`);
}
const arrayBuffer = await r.arrayBuffer();
const decoder = new TextDecoder('iso-8859-1');
const decodedText = decoder.decode(arrayBuffer);
return decodedText;
}
function queryFromHTML(htmlString, selector) {
const parser = new DOMParser();
const doc = parser.parseFromString(htmlString, 'text/html');
return doc.querySelectorAll(selector);
}
function retrievePosts(nodelist) {
let h = "";
nodelist.forEach(node => h += node.innerHTML);
return h;
}
const pageUrls = [];
const postIds = [];
var postHTML = "";
let currUrl = document.URL;
let pParts = currUrl.split("p");
if (pParts>2)
{
let nil = pParts.pop();
}
let url= pParts.join("p");
let nextPage = true;
let pageNum = 1;
while (nextPage) {
pageUrls.push(url);
let d = await retreiveURL(url);
let nx = queryFromHTML(d,"li.next a");
if (nx[0] !== undefined)
url = nx[0].href;
else
nextPage = false;
let postNodes = queryFromHTML(d,"div.post");
postHTML += retrievePosts(postNodes);
console.info("Parsing page "+pageNum++);
}
showModal(postHTML);