baptx

facebook_display_load_all

Feb 12th, 2021 (edited)
22
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This script will allow you to use the Ctrl+F search function of your web browser to search text in all posts / comments
  2.  * (without sending your search requests to Facebook and the HTML page can be saved with Ctrl+S as a backup for offline use).
  3.  * It is also useful if you want to save the content of a Facebook group that requires you to renew a paid membership to remain a member
  4.  * (video / audio / image and other files should be downloaded separately) */
  5.  
  6. // replace with your language translation if needed (case-sensitive)
  7. var moreCommentText = "^View 1 more comment$"; // if the text is only "comment", script will click on unintended links containing the word "comment" in the page title
  8. var moreCommentsText = "^View [0-9]+ more comments$";
  9. var previousCommentsText = "^View previous comments$";
  10. var moreReplyText = "^View 1 more reply$";
  11. var moreRepliesText = "^View [0-9]+ more replies$";
  12. var oneReplyText = "^View 1 reply$";
  13. var allRepliesText = "^View all [0-9]+ replies$";
  14. var replyText = "^1 Reply$";
  15. var repliesText = "^[0-9]+ Replies$"; // avoid clicking on "Hide X Replies"
  16. var seeMoreText = "See More"; // no need for ^ / $ delimeters since regex is not used
  17.  
  18. /* Scroll to the bottom of the page or to the limit you want to display before executing the function displayLoadAll().
  19.  * You may need to run the function 2 times or more to load everything like nested replies.
  20.  * You may also need to scroll back slowly to the other side of the page to refresh the DOM with pending HTTP requests before running the function again or saving page.
  21.  * The function displayContent() can be executed if necessary to display hidden content before saving the page, which should be done at the bottom of the page to avoid display issues */
  22. function displayLoadAll()
  23. {
  24.     // promise chain fixed based on https://stackoverflow.com/questions/66164973/promise-chain-does-not-wait-until-other-promise-is-resolved/66165074#66165074
  25.     /*displayContent()
  26.     .then(loadCommentsReplies)
  27.     .then(loadSeeMore);*/
  28.    
  29.     loadCommentsReplies()
  30.     .then(loadSeeMore);
  31. }
  32.  
  33. function displayContent()
  34. {
  35.     return new Promise(function(resolve) {
  36.         var list = document.querySelectorAll("[role=feed]")[0].childNodes;
  37.         var length = list.length;
  38.         for (var i = 1; i < length - 2; ++i) { // ignore first and last 2 elements
  39.             var target = list[i].firstChild.firstChild.firstChild.firstChild.firstChild.firstChild;
  40.             // unhide content when needed only
  41.             if (target.hidden) {
  42.                 console.log("displayContent", i, length);
  43.                 target.removeAttribute("hidden");
  44.                 target.style.display = "block";
  45.             }
  46.         }
  47.         resolve();
  48.     });
  49. }
  50.  
  51. function loadCommentsReplies()
  52. {
  53.     return new Promise(function(resolve) {
  54.         var list = document.querySelectorAll("span > span[dir=auto], span > span[dir=auto] > div > div:nth-child(4)"); // avoid using random class names that can change
  55.         var length = list.length;
  56.        
  57.         var i = -1;
  58.         function listLoop() {
  59.             if (++i < length) {
  60.                 // avoid clicking on unwanted buttons
  61.                 if (list[i].firstChild.nodeValue && list[i].firstChild.nodeValue.match(
  62.                 moreCommentText + "|"
  63.                 + moreCommentsText + "|"
  64.                 + previousCommentsText + "|"
  65.                 + moreReplyText + "|"
  66.                 + moreRepliesText + "|"
  67.                 + oneReplyText + "|"
  68.                 + allRepliesText + "|"
  69.                 + replyText + "|"
  70.                 + repliesText)) {
  71.                     console.log("loadCommentsReplies", i, length);
  72.                         // timeout not needed to send all requests since we have to scroll back to send pending requests for the displayed content
  73.                         //setTimeout(function(){
  74.                             list[i].click();
  75.                             listLoop();
  76.                         //}, 1000);
  77.                 }
  78.                 else {
  79.                     listLoop();
  80.                 }
  81.             }
  82.             else {
  83.                 resolve();
  84.             }
  85.         }
  86.         listLoop();
  87.     });
  88. }
  89.  
  90. function loadSeeMore()
  91. {
  92.     return new Promise(function(resolve) {
  93.         // use querySelectorAll instead of getElementsByClassName since it returns a static list instead of a live list (to avoid undefined error when links are clicked and removed from the DOM)
  94.         var list = document.querySelectorAll("div[role=button]");
  95.         var length = list.length;
  96.        
  97.         var i = -1;
  98.         function listLoop() {
  99.             if (++i < length) {
  100.                 // avoid clicking on like buttons by mistake if Facebook changes DOM structure
  101.                 if (list[i].firstChild.nodeValue == seeMoreText) {
  102.                     console.log("loadSeeMore", i, length);
  103.                     // timeout not needed unless there is a robot detection to bypass (data already present)
  104.                     //setTimeout(function(){
  105.                         list[i].click();
  106.                         listLoop();
  107.                     //}, 1000);
  108.                 }
  109.                 else {
  110.                     listLoop();
  111.                 }
  112.             }
  113.             else {
  114.                 resolve();
  115.             }
  116.         }
  117.         listLoop();
  118.     });
  119. }
  120.  
RAW Paste Data