Advertisement
Guest User

8kun Baker tools

a guest
Dec 22nd, 2019
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.   8Kun Baker Tools
  3.   Version 0.1.0
  4.   */
  5. (function ($) {
  6.   'use strict';
  7.  
  8.   var BAKER_TOOLS_WINDOW_ID = "baker-tools-window";
  9.  
  10.   //TODO: Create Dough?
  11.   //TODO: Highlight/filter yous - when they actually work for me again
  12.   //TODO: Nominate notable template
  13.  
  14. /******************************
  15.  * Initialization
  16.  ******************************/
  17.  
  18. /**
  19.  * Sets styles for notable, nominator, nominator in mentions and baker tools window
  20.  */
  21. function setupStyles() {
  22.     var sheet = window.document.styleSheets[0];
  23.     sheet.insertRule(`div.post.reply.${ResearchBread.NOTABLE_CLASS} { background-color: #FFFFCC; }`, sheet.cssRules.length);
  24.     sheet.insertRule(`div.post.reply.${ResearchBread.NOMINATOR_CLASS} { background-color: #FFCCE5;  }`, sheet.cssRules.length);
  25.     sheet.insertRule(`div.post.reply .mentioned .${ResearchBread.NOMINATOR_CLASS} { color: #00CC00; font-weight: bold; font-size: 1.5em}`, sheet.cssRules.length);
  26.     sheet.insertRule(`#${BAKER_TOOLS_WINDOW_ID} { width: 300px; background-color: rgb(214, 218, 240); padding: 5px;
  27.       position: fixed; z-index: 100; float: right; right:28.25px}`, sheet.cssRules.length);
  28.   }
  29.  
  30.   /**
  31.    * Listens for "new_post" event and checks for notable nominations
  32.    */
  33.   function setupEventListeners() {
  34.       $(document).on("new_post", function(e, post) {
  35.         var postBody = post.querySelector('.body')
  36.         $(post).removeAttr("style")
  37.          
  38.         if (ResearchBread.isNominatingPost(post)) {
  39.           ResearchBread.markAsNominator(post);
  40.           var [postNumber, notable] = ResearchBread.getNotableFromNominator(post)
  41.           ResearchBread.markAsNotable(notable)
  42.           ResearchBread.markNominatorInMentions(notable, post);
  43.         }
  44.  
  45.         //New posts br has style inline block which messes up show only notables
  46.         var postBreak = document.querySelector("div.post.reply:last-of-type+br");
  47.       });
  48.   }
  49.  
  50.  
  51.  
  52. /*****************************
  53.  * Research Bread Class
  54.  ****************************/
  55. class ResearchBread {
  56.   static OP_SUBJECT_SELECTOR = ".post.op > p > label > span.subject";
  57.   static POST_BODY_SELECTOR = ".post > .body";
  58.   static POST_SELECTOR = ".post";
  59.   static NOMINATING_REGEX = /notable/i;
  60.   static REPLY_REGEX = /highlightReply\('(.+?)'/;
  61.   static DOUGH_POSTS_REGEX = /^(Welcome To Q Research General|Global Announcements|War Room|QPosts Archives).*/;
  62.  
  63.   static NOMINATOR_CLASS = "notable-nominator";
  64.   static NOTABLE_CLASS = "notable";
  65.  
  66.   /**
  67.    * Get an array of post bodies with dough posts filtered out
  68.    * @returns NodeList of .post elements
  69.    */
  70.   static getPostsWithoutDough() {
  71.     var posts = Array.from(document.querySelectorAll(ResearchBread.POST_SELECTOR));
  72.     var filteredPosts = posts.filter(function(post){
  73.       return !post.querySelector('.body').innerText.match(ResearchBread.DOUGH_POSTS_REGEX);
  74.     })
  75.     return filteredPosts;
  76.   }
  77.  
  78.   /**
  79.    * Determine what the bread number is
  80.    */
  81.   static getBreadNumber() {
  82.     var breadNumberRegex = /#(.+?) /;
  83.     return document.querySelector(ResearchBread.OP_SUBJECT_SELECTOR)
  84.                     .innerText
  85.                         .match(breadNumberRegex)[1] || "COULD NOT FIND BREAD NUMBER";
  86.   }
  87.  
  88.   /**
  89.    * Is the post nominating a notable
  90.    * @arg Element .post
  91.    */
  92.   static isNominatingPost(post) {
  93.     return post.textContent.search(ResearchBread.NOMINATING_REGEX) != -1 && //User declared something notable
  94.            post.querySelector('.body').innerHTML.match(ResearchBread.REPLY_REGEX); // Link to the notable
  95.   }
  96.  
  97.   /**
  98.    * @arg post .post
  99.    * @return RegexMatch Capture 1 is the number
  100.    */
  101.   static getFirstReplyLink(post) {
  102.       var match = post.querySelector('.body').innerHTML.match(ResearchBread.REPLY_REGEX);
  103.       return match && match[1] || null;
  104.   }
  105.  
  106.   /**
  107.    * Finds posts that are being tagged as notable.    
  108.    *
  109.    * I.E. Finding any post that has been replied to by a post with the string "notable" in it.
  110.    * Maybe at somepoint this can be smarter.  Q give me some dwave snow white tech!
  111.    *
  112.    * Highlights notable posts in yellow
  113.    * Highlights nominating posts in pink <3
  114.    * Highlights nominating posts in mentions
  115.    * Add nominee count to post
  116.    */
  117.   static findNominatedNotables() {
  118.     var postsWithoutDough = ResearchBread.getPostsWithoutDough();
  119.  
  120.     //^s to ignore notables review posts
  121.     var nominatingPosts = postsWithoutDough.filter(post => ResearchBread.isNominatingPost(post));
  122.     var notables = [];
  123.  
  124.     nominatingPosts.forEach(function(nominatingPost) {
  125.       var [postNumber, notable] = ResearchBread.getNotableFromNominator(nominatingPost);
  126.  
  127.       if (postNumber) {
  128.         ResearchBread.markAsNominator(nominatingPost);
  129.  
  130.         if (postNumber in notables) {
  131.           var nominatedPost =  notables[postNumber];
  132.           nominatedPost.nominatingPosts.push(nominatingPost);
  133.         } else {
  134.  
  135.           var description = '[DESCRIPTION]'
  136.           // Post is in this bread.
  137.           // TODO: What to do for lb/pb?
  138.           if (notable)  {
  139.             ResearchBread.markAsNotable(notable);
  140.             description = notable.querySelector('.body').innerText.replace(/\n/g,' ');
  141.           }
  142.  
  143.           notables[postNumber] = {
  144.             nominatingPosts: [nominatingPost],
  145.             shortLink: ">>" + postNumber,
  146.             postNumber: postNumber,
  147.             description: description,
  148.             post: notable,
  149.           };
  150.         }
  151.  
  152.         ResearchBread.markNominatorInMentions(notable, nominatingPost);
  153.       }
  154.     });
  155.     console.log(notables)
  156.     return notables;
  157.   }
  158.  
  159.   static getNotableFromNominator(nominatingPost) {
  160.     var postNumber = ResearchBread.getFirstReplyLink(nominatingPost);
  161.     var nominatedPost = document.querySelector("#reply_" + postNumber);
  162.     return [postNumber, nominatedPost]
  163.   }
  164.  
  165.   /**
  166.    * @arg post .post
  167.    */
  168.   static markAsNotable(post) {
  169.     console.info(`Mark as notable: ${post}`)
  170.     post.classList.add(ResearchBread.NOTABLE_CLASS);
  171.   }
  172.  
  173.   /**
  174.    * @arg post .post
  175.    */
  176.   static markAsNominator(post) {
  177.       console.info(`Mark as nominator: ${post}`)
  178.       post.classList.add(ResearchBread.NOMINATOR_CLASS);
  179.   }
  180.  
  181.   static markNominatorInMentions(notablePost, nominatingPost) {
  182.         console.info(`Mark as nominator in mentions.  Notable: ${notablePost}, Nominating Post: ${nominatingPost}`)
  183.         if (!notablePost) {
  184.           console.info(`Notable post is null - possible pb/lb`)
  185.           return;
  186.         }
  187.         var nominatingPostId = nominatingPost.id.replace("reply_","");
  188.         $(notablePost).find('.mentioned-'+nominatingPostId).addClass(ResearchBread.NOMINATOR_CLASS);
  189.   }
  190.  
  191.   /**
  192.    * Toggle whether only the notable/nominee posts are shown or not
  193.    * @arg onlyShowNotables boolean If true, only show notables/nominators, else show all
  194.    */
  195.   static setOnlyShowNotables(onlyShowNotables) {
  196.     var notableOrNominationPostsSelector = `div.post.${ResearchBread.NOTABLE_CLASS}, div.post.${ResearchBread.NOMINATOR_CLASS}`
  197.     var notableOrNominationPostBreaksSelector = `div.post.${ResearchBread.NOTABLE_CLASS}+br,div.post.${ResearchBread.NOMINATOR_CLASS}+br`;
  198.  
  199.     if (onlyShowNotables){
  200.       $(`<style id='baker-tools-hide-non-notable' type='text/css'>
  201.         div.post.reply, div.post.reply+br {display: none !important; visibility: hidden !important;}
  202.         ${notableOrNominationPostsSelector}, ${notableOrNominationPostBreaksSelector}
  203.         { display: inline-block !important; visibility: visible !important; }
  204.         </style>`).appendTo("head")
  205.     } else {
  206.        $("#baker-tools-hide-non-notable").remove();
  207.       // For whatever reason, when the non notable posts are filtered and new posts come through the auto_update,
  208.       // the posts are created with style="display:block" which messes up display.  Remove style attr
  209.        $(ResearchBread.POST_SELECTOR).removeAttr("style")
  210.     }
  211.   }
  212.  
  213.  
  214. }
  215.  
  216.  
  217. /*****************************
  218.  * UI
  219.  * ***************************/
  220.  
  221.   /**
  222.    * Creates the Baker Tools link in the top bar (right next to 8kun options)
  223.    */
  224.   function createBakersToolsLink() {
  225.     var bakerToolsLink = document.createElement("a");
  226.     bakerToolsLink.textContent = "[Baker Tools]"
  227.     bakerToolsLink.style.cssText = "float: right;"
  228.     bakerToolsLink.title = "Baker Tools"
  229.     bakerToolsLink.href = "javascript:void(0)"
  230.     document.querySelector('.boardlist').appendChild(bakerToolsLink)
  231.  
  232.     bakerToolsLink.onclick = function() {
  233.       if($(`#${BAKER_TOOLS_WINDOW_ID}`).is(':visible') ) {
  234.         $(`#${BAKER_TOOLS_WINDOW_ID}`).hide();
  235.       } else {
  236.         $(`#${BAKER_TOOLS_WINDOW_ID}`).css({"top": 15});
  237.         $(`#${BAKER_TOOLS_WINDOW_ID}`).show();
  238.       }
  239.     };
  240.   }
  241.  
  242.   function createBakerWindow() {
  243.       var bakerWindow = document.createElement("div");
  244.       bakerWindow.id = BAKER_TOOLS_WINDOW_ID;
  245.  
  246.       bakerWindow.innerHTML = `
  247.       <h2>Baker Tools</h2>
  248.       <label for="baker-show-only-notable">Only Show Notable/Nomination Posts</label>
  249.       <input type="checkbox" id="baker-show-only-notable" />
  250.       <button id="baker-create-notable-post">Create Notable Post</button>
  251.       <textarea id="baker-notable-controls"></textarea>
  252.       `
  253.       document.body.appendChild(bakerWindow);
  254.  
  255.       $('#baker-show-only-notable').change(function(e) {
  256.         ResearchBread.setOnlyShowNotables(e.target.checked)
  257.       });
  258.       $('#baker-create-notable-post').click(function() {
  259.         if($('#baker-notable-controls').val()) {
  260.           if (!confirm("If you continue, any changes you made will be overwritten!")) {
  261.             return;
  262.           }
  263.         }
  264.         $('#baker-notable-controls').val(createNotablesPost());
  265.       })
  266.       $(bakerWindow).draggable();
  267.       $(bakerWindow).hide();
  268.   }
  269.  
  270.      
  271.   /**
  272.    * Create the notables post for review
  273.    */
  274.   function createNotablesPost() {
  275.     var notables = ResearchBread.findNominatedNotables();
  276.     var breadNumber = ResearchBread.getBreadNumber();
  277.     var post = `'''#${breadNumber}'''\n\n`;
  278.  
  279.     notables.forEach(function(notable) {
  280.       post += `${notable.shortLink} ${notable.description}\n\n`;
  281.     });
  282.  
  283.     return post;
  284.   }
  285.  
  286.  
  287.  
  288.   setupStyles();
  289.   createBakerWindow();
  290.   createBakersToolsLink();
  291.   ResearchBread.findNominatedNotables();
  292.   setupEventListeners();
  293.  
  294.  
  295. })(window.jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement