Skip to content

Instantly share code, notes, and snippets.

@BriSeven
Last active October 6, 2023 15:15
Show Gist options
  • Save BriSeven/a829d7aad3bd93fd68a3e37730a2d11f to your computer and use it in GitHub Desktop.
Save BriSeven/a829d7aad3bd93fd68a3e37730a2d11f to your computer and use it in GitHub Desktop.
HTMLElement.prototype.$ = function (selector) { return this.querySelectorAll(selector) }
HTMLElement.prototype.attr = function (selector) { return this.getAttribute(selector) }
NodeList.prototype.map = Array.prototype.map
function htmlToText (node) {
if(node.childNodes.length > 1) {
return node.childNodes.map(htmlToText).join('');
}
switch(node.tagName) {
case 'P':
return '<p>'+node.textContent+"</p>"
break;
case 'IMG':
return node.getAttribute('alt')
break;
case 'SPAN':
return node.textContent
break;
case 'A':
return node.textContent
break;
case 'BR':
return '\n'
break;
default:
return node.textContent
}
}
copy($$('.detailed-status, .status').map(x=>( u = x.$(' a.detailed-status__display-name, a.status__display-name'),{
id: x.$(".status__relative-time,.detailed-status__datetime")[0]?.attr('href') ,
dateTime: x.$("time, .status__relative-time time")[0]?.attr("datetime") || new Date(x.$(".detailed-status__datetime")[0].textContent).toISOString(),
title: x.$('.status__content--with-spoiler .translate')[0]?.textContent,
content: x.$(".status__content__text")[0].childNodes.map(htmlToText).join('').replaceAll("</p><p>","\n\n").replaceAll(/^<p>/g,'').replaceAll(/<\/p>$/g,'').replaceAll("<p>","\n\n").replaceAll("</p>","\n\n"),
mediaGallery: x.$(".media-gallery a").map(x=>({href:x.attr('href'),alt:x.querySelector('img').attr('alt') })),
user: {
href: u[0]?.attr('href'),
name: u[0]?.$('.display-name__html')[0].innerHTML,
handle: u[0]?.$('.display-name__account')[0].innerHTML
}
})).map((x)=>{
return `
${x.title ? `title: ${x.title}` : "" }
user: ${x.user.handle}
timeStamp: ${x.dateTime}
id: ${x.id}
---
${x.content}
${x.mediaGallery.map(x=>`[${x.alt}](${x.href})`)}`
}).join('')
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment