Помните это пропущенное сообщение о цели, в котором сказано, как:
Aoccdrnig to rscheearch в Cmabrigde Uinervtisy, он не имеет никакого отношения к тому, чтобы быть втянутыми в ловушку: olny iprmoetnt tihng - это frist и lsat ltteer на rghit pclae. Rset может быть тотальным mses, и вы можете сидеть сломать его в wouthit porbelm. Тих - это bcuseae huamn mnid deos, не растерзанный ervey leter by istlef, но wrod как wlohe.
В любом случае я пытаюсь сделать функцию, которая будет делать это на всей странице. Для этой функции существует несколько правил.
- осталось менее 4 символов.
- не буквенно-цифровые символы не считаются частью слова.
- переносимые слова - это действительно два слова
- слова должны быть искажены, если length >= 4 (не может быть как оригинал)
- Первый и последний символы остаются неизменными, и только средние символы становятся искаженными (спасибо Hersheezy)
- текст всегда должен быть случайным и создавать уникальные искажения при каждом прогоне
- Чистый javascript и итерации на всех текстовых узлах
- Побеждает самый короткий сладкий код.
В любом случае, это кажется достаточно простым для реализации, как насчет начала конкурса, чтобы узнать, кто может сделать самый чистый код для выполнения этой задачи. Не стесняйтесь брать в долг без признания моего кода (у меня есть)
Если я пропустил что-нибудь, добавьте его в комментарии. В любом случае я работал над этим очень халявно, и здесь я показываю свою менее чем парную работу.
var i, j, words, textNodes, punct = /[^a-zA-Z0-9]/;
Array.prototype.shuffle = function() {
for (var i = 0; i < this.length; i++) {
var j = i;
while (j == i) {
j = Math.floor(Math.random() * this.length);
}
var tmp = this[i];
this[i] = this[j];
this[j] = tmp;
}
return this;
};
String.prototype.shuffle = function() {
return this.split('').shuffle().join('');
};
function transverse(element, array) {
if (!array) array = [];
if (element.nodeType === 3) {
array.push(element);
} else {
for (var i = 0; i < element.childNodes.length; i++) {
transverse(element.childNodes[i], array);
}
}
return array;
}
function garble(str) {
if (!str) return '';
str = str.trim();
if (/-/.test(str)) {
str = str.split('-');
for (var i = 0; i < str.length; i++) {
str[i] = garble(str[i]);
}
return str.join('-')
}
if (punct.test(str.charAt(0))) {
return str.charAt(0) + garble(str.slice(1));
}
if (punct.test(str.charAt(str.length - 1))) {
return garble(str.slice(0, -1)) + str.charAt(str.length - 1);
}
if (str.length < 4) return str;
if (str.length === 4) return str.charAt(0) + str.charAt(2) + str.charAt(1) + str.charAt(3)
return str.charAt(0) + str.substr(1, str.length - 2).shuffle() +
str.charAt(str.length - 1);
}
window.onload = function() {
textNodes = transverse(document.documentElement);
for (i = 0; i < textNodes.length; i++) {
words = textNodes[i].data.split(' ');
for (j = 0; j < words.length; j++) {
words[j] = garble(words[j]);
}
textNodes[i].data = words.join(' ');
}
};