var articles = Array.from(document.querySelectorAll('article'))
var structured = articles.reduce((acc, key, pos) => {
var object = {
postId: key.id,
postNumber: key.children[0].innerText,
postbody: {
time: '',
tripcode: '',
post: '',
userId: '',
postLink: '',
links: [],
response: false,
responseBody: {
postId: '',
postLink: '',
time: '',
tricode: '',
userId: '',
links: [],
post: ''
}
}
}
Array.from(key.children).map((child, pos) => {
if (child.tagName === 'A') {
object.postbody.links.push(child.href)
}
if (child.tagName === 'HEADER') {
//headers
Array.from(child.children).map((header, p) => {
if (header.tagName === 'SMALL') {
// loop through small
Array.from(header.children).map((small, pos) => {
if (small.tagName === 'TIME') {
// Object.assign({}, object, { postbody.time : small.dateTime })
object.postbody.time = small.dateTime
}
if (small.className === 'trip') {
object.postbody.tripcode = small.innerText
}
if (small.className === 'userid') {
object.postbody.userId = small.innerText
}
if (small.tagName === 'A') {
object.postbody.postLink = small.href
}
})
}
})
}
if (child.tagName === 'DIV') {
var text = ''
Array.from(child.childNodes).map((div, p) => {
var msg = div.nodeName !== '#text'? div.innerHTML : div.nodeValue
if (div.nodeName === 'BR') {
msg = ', '
}
text += `${msg}`
})
object.postbody.post = text
}
if (child.tagName === 'BLOCKQUOTE') {
Array.from(child.children).map((elem) => {
// header inside of BlockQuote
if (elem.tagName === 'HEADER') {
//headers
Array.from(elem.children).map((header, p) => {
if (header.tagName === 'SMALL') {
// loop through small
Array.from(header.children).map((small, pos) => {
if (small.tagName === 'TIME') {
// Object.assign({}, object, { postbody.time : small.dateTime })
object.postbody.responseBody.time = small.dateTime
}
if (small.className === 'trip') {
object.postbody.responseBody.tripcode = small.innerText
}
if (small.className === 'userid') {
object.postbody.responseBody.userId = small.innerText
}
})
}
})
}
if (elem.tagName === 'A') {
// object.postbody.responseBody.imageLink
// var link = elem.href.match(/png|jpg|html|pdf/g)
object.postbody.responseBody.links.push(elem.href)
}
var textBlock = ''
if (elem.tagName === 'DIV') {
Array.from(child.childNodes).map((div) => {
if (div.tagName !== 'SMALL') {
//console.log(kid)
var msgBlock = div.nodeName !== '#text'? div.innerHTML : div.nodeValue
if (div.nodeName === 'BR') {
msgBlock = ', '
}
textBlock += `${msgBlock}`
}
})// end of array.map() loop
object.postbody.responseBody.post = textBlock
}
})
}
})
if (key) {
acc.push(object)
}
return acc
}, [])
var massaged = structured.reduce((acc, key, pos) => {
if (key) {
var changed = key.postbody.post.replace('span style=\"color:#bd0f0f\">>>',).replace('</span',).replace(/(?<=\<)(.*?)(?=\>)/,).replace('<>, ',)
key.postbody.post = changed
acc.push(Object.assign({}, key))
}
return acc
}, [])
(Self-doxxing. Leave the name and email field blank. This is for your safety and anonymity.) -BO