Guest User

Untitled

a guest
Mar 3rd, 2016
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. $(function(){
  4.  
  5.     var getTextNodesIn = function(el) {
  6.         return $(el).find(":not(iframe)").addBack().contents().filter(function() {
  7.             return this.nodeType == 3;
  8.         });
  9.     };
  10.  
  11.     // var textNodes = getTextNodesIn($("p, h1, h2, h3"));
  12.     var textNodes = getTextNodesIn($("*"));
  13.  
  14.  
  15.  
  16.     function isLetter(char) {
  17.         return /^[\d]$/.test(char);
  18.     }
  19.  
  20.  
  21.     var wordsInTextNodes = [];
  22.     for (var i = 0; i < textNodes.length; i++) {
  23.         var node = textNodes[i];
  24.  
  25.         var words = []
  26.  
  27.         var re = /\w+/g;
  28.         var match;
  29.         while ((match = re.exec(node.nodeValue)) != null) {
  30.  
  31.             var word = match[0];
  32.             var position = match.index;
  33.  
  34.             words.push({
  35.                 length: word.length,
  36.                 position: position
  37.             });
  38.         }
  39.  
  40.         wordsInTextNodes[i] = words;
  41.     };
  42.  
  43.  
  44.     function messUpWords () {
  45.  
  46.         for (var i = 0; i < textNodes.length; i++) {
  47.  
  48.             var node = textNodes[i];
  49.  
  50.             for (var j = 0; j < wordsInTextNodes[i].length; j++) {
  51.  
  52.                 // Only change a tenth of the words each round.
  53.                 if (Math.random() > 1/10) {
  54.  
  55.                     continue;
  56.                 }
  57.  
  58.                 var wordMeta = wordsInTextNodes[i][j];
  59.  
  60.                 var word = node.nodeValue.slice(wordMeta.position, wordMeta.position + wordMeta.length);
  61.                 var before = node.nodeValue.slice(0, wordMeta.position);
  62.                 var after  = node.nodeValue.slice(wordMeta.position + wordMeta.length);
  63.  
  64.                 node.nodeValue = before + messUpWord(word) + after;
  65.             };
  66.         };
  67.     }
  68.  
  69.     function messUpWord (word) {
  70.  
  71.         if (word.length < 3) {
  72.  
  73.             return word;
  74.         }
  75.  
  76.         return word[0] + messUpMessyPart(word.slice(1, -1)) + word[word.length - 1];
  77.     }
  78.  
  79.     function messUpMessyPart (messyPart) {
  80.  
  81.         if (messyPart.length < 2) {
  82.  
  83.             return messyPart;
  84.         }
  85.  
  86.         var a, b;
  87.         while (!(a < b)) {
  88.  
  89.             a = getRandomInt(0, messyPart.length - 1);
  90.             b = getRandomInt(0, messyPart.length - 1);
  91.         }
  92.  
  93.         return messyPart.slice(0, a) + messyPart[b] + messyPart.slice(a+1, b) + messyPart[a] + messyPart.slice(b+1);
  94.     }
  95.  
  96.     // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random
  97.     function getRandomInt(min, max) {
  98.        
  99.         return Math.floor(Math.random() * (max - min + 1) + min);
  100.     }
  101.  
  102.  
  103.     setInterval(messUpWords, 50);
  104. });
Advertisement
Add Comment
Please, Sign In to add comment