Skip to content

Instantly share code, notes, and snippets.

@blinry
Created March 8, 2019 19:14
Show Gist options
  • Save blinry/cfd8c3a5951829ed440ad5505b36cce0 to your computer and use it in GitHub Desktop.
Save blinry/cfd8c3a5951829ed440ad5505b36cce0 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Word scrambler
// @namespace https://github.com/blinry
// @description Scrambles words. Based on https://geon.github.io/programming/2016/03/03/dsxyliea
// @include *
// @version 0.1.0
// @grant none
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js
// ==/UserScript==
this.$ = this.jQuery = jQuery.noConflict(true);
"use strict";
$(function() {
var getTextNodesIn = function(el) {
return $(el).find(':not(iframe)').addBack().contents().filter(function() {
return this.nodeType == 3
})
};
function isLetter(char) {
return /^[\d]$/.test(char)
}
function messUpWords() {
for (var i = 0; i < textNodes.length; i++) {
var node = textNodes[i];
for (var j = 0; j < wordsInTextNodes[i].length; j++) {
if (Math.random() > 1 / 10) {
continue
}
var wordMeta = wordsInTextNodes[i][j];
var word = node.nodeValue.slice(wordMeta.position, wordMeta.position + wordMeta.length);
var before = node.nodeValue.slice(0, wordMeta.position);
var after = node.nodeValue.slice(wordMeta.position + wordMeta.length);
node.nodeValue = before + messUpWord(word) + after
}
}
}
function messUpWord(word) {
if (word.length < 3) {
return word
}
return word[0] + messUpMessyPart(word.slice(1, -1)) + word[word.length - 1]
}
function messUpMessyPart(messyPart) {
if (messyPart.length < 2) {
return messyPart
}
var a, b;
while (!(a < b)) {
a = getRandomInt(0, messyPart.length - 1);
b = getRandomInt(0, messyPart.length - 1)
}
return messyPart.slice(0, a) + messyPart[b] + messyPart.slice(a + 1, b) + messyPart[a] + messyPart.slice(b + 1)
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min)
}
var textNodes = [];
var wordsInTextNodes = [];
function findWords() {
textNodes = getTextNodesIn($('div:not(:has(*)), p, h1, h2, h3, h4, span, a, li, small, button, td'));
for (var i = 0; i < textNodes.length; i++) {
var node = textNodes[i];
var words = [];
var re = /\w+/g;
var match;
while ((match = re.exec(node.nodeValue)) != null) {
var word = match[0];
var position = match.index;
words.push({
length: word.length,
position: position
})
}
wordsInTextNodes[i] = words
}
}
findWords()
setInterval(findWords, 10000)
setInterval(messUpWords, 50)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment