Advertisement
Guest User

herp-derp-mokum.user.js

a guest
Aug 19th, 2017
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @id             herp-derp-mokum
  3. // @name           herp derp for mokum
  4. // @version        0.0.0.1
  5. // @description    Post&comment beautifier for Mokum. Based on Herp Derp for Youtube Chrome extension https://www.tannr.com/herp-derp-youtube-comments/
  6. // @author         Tanner Stokes, me, you
  7. // @include        https://*.mokum.place/*
  8. // @match          https://*.mokum.place/*
  9. // ==/UserScript==
  10.  
  11. // Put your favorite users here
  12. var users = ['unmokum', 'unruled'];
  13.  
  14. // Don't look here :(
  15.  
  16. // data to look for derpable elements
  17. // syntax: blocks['unique-parent'] = [ 'child-to-derp', 'child-to-identify-user' (, 'child-of-child-to-...') ]
  18. var blocks = [];
  19. blocks['div.post-body'] = ['div.post.text.post_text_div',  'div.title', 'a'];
  20. blocks['div.comment'] = ['span.comment-text', 'span.author', 'a'];
  21.  
  22. // builds a string with random herps and derps
  23. var derpString = function() {
  24.   var randomLength = Math.floor(Math.random()*20)+1;
  25.   var returnString = '';
  26.  
  27.   for (x=0; x<=randomLength; x++) {
  28.     returnString += (Math.floor(Math.random()*2) ? 'herp ' : 'derp ');
  29.   }
  30.  
  31.   return returnString;
  32. };
  33.  
  34. // derps a comment
  35. var derpComment = function(comment) {
  36.     // don't derp the 'more comments' element
  37.     if (comment.hasChildNodes() && comment.querySelector('a.more-comments') !== null)
  38.         return false;
  39.  
  40.     // preserve the original contents
  41.     comment.derpOriginal = comment.innerHTML;
  42.  
  43.     // revert to the original when clicked
  44.     //comment.onclick = function() {
  45.     //  comment.innerHTML = comment.derpOriginal;
  46.     //};
  47.  
  48.     // change the contents
  49.     comment.innerHTML = derpString();
  50.     return true;
  51. };
  52.  
  53. // derp a block, that contains an element to derp and identification elements
  54. var derpBlock = function(sel) {
  55.     // how do i fucking do it??? what was the selector name again
  56.     blk_selector = [sel.nodeName, sel.getAttribute('class')].join('.').toLowerCase();
  57.     var blk = blocks[blk_selector];
  58.     if (blk === null)
  59.         return;
  60.     // check if a child to be derped exists
  61.     if(sel.querySelector(blk[0]) === null)
  62.         return;
  63.     // go to a child containing username
  64.     var prnt = sel;
  65.     for(i = 1; i < blk.length; i++) {
  66.         prnt = prnt.querySelector(blk[i]);
  67.         if (prnt === null)
  68.             return;
  69.     }
  70.     // we extract username from href
  71.     var usr = prnt.getAttribute("href").substring(1);
  72.     if (!users.includes(usr)) {
  73.         // add derped class even if we don't derp it
  74.         sel.classList.add('derped');
  75.         return;
  76.     }
  77.     // derp the child
  78.     if (derpComment(sel.querySelector(blk[0])))
  79.         // add derped class
  80.         sel.classList.add('derped');
  81. };
  82.  
  83. // build the full selector string from block parents
  84. var derpSelectorString = Object.keys(blocks).map(function(key, index) {
  85.   return key + ':not(.derped)';
  86. }).join(", ");
  87.  
  88. (function() {
  89.     //'use strict';
  90.     document.querySelectorAll(derpSelectorString).forEach(derpBlock);
  91.     // every 200 milliseconds, derp any un-derped elements
  92.     setInterval(function() {
  93.         document.querySelectorAll(derpSelectorString).forEach(derpBlock);
  94.     }, 200);
  95.  
  96. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement