SHARE
TWEET

Untitled

a guest Apr 25th, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * @postId STRING: id do elemento da publicação (opcional)
  3.  * @expand BOOL: opção de expandir todos os comentários antes de raspar
  4.  */
  5. var copyToClipboard = str => {
  6.   const el = document.createElement('textarea')
  7.   el.value = str
  8.   document.body.appendChild(el)
  9.   el.select()
  10.   document.execCommand('copy')
  11.   document.body.removeChild(el)
  12. }
  13.  
  14. var copyResults = (str, len) => {
  15.   if (str) {
  16.     window.localStorage.setItem('fb-scrapper-results', str)
  17.     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="fb-scrapper-container">${len} comentários extraídos <button onClick="copyResults()">Copiar</button></div>`
  18.     const node = new DOMParser().parseFromString($el, 'text/html').body.childNodes[0]
  19.     document.body.appendChild(node)
  20.   } else {
  21.     const results = window.localStorage.getItem('fb-scrapper-results')
  22.     copyToClipboard(results)
  23.     alert('Comentários copiados')
  24.     document.querySelector('#fb-scrapper-container').parentElement.removeChild(document.querySelector('#fb-scrapper-container'))
  25.   }
  26. }
  27. function fetchComments(length) {
  28.  
  29.   let baseSelector = document.querySelector('#pagelet_timeline_main_column .permalinkPost')
  30.     || document.querySelectorAll('.uiStreamStory')
  31.   if (!baseSelector || (baseSelector instanceof window.NodeList && !baseSelector.length)) {
  32.     baseSelector = document.querySelector('._wyj._20nr')
  33.   } else {
  34.     if (baseSelector.forEach) {
  35.       baseSelector = baseSelector[baseSelector.length - 1]
  36.     }      
  37.   }
  38.   const allCommentsQuery = '[data-testid="UFI2Comment/root_depth_0"]'
  39.  
  40.   const getCommentsLength = () => baseSelector
  41.     .querySelectorAll(allCommentsQuery).length
  42.  
  43.   const exportToCsv = (filename, rows) => {
  44.  
  45.     var processRow = function (row) {
  46.       return row
  47.       .map(col => {
  48.         let innerValue = col === null ? '' : col.toString()
  49.         if (col instanceof Date) innerValue = col.toLocaleString()
  50.         return '"' + innerValue.replace(/"/g, '\"').replace(/\s+$/, '') + '"'
  51.       })
  52.       .join(';')
  53.     }
  54.  
  55.     let csvFile = rows.map(processRow).join('\n')
  56.  
  57.     var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' })
  58.     if (navigator.msSaveBlob) { // IE 10+
  59.       navigator.msSaveBlob(blob, filename)
  60.     } else {
  61.       var link = document.createElement("a")
  62.       if (link.download !== undefined) { // feature detection
  63.         // Browsers that support HTML5 download attribute
  64.         var url = URL.createObjectURL(blob)
  65.         link.setAttribute("href", url)
  66.         link.setAttribute("download", filename)
  67.         link.style.visibility = 'hidden'
  68.         document.body.appendChild(link)
  69.         link.click()
  70.         document.body.removeChild(link)
  71.       }
  72.     }
  73.   }
  74.  
  75.   const scrapComments = () => {
  76.     console.log('Raspando comentários...')
  77.     const comments = []
  78.     comments.push(['time', 'name', 'comment'])
  79.     baseSelector
  80.       .querySelectorAll(allCommentsQuery)
  81.       .forEach((el) => {
  82.         const $comment = el.querySelector('[data-testid="UFI2Comment/body"]')
  83.         const $actions = el.querySelector('[data-testid="UFI2CommentActionLinks/root"]')
  84.         if (!$comment) return
  85.         const time = $actions.querySelector('abbr').getAttribute('title')
  86.         const name = $comment.querySelector('a').innerText
  87.         const textEl = $comment.querySelector('._3l3x')
  88.         const text = textEl ? textEl.innerText
  89.           .replace(/^[·:\d\s]+/, '')
  90.           .replace(/\n+/g, ' ') : ''
  91.         comments.push([time, name, text])
  92.     })
  93.  
  94.     if (comments.length) {
  95.       // const name = prompt('Digite o nome do arquivo CSV')
  96.       // exportToCsv(name, comments)
  97.       copyResults(comments.map(c => c.join('\t')).join('\n'), comments.length)
  98.     } else {
  99.       alert('Nenhum comentário encontrado')
  100.     }
  101.   }
  102.  
  103.   const expandComments = () => {
  104.     const currentLength = getCommentsLength()
  105.     if (currentLength >= length) {
  106.       return Promise.resolve()
  107.     }
  108.     console.log(`Expandindo comentários ${(currentLength / length * 100).toFixed(2)}%`)
  109.     baseSelector
  110.       .querySelector('[data-testid="UFI2CommentsPagerRenderer/pager_depth_0"]')
  111.       .click()
  112.     return new Promise(function(resolve) {
  113.       const interv = setInterval(() => {
  114.         if (getCommentsLength() > currentLength) {
  115.           clearInterval(interv)
  116.           resolve(expandComments())
  117.         }
  118.       }, 100)
  119.     })
  120.   }
  121.  
  122.   // Initialize
  123.   if (length) {
  124.     return expandComments()
  125.       .then(scrapComments)
  126.   } else {
  127.     return scrapComments()
  128.   }
  129.  
  130. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top