Guest User

Untitled

a guest
Jul 7th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Set date and time when you want to stop this loop
  2. var endLoopAt = new Date('July 08, 2016 08:24:00');
  3.  
  4. // Create a sandboxed environment
  5. var sandBoxedIframe = document.createElement("iframe");
  6. sandBoxedIframe.onload = checkAndUpvote;
  7. document.body.appendChild(sandBoxedIframe);
  8.  
  9. // Used to check if last updated link is same as one being checked now
  10. var lastUpdatedFirstPostLink = "";
  11.  
  12. function checkAndUpvote() {
  13.     // Get document of sandboxed iframe
  14.     var rootDoc = sandBoxedIframe.contentWindow.document;
  15.     // get all posts
  16.     var posts = rootDoc.querySelectorAll("#siteTable > .thing");
  17.     // get top most item from posts
  18.     var mostRecent = posts[0].querySelector(".first > a");
  19.     // get url to comments for the mostRecent post
  20.     var commentUrl = mostRecent.getAttribute("href");
  21.     // Return if this one is same as the one checked in last run
  22.     if(commentUrl === lastUpdatedFirstPostLink) {
  23.         return;
  24.     }
  25.     // else update the env
  26.     lastUpdatedFirstPostLink = commentUrl;
  27.     console.log("Proceeding to upvote first reply!!!");
  28.    
  29.     // Create another sandboxed env for the new post
  30.     var postIframe = document.createElement("iframe");
  31.      
  32.     // Tell the iframe to do this when post page loads inside sandbox
  33.     postIframe.onload = function() {
  34.         // Wait for 2 seconds after the post page has loaded
  35.         setTimeout(function() {
  36.             // Get document of target post page
  37.             var postDocRoot = postIframe.contentWindow.document;
  38.             // Get all replies to the post
  39.             var replies = postDocRoot.querySelectorAll(".sitetable .thing");
  40.             // get upvote button for top most reply
  41.             var firstReply_UpvoteButton = replies[1].querySelector(".midcol .up");
  42.             // Create click event to trigger upon upvote button
  43.             var clickToUpvoteEvent = new MouseEvent("click");
  44.             // Trigger the click event
  45.             firstReply_UpvoteButton.dispatchEvent(clickToUpvoteEvent);
  46.         }, 2000);
  47.        
  48.         // After 30 seconds remove the post sandbox
  49.         setTimeout(function() {
  50.             document.body.removeChild(postIframe);
  51.         }, 30 * 1000);
  52.     };
  53.    
  54.     // load the comment page for the post
  55.     postIframe.src = commentUrl;
  56.     // Add it to body
  57.     document.body.appendChild(postIframe);
  58. }
  59.  
  60. var loop = setInterval(function() {
  61.     // If time right now is more than time it's supposed to end, then end it
  62.     if(Date.now() > endLoopAt.getTime()) {
  63.         clearInterval(loop);
  64.         console.log("End time reached. No more loops will be run again");
  65.         return;
  66.     }
  67.  
  68.     // else refresh the page, which will fire refreshAndUpvote
  69.     console.log("Running loop!!");
  70.     // This will refresh the sandboxed page to update new posts
  71.     sandBoxedIframe.src = "https://www.reddit.com/r/webdev/";
  72. }, 30 * 60 * 1000);
Add Comment
Please, Sign In to add comment