밴드 댓글 가져오기

2022. 11. 18. 오후 6:00:02

As of 2022-11-18. See the latest version.

// ==UserScript==
// @name        밴드 댓글 가져오기
// @namespace   Violentmonkey Scripts
// @match       https://band.us/band/*
// @grant       none
// @version     1.0
// @author      WisdomIT
// @description 2022. 11. 18. 오후 6:00:02
// @license MIT
// ==/UserScript==


const getcommentboolean = false;

const getCommentBtn = document.createElement('a')

getCommentBtn.innerText = '댓글 파싱기능 활성화하기'
getCommentBtn.style.padding = '20px'
getCommentBtn.style.border = '1px solid #000'
getCommentBtn.style.borderRadius = '10px'
getCommentBtn.style.position = 'fixed'
getCommentBtn.style.zIndex = '50000'
getCommentBtn.style.backgroundColor = '#fff'
getCommentBtn.style.fontSize = '20px'
getCommentBtn.style.bottom = '100px'
getCommentBtn.style.right = '10px'
getCommentBtn.style.cursor = 'pointer'

getCommentBtn.addEventListener('click', event => {

  getCommentBtn.style.backgroundColor = 'dodgerblue'
  getCommentBtn.style.color = '#fff'

  const allComments = document.querySelectorAll('.dPostCommentMainView')
  console.log(allComments)

  allComments.forEach(e => {

    e.style.border = '5px solid dodgerblue'
    e.style.backgroundColor = '#eee'
    e.style.position = 'relative'

    const tempBtn = document.createElement('div')

    tempBtn.style.position = 'absolute'
    tempBtn.style.top = '0px'
    tempBtn.style.bottom = '0px'
    tempBtn.style.left = '0px'
    tempBtn.style.right = '0px'
    tempBtn.style.cursor = 'pointer'
    tempBtn.style.zIndex = '50000'

    tempBtn.addEventListener('click', event => {
        console.log(e)
        if(confirm('해당 댓글을 인쇄하시겠습니까?')){
            setTimeout(() => { getAllComments(e) }, 500)
        }
    })

    tempBtn.addEventListener('mouseover', event => {
      tempBtn.style.backgroundColor = 'rgba(255,255,255,0.5)'
    })

    tempBtn.addEventListener('mouseout', event => {
      tempBtn.style.backgroundColor = null
    })

    e.appendChild(tempBtn)
  })

} )

document.body.appendChild(getCommentBtn)


const getAllComments = (thiselem) => {
    //console.log(thiselem)
    const allCommentDivs = thiselem.querySelectorAll('[data-viewname="DCommentView"]')

    let commentArray = []
    allCommentDivs.forEach(e => {
        const name = e.querySelector('strong.name')
        const comment = e.querySelector('p._commentContent')
        const time = e.querySelector('time.time')
        if(name !== null && comment !== null && time !== null){
            const nametext = name.innerText
            const commenttext = comment.innerText
            commentArray.push({
                name: nametext,
                comment: commenttext
            })
        }
    })

    console.log(commentArray)

    let commentToHtml = ''

    commentArray.forEach(e => {
        commentToHtml = commentToHtml + `<div style="margin-bottom: 15px">
            <h3 style="margin: 0px; margin-bottom: 5px;">${e.name}</h3>
            <p style="margin: 0px;">${e.comment.replace(/\n/g, '<br>')}</p>
        </div>`
    })


    let mywindow = window.open('', 'PRINT', 'height=400,width=600');

    mywindow.document.write('<html><head><style>div{page-break-inside:avoid;page-break-after:auto;}</style></head><body>');
    mywindow.document.write(commentToHtml);
    mywindow.document.write('</body></html>');

    mywindow.document.close(); // necessary for IE >= 10
    mywindow.focus(); // necessary for IE >= 10*/

    mywindow.print();
    mywindow.close();
}