Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Select the node that will be observed for mutations
- var targetNode = document.querySelector("section > div > div");
- var count = 0;
- var likes = 0;
- var retweets = 0;
- var coms = 0;
- var mentions = 0;
- var hashtagCount = 0;
- // Options for the observer (which mutations to observe)
- var config = { attributes: true, childList: true, subtree: true };
- addToStats = function(node) {
- var engagement = node.querySelectorAll('.css-1dbjc4n.r-xoduu5.r-1udh08x')
- if(engagement.length > 2) {
- var mentionsNode = node.querySelector('.css-1dbjc4n.r-4qtqp9.r-zl2h9q')
- var addLikes = parseInt(engagement[2].textContent)
- var addRetweets = parseInt(engagement[1].textContent)
- var addComs = parseInt(engagement[0].textContent)
- var addMentions = mentionsNode? mentionsNode.querySelectorAll('a').length : 1
- var text = node.querySelector('.css-901oao.r-jwli3a.r-1qd0xha.r-a023e6.r-16dba41.r-rjixqe.r-bcqeeo.r-bnwqim.r-qvutc0')
- var hasHashtag= text && text.textContent && text.textContent.includes('#')
- count++
- likes += isNaN(addLikes) ? 0 : addLikes
- retweets += isNaN(addRetweets) ? 0 : addRetweets
- coms += isNaN(addComs) ? 0 : addComs
- mentions += isNaN(addMentions) ? 0 : addMentions
- if(hasHashtag) hashtagCount ++
- console.log([count, likes, retweets, coms, mentions, hashtagCount].join('\t'))
- }
- }
- document.querySelectorAll("section[role='region'] > div > div > div").forEach(e => {
- if(!e.className) {
- addToStats(e)
- }
- })
- // Callback function to execute when mutations are observed
- var callback = function(mutationsList, observer) {
- for(var mutation of mutationsList) {
- //console.log(mutation, " MUTATION")
- if (mutation.type == 'childList') {
- // var text = mutation.addedNodes[0].textContent;
- // var regexResult = text.match(reg);
- if (mutation.addedNodes.length > 0 ) {
- console.log(mutation.addedNodes[0].style.width)
- var node = mutation.addedNodes[0]
- addToStats(node)
- }
- }
- }
- };
- // Create an observer instance linked to the callback function
- var observer = new MutationObserver(callback);
- // Start observing the target node for configured mutations
- observer.observe(targetNode, config);
Advertisement
Add Comment
Please, Sign In to add comment