Guest User

Untitled

a guest
Jul 15th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Select the node that will be observed for mutations
  2. var targetNode = document.querySelector("section > div > div");
  3. var count = 0;
  4. var likes = 0;
  5. var retweets = 0;
  6. var coms = 0;
  7. var mentions = 0;
  8. var hashtagCount = 0;
  9. // Options for the observer (which mutations to observe)
  10. var config = { attributes: true, childList: true, subtree: true };
  11.  
  12. addToStats = function(node) {
  13.    
  14.                var engagement = node.querySelectorAll('.css-1dbjc4n.r-xoduu5.r-1udh08x')
  15.                if(engagement.length > 2) {
  16.                    var mentionsNode = node.querySelector('.css-1dbjc4n.r-4qtqp9.r-zl2h9q')
  17.                  
  18.                     var addLikes = parseInt(engagement[2].textContent)
  19.                     var addRetweets = parseInt(engagement[1].textContent)
  20.                     var addComs = parseInt(engagement[0].textContent)
  21.                     var addMentions = mentionsNode? mentionsNode.querySelectorAll('a').length : 1
  22.                     var text = node.querySelector('.css-901oao.r-jwli3a.r-1qd0xha.r-a023e6.r-16dba41.r-rjixqe.r-bcqeeo.r-bnwqim.r-qvutc0')
  23.                     var hasHashtag=  text && text.textContent && text.textContent.includes('#')
  24.                     count++
  25.                     likes += isNaN(addLikes) ? 0 : addLikes
  26.                     retweets += isNaN(addRetweets) ? 0 : addRetweets
  27.                     coms += isNaN(addComs) ? 0 : addComs
  28.                     mentions += isNaN(addMentions) ? 0 : addMentions
  29.                     if(hasHashtag) hashtagCount ++
  30.                     console.log([count, likes, retweets, coms, mentions, hashtagCount].join('\t'))
  31.                  
  32.                  
  33.                }
  34. }
  35.  document.querySelectorAll("section[role='region'] > div > div > div").forEach(e => {
  36.      if(!e.className) {
  37.          addToStats(e)
  38.      }
  39.  })
  40. // Callback function to execute when mutations are observed
  41. var callback = function(mutationsList, observer) {
  42.     for(var mutation of mutationsList) {
  43. //console.log(mutation, " MUTATION")
  44.         if (mutation.type == 'childList') {
  45.          //   var text = mutation.addedNodes[0].textContent;
  46.            //  var regexResult = text.match(reg);
  47.            if (mutation.addedNodes.length > 0 ) {
  48.                console.log(mutation.addedNodes[0].style.width)
  49.                var node = mutation.addedNodes[0]
  50.                addToStats(node)
  51.              
  52.            }
  53.        
  54.          
  55.         }
  56.     }
  57. };
  58.  
  59. // Create an observer instance linked to the callback function
  60. var observer = new MutationObserver(callback);
  61.  
  62. // Start observing the target node for configured mutations
  63. observer.observe(targetNode, config);
  64.  
Advertisement
Add Comment
Please, Sign In to add comment