Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * USAGE:
- * fetchComments()
- */
- var copyToClipboard = str => {
- const el = document.createElement('textarea')
- el.value = str
- document.body.appendChild(el)
- el.select()
- document.execCommand('copy')
- document.body.removeChild(el)
- }
- var copyResults = (str, len) => {
- if (str) {
- window.localStorage.setItem('tw-scrapper-results', str)
- const $el = `<div style="background-color: black; color: white; position: fixed; width: 100%; left: 0; bottom: 0; z-index: 10000; text-align: center; padding: 1em;" id="tw-scrapper-container">${len} comentários extraídos <button onClick="copyResults()">Copiar</button></div>`
- const node = new DOMParser().parseFromString($el, 'text/html').body.childNodes[0]
- document.body.appendChild(node)
- } else {
- const results = window.localStorage.getItem('tw-scrapper-results')
- copyToClipboard(results)
- alert('Comentários copiados')
- document.querySelector('#tw-scrapper-container').parentElement.removeChild(document.querySelector('#tw-scrapper-container'))
- }
- }
- function fetchComments(length) {
- let baseSelector = window.document
- const prepareCSV = (data, header) => {
- data.unshift(header)
- const dataCSV = data.map(c => {
- return c.map(v => {
- return v
- ? v
- .replace(/[\n\s]+/g, ' ')
- .replace(/^\s+/, '')
- .replace(/\s+$/, '')
- : ''
- }).join('\t')
- }).join('\n')
- return dataCSV
- }
- const scrapComments = () => {
- console.log('Raspando comentários...')
- const $comments = Array.from(baseSelector.querySelectorAll('.stream-items .tweet'))
- const header = ['timeString', 'name', 'username', 'text', 'likes', 'replies', 'retweets']
- const comments = $comments.map((el) => {
- const name = el.querySelector('.fullname').innerText
- const username = el.querySelector('.username b').innerText
- const timeEl = el.querySelector('.tweet-timestamp')
- const timeString = timeEl.getAttribute('data-original-title') || timeEl.getAttribute('title')
- const timeObj = new Date(parseInt(el.querySelector('.tweet-timestamp span').getAttribute('data-time-ms'), 10))
- const text = el.querySelector('.tweet-text').innerText
- const likes = el.querySelector('.ProfileTweet-action--favorite .ProfileTweet-actionCountForPresentation').innerText || '0'
- const replies = el.querySelector('.ProfileTweet-action--reply .ProfileTweet-actionCountForPresentation').innerText || '0'
- const retweets = el.querySelector('.ProfileTweet-action--retweet .ProfileTweet-actionCountForPresentation').innerText || '0'
- return [ timeString, name, username, text, likes, replies, retweets ]
- })
- const csvComments = prepareCSV(comments, header)
- if (comments.length) {
- copyToClipboard(csvComments)
- copyResults(csvComments, comments.length)
- } else {
- alert('Nenhum comentário encontrado')
- }
- }
- const expandComments = () => {
- }
- // Initialize
- return scrapComments()
- /* if (length) {
- return expandComments()
- .then(scrapComments)
- } else {
- return scrapComments()
- } */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement