Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Set date and time when you want to stop this loop
- var endLoopAt = new Date('July 08, 2016 08:24:00');
- // Create a sandboxed environment
- var sandBoxedIframe = document.createElement("iframe");
- sandBoxedIframe.onload = checkAndUpvote;
- document.body.appendChild(sandBoxedIframe);
- // Used to check if last updated link is same as one being checked now
- var lastUpdatedFirstPostLink = "";
- function checkAndUpvote() {
- // Get document of sandboxed iframe
- var rootDoc = sandBoxedIframe.contentWindow.document;
- // get all posts
- var posts = rootDoc.querySelectorAll("#siteTable > .thing");
- // get top most item from posts
- var mostRecent = posts[0].querySelector(".first > a");
- // get url to comments for the mostRecent post
- var commentUrl = mostRecent.getAttribute("href");
- // Return if this one is same as the one checked in last run
- if(commentUrl === lastUpdatedFirstPostLink) {
- return;
- }
- // else update the env
- lastUpdatedFirstPostLink = commentUrl;
- console.log("Proceeding to upvote first reply!!!");
- // Create another sandboxed env for the new post
- var postIframe = document.createElement("iframe");
- // Tell the iframe to do this when post page loads inside sandbox
- postIframe.onload = function() {
- // Wait for 2 seconds after the post page has loaded
- setTimeout(function() {
- // Get document of target post page
- var postDocRoot = postIframe.contentWindow.document;
- // Get all replies to the post
- var replies = postDocRoot.querySelectorAll(".sitetable .thing");
- // get upvote button for top most reply
- var firstReply_UpvoteButton = replies[1].querySelector(".midcol .up");
- // Create click event to trigger upon upvote button
- var clickToUpvoteEvent = new MouseEvent("click");
- // Trigger the click event
- firstReply_UpvoteButton.dispatchEvent(clickToUpvoteEvent);
- }, 2000);
- // After 30 seconds remove the post sandbox
- setTimeout(function() {
- document.body.removeChild(postIframe);
- }, 30 * 1000);
- };
- // load the comment page for the post
- postIframe.src = commentUrl;
- // Add it to body
- document.body.appendChild(postIframe);
- }
- var loop = setInterval(function() {
- // If time right now is more than time it's supposed to end, then end it
- if(Date.now() > endLoopAt.getTime()) {
- clearInterval(loop);
- console.log("End time reached. No more loops will be run again");
- return;
- }
- // else refresh the page, which will fire refreshAndUpvote
- console.log("Running loop!!");
- // This will refresh the sandboxed page to update new posts
- sandBoxedIframe.src = "https://www.reddit.com/r/webdev/";
- }, 30 * 60 * 1000);
Add Comment
Please, Sign In to add comment