Advertisement
Danny_Berova

collectReactions

Nov 10th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. async function checkForReactions() {
  2.     return new Promise((resolve, reject) => {
  3.  
  4.         try {
  5.             var maxWaitReactionsTime = 10000;
  6.             var reactions = document.evaluate(".//ul[contains(@class, 'uiList')]/li[div[text()]]", document, null, 7, null)
  7.             // var button = document.evaluate(".//div/a[contains(@class, 'uiMorePagerPrimary')]", reactions.snapshotItem(0), null, 9, null).singleNodeValue;
  8.  
  9.             var reactionsInterval = setInterval(() => {
  10.                 reactions = document.evaluate(".//ul[contains(@class, 'uiList')]/li[div[text()]]", document, null, 7, null)
  11.  
  12.                 if (maxWaitReactionsTime <= 0) {
  13.                     clearInterval(reactionsInterval);
  14.                     console.log('Time finished')
  15.                     resolve(reactions);
  16.                 }
  17.  
  18.                 if (reactions) {
  19.                     clearInterval(reactionsInterval);
  20.                     console.log('reactions length ___' + reactions.snapshotLength)
  21.                     resolve(reactions);
  22.  
  23.                 } else {
  24.                     maxWaitReactionsTime -= 500;
  25.                     console.log('Adding time...')
  26.                 }
  27.  
  28.             }, 500);
  29.         } catch (error) {
  30.             clearInterval(reactionsInterval);
  31.             console.log(error.message);
  32.             resolve(error.message || error)
  33.  
  34.         }
  35.     })
  36. }
  37.  
  38.  
  39.  
  40. async function proccessReactions(allReactions) {
  41.     return new Promise(async (resolve, reject) => {
  42.         try {
  43.  
  44.             var likesLimit = 50;
  45.             var otherReactionsLimit = 20;
  46.             var limit = 0;
  47.  
  48.  
  49.  
  50.             if (allReactions.snapshotLength > 0) {
  51.  
  52.                 for (let i = 0; i < allReactions.snapshotLength; i++) {
  53.                     const currReaction = allReactions.snapshotItem(i);
  54.  
  55.                     var reactionType = document.evaluate("./div[text()]", currReaction, null, 9, null).singleNodeValue;
  56.                     if (reactionType.textContent) {
  57.                         var reactionText = reactionType.textContent;
  58.                         console.log(reactionText);
  59.                         var clicking = await processSingleReactionByType(reactionText, currReaction);
  60.                         console.log(clicking)
  61.  
  62.                         var reactiosByType = document.evaluate(".//ul/li[//a]", currReaction, null, 7, null)
  63.                         console.log(reactiosByType.snapshotLength);
  64.                         //if reactionsByType.snapshotLength;
  65.                         limit = reactionText === 'Like' ? likesLimit : otherReactionsLimit;
  66.                         console.log(limit)
  67.                         for (let j = 0; j < reactiosByType.snapshotLength; j++) {
  68.                             const el = reactiosByType.snapshotItem(j);
  69.                             if (j < limit) {
  70.                                 var aTitle = document.evaluate(".//div[@class='clearfix']/a", el, null, 9, null).singleNodeValue;
  71.                                 console.log(j)
  72.                                 if(aTitle.getAttribute('title')) {
  73.                                     console.log(aTitle.getAttribute('title') + ' ::: ' + reactionText)
  74.                                 }
  75.                                 // if(aTitle.getAttribute('ajaxify')) {
  76.                                 //     console.log(aTitle.getAttribute('ajaxify') + ' ::: ' + reactionText)
  77.                                 // }
  78.                                 // if(aTitle.getAttribute('data-hovercard')) {
  79.                                 //     console.log(aTitle.getAttribute('data-hovercard') + ' ::: ' + reactionText)
  80.                                 // }
  81.                             } else {
  82.                                 continue;
  83.                             }
  84.  
  85.                         }
  86.  
  87.                     } else {
  88.                         console.log('No text content for reaction type...');
  89.                         continue;
  90.                     }
  91.  
  92.  
  93.                 }
  94.             } else {
  95.                 resolve('No reactions to collect')
  96.             }
  97.  
  98.         } catch (e) {
  99.             resolve(e.message || e)
  100.  
  101.         }
  102.     })
  103. }
  104.  
  105. async function processSingleReactionByType(type, reaction) {
  106.     return new Promise((resolve, reject) => {
  107.  
  108.         try {
  109.             var maxWaitReactionsTime = 10000;
  110.             var counter = 0;
  111.             // var reactions = document.evaluate(".//ul[contains(@class, 'uiList')]/li[div[text()]]", document, null, 7, null)
  112.             var button = document.evaluate(".//div/a[contains(@class, 'uiMorePagerPrimary')]", reaction, null, 9, null).singleNodeValue;
  113.  
  114.             var buttonInterval = setInterval(() => {
  115.                 button = document.evaluate(".//div/a[contains(@class, 'uiMorePagerPrimary')]", reaction, null, 9, null).singleNodeValue;
  116.  
  117.                 if (maxWaitReactionsTime <= 0) {
  118.                     clearInterval(buttonInterval);
  119.                     console.log('Time finished')
  120.                     resolve(true);
  121.                 }
  122.  
  123.                 if (button) {
  124.  
  125.                     if (counter >= 1) {
  126.                         clearInterval(buttonInterval);
  127.                         console.log('button clicked enough ___' + counter)
  128.                         resolve(true);
  129.                     } else {
  130.                         button.click();
  131.                         counter += 1;
  132.                         console.log('Clicking...')
  133.                     }
  134.  
  135.  
  136.                 } else {
  137.                     maxWaitReactionsTime -= 2000;
  138.                     console.log('Adding time...')
  139.                 }
  140.  
  141.             }, 2000);
  142.         } catch (error) {
  143.             clearInterval(buttonInterval);
  144.             console.log(error.message);
  145.             resolve(error.message || error)
  146.  
  147.         }
  148.     })
  149. }
  150.  
  151. async function startCollection() {
  152.  
  153.     var result = await checkForReactions();
  154.     if (result && result.snapshotLength) {
  155.         console.log(result.snapshotLength);
  156.         await proccessReactions(result)
  157.     } else {
  158.         console.log('No reactions found!')
  159.         return;
  160.         //finalize();
  161.     }
  162. }
  163.  
  164. startCollection();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement