Advertisement
renix1

memory leak facebook

Oct 3rd, 2018
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function searchForPosts() {
  2.     var elements = document.querySelectorAll('._5jmm');
  3.     return elements;
  4. }
  5.  
  6. function isElementInViewport(el) {
  7.     var rect = el.getBoundingClientRect();
  8.     return rect.bottom > 0 &&
  9.         rect.right > 0 &&
  10.         rect.left < (window.innerWidth || document.documentElement.clientWidth) /* or $(window).width() */ &&
  11.         rect.top < (window.innerHeight || document.documentElement.clientHeight) /* or $(window).height() */;
  12. }
  13.  
  14. function appendToRemove(posts) {
  15.     let found = false;
  16.     let toRemove = [];
  17.     for (let i=0; i<posts.length; i++) {
  18.         if (isElementInViewport(posts[i]) === true) {
  19.             found = true;
  20.         } else {
  21.             if (found === true)
  22.                 toRemove.push(posts[i]);
  23.         }
  24.     }
  25.     return toRemove;
  26. }
  27.  
  28. function removeOutOutOfViewport(toRemove) {
  29.     toRemove.forEach((el) => {
  30.         console.log('Removing element');
  31.         el.innerHTML = '';
  32.     });
  33. }
  34.  
  35.  
  36. function clearMemory () {
  37.     console.log('Running');
  38.     posts = searchForPosts();
  39.     postsToRemove = appendToRemove(posts);
  40.     removeOutOutOfViewport(postsToRemove);
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement